Polymorphism in java

Polymorphism:

  • Ploy means many and prism means form so polymorphism means many form.
  • It is used to perform a single action in different ways.
  • For example,
  • Action is draw and it performance may be draw a line or draw circle.

Types of polymorphism:

1. Runtime polymorphism

2. Compile time polymorphism

Runtime polymorphism:

  • The runtime polymorphism is also called as dynamic polymorphism.
  • It is best example is method overloading.

Example program:

  1. import java.util.Scanner;

    class Shape {

    Scanner sc = new Scanner(System.in);

    double a;

        void area() {

            System.out.println("\tCalculating area of Rectangle,Circle,Triangle .........");

        }

    }

     

    class Rectangle extends Shape {

        void area() {

            float l,w;

            System.out.println("Enter length and width of rectangle : ");

            l=sc.nextFloat();

            w=sc.nextFloat();

            a=l*w; //area=length*width;

            System.out.println("Area of rectangle : "+a);

        }

    }

     

    class Circle extends Shape {

        void area() {

            float r;

            System.out.println("Enter radius  of circle : ");

            r=sc.nextFloat();

            a=3.14*r*r; //area=3.14*radius*radius;

            System.out.println("Area of circle : "+a);

        }

    }

     

    class Triangle extends Shape {

        void area() {

            double b,h;

            System.out.println("Enter base and length of triangle : ");

            b=sc.nextDouble();

            h=sc.nextDouble();

            a=(b*h)/2; //area=1/2*Base*length;

            System.out.println("Area of triangle : "+a);

        }

    }

     

    class Runtime_Polymerpism {

        public static void main(String args[]) {

            Shape s=new Shape();

            s.area();

            s = new Rectangle();

            s.area();

            s = new Circle();

            s.area();

            s = new Triangle();

            s.area();

        }

    }

Output:

Compile time polymorphism:

  • The runtime polymorphism is also called as static polymorphism.
  • It is best example is method overriding.

Example program:

  1. public class Complietime_polymorpism {

        float l, w, r;

        double a,b, h;

        void area() {

            System.out.println("\tCalculating area of Rectangle,Circle,Triangle .........");

        }

     

        void area(float l, float w) {

            a = l * w; //area=length*width;

            System.out.println("Area of rectangle : " + a);

        }

     

        void area(float r) {

            a = 3.14 * r * r; //area=3.14*radius*radius;

            System.out.println("Area of circle : " + a);

        }

     

        void area(double b, double h) {

            a = (b * h) / 2; //area=1/2*Base*length;

            System.out.println("Area of triangle : " + a);

        }

    }

     

    class Passing_value_through_obj {

     

        public static void main(String args[]) {

            Complietime_polymorpism cp = new Complietime_polymorpism();

            cp.area();

            cp.area(1, 2);

            cp.area(2);

            cp.area(8, 5);

     

        }

    }

Output:

Note:

Share:

Method overloading and overriding in java

Overriding:

  • Method overriding is a mechanism in which a subclass inherits the methods of super class and sometimes the subclass modifies the implementation of a method defined in super class.
  • That means if subclass (Derived class) has the same method as declared in the parent class (base class).
  • Method overriding is used for runtime polymorphism.

Rules for Java Method Overriding:

  • The method must have the same name as in the parent class
  • The method must have the same parameter as in the parent class.
  • There must be inherited.
  • A static method cannot be overridden. It can be proved by runtime polymorphism.

Syntax:

  1. class base_class_name

    {

    return_type method_name(parameter_list)

    {

              //Statement

    }

    }

     

    class derived_class_name

    {

    base_class_method(parameter_list)

    {

              //Statement

    }

    }

Example program:

  1. class Bank {

        int get_Rate_Of_Interest() {

            return 0;

        }

    }

    class SBI extends Bank {

        int get_Rate_Of_Interest() {

            return 8;

        }

    }

    class ICICI extends Bank {

        int get_Rate_Of_Interest() {

            return 7;

        }

    }

    class AXIS extends Bank {

        int get_Rate_Of_Interest() {

            return 9;

        }

    }

    class Method_Overriding {

        public static void main(String args[]) {

            SBI s = new SBI();

            ICICI i = new ICICI();

            AXIS a = new AXIS();

            System.out.println("SBI Rate of Interest: " + s.get_Rate_Of_Interest());

            System.out.println("ICICI Rate of Interest: " + i.get_Rate_Of_Interest());

            System.out.println("AXIS Rate of Interest: " + a.get_Rate_Of_Interest());

        }

    }

Output:

Method overloading:

  • Overloading is a mechanism in which we can use many methods having the same function name but can pass different number of parameters or different types of parameters.

Syntax:

  1. class base_class_name

    {

    return_type method_name(parameter_list)

    {

              //Statement

    }

    return_type same_method_name(parameter_list)

    {

              //Statement

    }

    }

Example program:

  1. class SimpleCalculator

    {

        int add(int a, int b)

        {

             return a+b;

        }

        int  add(int a, int b, int c) 

        {

             return a+b+c;

        }

    }

    class Method_Overloading {

       public static void main(String args[])

       {

           SimpleCalculator obj = new SimpleCalculator();

           System.out.println("Adding two number : "+obj.add(10, 20));

           System.out.println("Adding three number : "+obj.add(10, 20, 30));

       }

    }

Output:
Share:

Constructor overloading in java

Constructor overloading:

  • If one class has more than one constructor different parameters list called Constructor overloading.

Example program:

  1. class StudentData

    {

       private int stuID;

       private String stuName;

       private int stuAge;

       StudentData()

       {

           //Default constructor

           stuID = 1001;

           stuName = "Sabrina";

           stuAge = 15;

       }

       StudentData(int num1, String str, int num2)

       {

           //Parameterized constructor

           stuID = num1;

           stuName = str;

           stuAge = num2;

       }

     

       public int getStuID() {

           return stuID;

       }

       public void setStuID(int stuID) {

           this.stuID = stuID;

       }

       public String getStuName() {

           return stuName;

       }

       public void setStuName(String stuName) {

           this.stuName = stuName;

       }

       public int getStuAge() {

           return stuAge;

       }

       public void setStuAge(int stuAge) {

           this.stuAge = stuAge;

       }

     

       public static void main(String args[])

       {

           //This object creation would call the default constructor

           StudentData obj = new StudentData();

           System.out.println("Values from Default constructor.....");

           System.out.println("Student Name is: "+obj.getStuName());

           System.out.println("Student Age is: "+obj.getStuAge());

           System.out.println("Student ID is: "+obj.getStuID());

     

           System.out.println("\nValues from Parameterized constructor.....");

           StudentData obj2 = new StudentData(2005, "Kurshitha", 18);

           System.out.println("Student Name is: "+obj2.getStuName());

           System.out.println("Student Age is: "+obj2.getStuAge());

           System.out.println("Student ID is: "+obj2.getStuID());

      }

    }

Output:

Note:

If you need details about this keyword then click the below link to detail learning.

https://www.whereisstuff.com/2020/11/use-of-this-keyword-in-java.html

Share:

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me