What is Polymorphism?
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
How Polymorphism support in Java?
Java has excellent support of polymorphism in terms of method overloading (compile time polymorphism) and method overriding (runtime polymorphism).
Example
public class A{ public String getMessage(){ return "calling A"; } } public class B extends A{ public String getMessage(){ return "calling B"; } } public class C extends A{ public String getMessage(){ return "calling C"; } }
What is Method Overloading (Static Binding or compile time polymorphism)
If a class have multiple methods by same name but different parameter is known as Method Overloading.
How to overload a method?
There are two way to overload the method.
- By changing number of arguments.
- By changing the data type of arguments.
Note: In Java, Method overloading is not possible with changing return type of methods.
Example1
public class Calculation{ public void sum(int a, int b){ System.out.println(a+b); } public void sum(int a, int b, int c){ System.out.println(a+b+c); } public static void main(String args[]){ Calculation test = new Calculation(); test.sum(10,20); test.sum(10,20,30); } }Try it Yourself
Example2
public class Calculation{ public void sum(int a, int b){ System.out.println(a+b); } public void sum(double a, double b){ System.out.println(a+b); } public static void main(String args[]){ Calculation test = new Calculation(); test.sum(10,20); test.sum(10.5,20.5); } }Try it Yourself
Rules for Method Overloading:
- Overloading can take place in the same class or in its sub-class.
- Constructor in Java can be overloaded.
- Overloaded methods must have a different argument list.
- Overloaded method should always be the part of the same class (can also take place in sub class), with same name but different parameters.
- The parameters may differ in their type or number, or in both.
- It is also known as compile time polymorphism.
Advantage of Method Overloading:
Method overloading increases the readability of program.
What is Method Overriding (Dynamic Binding or Runtime Polymorphism)
Having the same method in the subclass as declare in parent class is known as method overriding.
How to override a method?
- It MUST have the same argument list (if not, it might be a case of overloading).
- It MUST have the same return type.
- It MUST NOT have more restrictive access modifier, but MAY have less restrictive one.
Example2
public class person { void message(){ System.out.println("welcome"); } } class student1 extends person{ void message() { System.out.println("This is subclass method"); } public static void main(String[] args) { student1 s=new student1(); s.message(); } }Try it Yourself
Rules for Method Overriding:
- Applies only to inherited methods.
- Object type (NOT reference variable type) determines which overridden method will be used at runtime.
- Overriding method must not have more restrictive access modifier.
- Abstract methods must be overridden.
- Static and final methods cannot be overridden.
- Constructors cannot be overridden.
- It is also known as Runtime polymorphism.
Advantages of Method Overriding:
- Time to invest method signature is reduced.
- Different functionality in both super class and sub class by sharing same signature.
- The functionality can be enhanced.
- The behavior can be replaced in the sub class.
- Method Overriding is used for Runtime Polymorphism.
Solve this!!
12402.What is the output of this program?
class A { public void display() { System.out.println("A"); } } class B extends A { public void display() { System.out.println("B"); } } class Demo { public static void main(String args[]){ A a1=new A(); a1.display(); A a = new B(); a.display(); } }
A B
B A
A A
B B
12403.What is the output of this program?
class A { int i=20; } class B extends A { int i=30; } class method_overriding { public static void main(String args[]) { A obj = new B(); System.out.println(obj.i); B a1=new B(); System.out.println(a1.i); } }
30 20
20 30
20
30
- Collection
- ArrayList
- LinkedList
- Vector
- HashSet
- Conditional Statements
- Classes and Objects
- Inheritance
- Packages
- Exception Handling
- Threads
- TreeSet
- LinkedHashSet
- HashMap
- TreeMap
- LinkedHashMap
- HashTable
- Iterator and ListIterator
- String
- Scanner
- Array Small Programs
- Introduction
- Variables
- DataTypes
- Operators
- Conditional Statements
- Loop
- Arrays
- Object Oriented Programming
- Classes and Objects
- Access Modifiers
- Methods
- Abstraction
- Constructor
- Packages
- StringBuffer
- Inheritance
- StringBuilder
- Encapsulation
- Java IO
- Polymorphism
- Interface
- Types Of Variables
- Static Keyword
- Final Keyword
- Super Keyword
- This Keyword
- Exception Handling