Polymorphism


Polymorphism in Object-Oriented-Programming means "one name, many forms". It allow objects of a common superclass. The main idea is that the same function or method can behave differently depending on the object that calls it. Polymorphism have two types, such as method overloading & method Overriding.
                
                        1. Compile time Polymorphism - Achieved through Method Overloading
                        
                        2. Runtime Polymorphism - Achieved through Method Overriding
    



  •  Method Overloading (Compile time Polymorphism)
When multiple method in the same class have the same name but different parameters (type, number or order) it is called method overloading.

Key Points:
  • Occurs within the same class.
  • Resolved at compile time.
  • Improves code readability and flexibility.
 Example code : 





  • Method Overriding (Runtime Polymorphism)
Method Overriding is one more type of Polymorphism. The following example code shows the two java classes, parent and child. In this case, the parent class is extended to the child class and the method in the parent class is called from the child class. The method of the parent class is called and the output is given






In the example code given below, you can see how a method similar to the method in the parent class has been made in the child class. In that case, the child class has been extended to the parent class, but the OBJ calls the same method as the child class












Comments

Popular posts from this blog

Java Design Patterns

Application Programming Interface (API)