Are you sure? because void of display method of Parent is missing.
Rahul Zanwar wrote:the above code works properly
Only if the parent class's method is overridden in the child class.
according to the law of polymorphism, the method calling should be:
when a parent class reference holds instance of child class, the child class method will be called.
This will not work because, below code give compile time error Type mismatch: cannot convert from Child to Parent, because Child is not sub class of Parent. and although you add ; it gives compile time error which says The method display() from the type Parent is not visible, because it has private access modifier which means that method can only be accessed within Parent class but here you are trying to access it in Test class which is invalid. So this will not work.
If this is working, then what is the rational behind this.?
Ohh I see,
Rob Spoor wrote:For every set of overloaded methods, the compiler will use the most specific one
Most specific method is having Integer then after that would be Number then Object. am I correct?
In your second example, there are three varargs methods, and so the most specific one is the Integer... one.
Yes I got that now, It was too confusing but your explanation cleared it. Thank you so much
Now, if your override in OneSubclass would also have used Integer... instead of Integer, you' see the same behavior as the second example.
Yes helped a lot, Wiki is really a great source of information, I better peruse them. Thank you
Campbell Ritchie wrote:I hope that will help
I kept pondering on that how is this printing correct output instead of fullyqualifiedclassname@hashcode then got detail information in source code, It was really awesome. I never knew we could print in this manner also.
Carey Brown wrote:
Ohh I see, you mean declare common fields of Pets in Pet abstract class. Common behaviours of Pets comes in Pet abstract class like (Just assumed) sleeping(), drinkingWater() as defined methods of Pet abstract class whose implementation we know and methods (behaviour) which are type specific like makeNoise() where Dog barks and Cat meows so their implementation will be in respective classes i.e. type specific. Means when we know partial implementation then go for abstract class and when have no idea of implementation then go for interface, hope I'm correct?
Campbell Ritchie wrote:Maybe. But surely you are better off having an abstract Pet class and subclasses Dog, Cat, etc etc.