
The same person can be a teacher as well as a player, As a person we have many different forms like student, teacher, player, father/mother etc. To take a real time example, we can consider ourself. The same happens with class B object as well which can take the form of it's own and class A. This means object of C class is polymorphic in nature. In this case the object of class C can be assigned into class C, classī or class A which means the object of class C can take form of it's own, class B or class A.ī b = c or B b = new C() // C class object will act as B class objectĪ a = c or A a = new C() // C class object will act as A class objectĪs you can see object of class C can be assigned into any of it's parent class which means class C object can take forms of class B and class A as well along with it's own. The form of any of it's parent classes in it's inheritance hierarchy, so we can say all the parent classes are basically different forms of child class, which is what the polymorphism is.įor example let's consider a scenario in which we have class A, B and C where class C extends class B which extends class A. Programmatically, in java the object of a child class can be assigned in any of its parent classes in the inheritance hierarchy which means the object of a subclass can take Polymorphims in java is a mechanism in which an object or it's behavior can have many different forms, we call such objects as polymorphic object. So the word polymorphism means many forms. The word polymorphism is made from two words, poly which means many and morphism which means forms or types, This tutorial covers different aspect of polymorphism like what is polymorphism, real world example,Īdvantages of polymorphism etc. So we will try to see this with some real world examples and programs to understand this easily. Sometimes beginners find it little difficult to understand what exactly the polymorphism is, Polymorphism is another fundamental principal of object-oriented programming. ➤ Overriding using Covariant Return Type.➤ Overloading and Overriding Differences.➤ Runtime and Compile-time Polymorphism.➤ Overflow Underflow Widening Narrowing.Overriding – Different Class, Same Method Name, Same Parameters. Overloading – Same Class, Same Method Name, Different Parameters Note : To easily remember Overloading and Overriding Here super.disp() will help you calling the Parent Class disp(). Then the same above example can be re-written as class ParentClass If you wish to call the ParentClass method through the ChildClass Method then we can use the super keyword to do so. This is called as Dynamic Binding or Late Binding or Runtime Polymorphism Use of Super Keyword in Overriding The Child Class disp is called because though the obj2 reference may be a ParentClass reference but the object is ChildClass object and hence the disp() of the child class is called.
#Polymorphism java code
The above code gives you the below output. Child Class disp () will be called, as it reference to the child class. ParentClass reference but ChildClass Object. Method Overriding is possible only through inheritance. Method overriding is almost the same as Method Overloading with a slight change, overriding has the same method name, with the same number of arguments but the methods present in the different classes.


The answer is based on the parameter the compiler will choose which methods to be called. Here the disp() method will be called three times, but the question is how the different disp() are called. Inside Third disp method, values are : JavaInterview,Point

Inside Second disp method, value is: Java Interview The output will be Inside First disp method Oo.disp("JavaInterview", "Point") //Calls the third disp method Oo.disp("Java Interview") //Calls the second disp method ("Inside Third disp method, values are : "+val1+","+val2) Public void disp(String val1,String val2) ("Inside Second disp method, value is: "+val) Method Overloading implies you have more than one method with the same name within the same class but the conditions here is that the parameter which is passed should be different. Method Overriding What is Method Overloading in Java?
