Polymorphism

 1. Polymorphism 

    Static polymorphism can be by change in number of arguments or by change in data type passed      to function.

   Runtime polymorphism can be obtained when same method is defined in parent and sub class.

   Sub class object can be assigned to parent class object. But when same name method is called by       above assigned parent object, then sub class method is called, not parent class method.

              For e.g. parent class Parent contains method print("In Parent class")
                            Subclass S1,S2 contains method print("In sub class 1"),print("In sub Class 2")                                    respectively                              

                            Inside Test class -
                                Parent P=new S1(); P.print();// will print In subclass 1
                                Parent P=new S2();P.print();// 
will print In subclass 2

Refer- https://www.geeksforgeeks.org/polymorphism-in-java/

    

Comments

Popular posts from this blog

Checked and Unchecked Exception in Java

Static Keyword in Java