This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
This is out of the SCJP book by Kathy and Bert. I added some code to play with. The line Fruit b = new Apple(); what does this actually do? If I do Fruit b = new Fruit I get an error because Fruit is abstract I know that. [ July 26, 2007: Message edited by: James Hambrick ]
sorry, but thats in the next chapter. I will hopefully understand this and find my answer in the next chapter. I was taught at college procedural programming with COBOL, C and VB6. Never really used objects much. I graduated in 2002 so they should have showed me some object stuff, more than they did. My C++ professor did not know C, my web programming professor taught us HTML and when we got to Java he just expected us to edit a program to add functinality. at this time we only had taken COBOL, one student took C by then and kinda knew what he was doing.
Here we are doing. Program to a supertype. Here super type can be interface or a abstract class. The point here is to exploit polymorphism by programming to a supertype so that the actual runtime object isnt locked in the code. With the power polymorphism we can change the behavior of objects at run time. Here is a simple example.
Now suppose my requirement is to write a method that takes a list of any animal object and to make them make sound , If I would program to a super type , I will have to do the following
So here in Test class i will have to write three methods for each Animal object so that i could make them make Sound. Now in future if i have another new animal i will have to modify Test class and include another new method for the new animal , And this will go on for ever.
Now comes Program to supertye
Now i have only one method that takes Animal reference as arugument. Since i have programmed to super type Animal a = dog , All i have to do is just invoke the method makeSound() with Animal reference, Now @ run time depending on the appropriate object type, makeSound() will be invoked. With this approach if in future i have a new animal type, I will not have to modify my Test class. Hence the class is closed for modification. Always we need to code in such a way that A class is closed for modification. . I can go on and on on this topic. But i hope, Now you understand the power of programming to supertype. Thanks Deepak
You're right cowboy. Don't try to ride a wild mustang when you have problems with ponys. What we are talking about here is called polymorphy and this is one of the key concepts of object oriented programming.
I modified Deepak's code a bit so it is now compiling:
In class Zoo the variable "animal" is of type Animal and can take any subtype of Animal as an object. Only not Animal itself because the class is abstract and cannot be instanciated. Hence no animal objects, only Animal type. But in this code only at runtime (due to the random) it is clear, what kind of an animal the variable will hold. The same is true for the array named "zoo". In the line "assing" an object of a subclass of Animal (thats is refered by the variable "animal") will taken as an element of the array.
And in the listening part you will also see polymorphy. Class animal demands a method makeSound from all non-abstract classes that extend Animal. It also has a non-abstract method (toString) that will be inherited by the subclasses.
Output is something like: I am a Duck Quack I am a Dog Bark I am a Dog Bark I am a Dog Bark I am a Dog Bark I am a Dog Bark I am a Duck Quack I am a Dog Bark I am a Duck Quack I am a Cat Meow
So having something like Animal any = new Cat(); It is sure, that the object refered by variable any surely has all methods, that are abstract in Animal in a concrete implementation.
An interface can be regarded as a special form of an abstract class that has no concrete (non-abstract) methods at all. But that would be a differend thread.