+ 1
Question on Abstraction
What is the difference between the following snippets of code: abstract class Animal{ abstract void makeSound(); } class Cat extends Animal{ public void makeSound(){ System.out.println("Meow! Meow!"); } } public class MyProgram{ public static void main(String[] args){ Animal cat = new Cat(); cat.makeSound(); } } and class Cat{ public void makeSound(){ System.out.println("Meow! Meow!"); } } public class MyProgram{ public static void main(String[] args){ Cat cat = new Cat(); cat.makeSound(); } } They both print "Meow! Meow!" but the upper one is an abstraction. What is the value of having an abstraction in this case?
1 Respuesta
+ 19
Abstraction is necessary as the abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.
That's very handy for me! :-D