Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java Polymorphism

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

JAVA POLYMORPHISM

INTRODUCTION:
• “Poly” means many and “Morphs” means forms, So it means many
forms.
• In simple words, we can define polymorphism as the ability of a
message to be displayed in more than one form. 
•  Perform a single action in different ways. 
• Example: A person at the same time can have different characteristics.
Like a man at the same time is a father, a husband, an employee. So
the same person possesses different behavior in different situations.
Types of polymorphism
• In Java polymorphism is mainly divided into two types: 
• Compile-time Polymorphism
• Runtime Polymorphism
Compile-time Polymorphism
• Static polymorphism. Function Overloading or Operator overloading. 

Method Overloading: When there are multiple functions


with the same name but different parameters then these
functions are said to be overloaded. Functions can be
overloaded by change in the number of arguments or/and
a change in the type of arguments.
Example 01 : Method overloading By using Different Types of Arguments
// Class 1
// Helper class
class Helper { // Class 2
// Main class
  class GFG {
    // Method with 2 integer parameters  
    static int Multiply(int a, int b)
    {
    // Main driver method
    public static void main(String[] args)
      {
        // Returns product of integer numbers  
        return a * b;         // Calling method by passing
    }
        // input as in arguments
          System.out.println(Helper.Multiply(2, 4));
    // Method 2         System.out.println(Helper.Multiply(5.5, 6.3));
    // With same name but with 2 double parameters     }
    static double Multiply(double a, double b) }
    {
 
        // Returns product of double numbers
        return a * b;
    } Output: 8 34.65
}
 
Example 02: Method Overloading by Using Different Numbers of
Argument
// Class 1
// Helper class // Class 2
class Helper { // Main class
    // Method 1 class GFG {
    // Multiplication of 2 numbers
    static int Multiply(int a, int b)  
    {     // Main driver method
         // Return product
    public static void main(String[] args)
    {
        return a * b;
    }          // Calling method by passing
    // Method 2         // input as in arguments
    // // Multiplication of 3 numbers         System.out.println(Helper.Multiply(2, 4));
    static int Multiply(int a, int b, int c)         System.out.println(Helper.Multiply(2, 7, 3));
    {     }
        // Return product }
        return a * b * c;
    }
}
Output: 8 42
Type 02: Runtime polymorphism
• It is also known as Dynamic Method Dispatch.
• It is a process in which a function call to the overridden method is
resolved at Runtime. This type of polymorphism is achieved by
Method Overriding.
•  Method overriding, on the other hand, occurs when a derived class
has a definition for one of the member functions of the base class. That
base function is said to be overridden.
// Class 1
// Class 3
// Helper class // Class 2 // Helper class
class Parent { // Helper class class subclass2 extends Parent {
  class subclass1 extends Parent {
 
 
    // Method of parent class     // Method
    // Method     void Print()
    void Print()
    void Print()     {
    {
{ System.out.println("subclass1"); }  
  }         // Print statement
        // Print statement         System.out.println("subclass2");
        System.out.println("parent class");     }
    } }
}
// Class 4
   // Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
Output:     {
 
subclass1         // Creating object of class 1
        Parent a;
subclass2   Try Out the
        // Now we will be calling print methods
        // inside main() method Reverse !!!
 
        a = new subclass1();
        a.Print();
 
        a = new subclass2();
        a.Print();
    }
}
Is the below code successfully overridden?
package overridingPrograms;
public class A
{
private void m1()
{
System.out.println("m1-A");
}
}
public class B extends A
{
private void m1()
{
System.out.println("m1-B");
}
public static void main(String[] args)
{
B b = new B();
b.m1();
}
}
Exercise
• Write a java program to calculate the area of Circle (Input: Integer),
Triangle( Input: Float) and rectangle (Input: Int, Int) using Compile
time polymorphism.
• To write a Java program that implements run-time polymorphism for
product information. Calculate the total sales price for each
product( Pendrive & RAM ). The sales price should be calculated
differently depending on the product type. Overriding method of
object instance of the sub-class should be invoked.
Use of Super
• Used to refer immediate parent class object.
• Whenever you create the instance of subclass, an instance of parent class
is created implicitly which is referred by super reference variable.
• Usage of Java super Keyword
1.Super can be used to refer immediate parent class instance variable.
2.Super can be used to invoke immediate parent class method.
3.Super() can be used to invoke immediate parent class constructor.
1. Super is used to class Animal{  
refer immediate String color="white";  
parent class instance }  
variable - super class Dog extends Animal{  
keyword to access the String color="black";  
data member or field void printColor(){  
of parent class. It is System.out.println(color);//prints color of Dog class  
used if parent class System.out.println(super.color);//
and child class have prints color of Animal class  
}  
same fields.
}  
class TestSuper1{  
public static void main(String args[]){  
Dog d=new Dog();  
d.printColor();  
}} 
2. Super can be used to Class Animal{  
invoke parent class method - void eat(){ System.out.println("eating...");}  
used to invoke parent class }  
method. It should be used if class Dog extends Animal{  
subclass contains the same void eat()
method as parent class or {System.out.println("eating bread...");}  
used if method is void bark(){System.out.println("barking...");}  
overridden void work(){  
super.eat();  
bark();  
Output: }}  
eating... class TestSuper2{  
public static void main(String args[]){  
barking...
Dog d=new Dog();  
d.work();  
}}  
class Animal{  
Animal()
• Super is used to invoke {System.out.println("animal is created");}  
parent class constructor }  
class Dog extends Animal{  
Dog(){  
super();  
System.out.println("dog is created");  
Output: }  
animal is created }  
dog is created class TestSuper3{  
public static void main(String args[]){  
Dog d=new Dog();  
}}  

You might also like