Welcome to
CodeRanch!
Rahul Zanwar wrote:the above code works properly
Are you sure? because void of display method of Parent is missing.
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.
Only if the parent class's method is overridden in the child class.
Here in your code, Child class doesn't extend Parent class
If this is working, then what is the rational behind this.?
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.