0

Need help with Polymorphism please help !!

let's say we have two classes (A and B) while B inherits from A and they both have the same methode called AlphabetOrder() that returns the order of the letters A or B in the alphabet so in this case 1 or 2 , so when we do : A obj=new B(); System.out.println(obj.AlphabetOrder()); Why it goes always for the B methode despite the obj is of type A?

17th Dec 2017, 10:14 PM
HAMDI
HAMDI - avatar
2 Réponses
0
B class overridden A method. Inheritance means B inherited some methods in A. And if not overridden it will use A methods. However you should not be able to do the following: B obj = new A (); Since B might have methods A does not have. Polymorphism defines a base class (which can be virtual. That means that all descendant Classes must implement the virtual methods). However you can have a base class container var or var [] that contains descendant objects but uses the correct method that they share.
18th Dec 2017, 12:26 AM
H Chiang