Programs on Polymorphism

1. Constructor Overloading practice program-1

This program demonstrates constructor overloading by creating objects with different constructors. It initializes student details using both the default and parameterized constructors.

class Student {

    int id;
    String name;

    Student() {
        id = 101;
        name = "Rahul";
    }

    Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    void display() {
        System.out.println("Student ID : " + id);
        System.out.println("Student Name : " + name);
    }
     public static void main(String[] args) {

        Student s1 = new Student();
        Student s2 = new Student(102, "Priya");

        System.out.println("Default Constructor:");
        s1.display();

        System.out.println("\nParameterized Constructor:");
        s2.display();
    }
}

Output:
Default Constructor:
Student ID : 101
Student Name : Rahul

Parameterized Constructor:
Student ID : 102
Student Name : PriyaCode language: PHP (php)

2. Constructor Overloading practice program-2

This program calculates the area of a square and a rectangle using overloaded constructors. Different constructors initialize the required dimensions.

class Area {

    Area(int side) {
        System.out.println("Area of Square = " + (side * side));
    }

    Area(int length, int breadth) {
        System.out.println("Area of Rectangle = " + (length * breadth));
    }
     public static void main(String[] args) {

        Area square = new Area(5);
        Area rectangle = new Area(8, 4);
    }
}
Output:
Area of Square = 25
Area of Rectangle = 32

3. Method Overloading practice program-1

This program demonstrates method overloading by adding different numbers of arguments. The compiler selects the appropriate method based on the method parameters.

class Addition {

    void add(int a, int b) {
        System.out.println("Sum = " + (a + b));
    }

    void add(int a, int b, int c) {
        System.out.println("Sum = " + (a + b + c));
    }

    void add(double a, double b) {
        System.out.println("Sum = " + (a + b));
    }

    public static void main(String[] args) {

        Addition obj = new Addition();

        obj.add(10, 20);
        obj.add(10, 20, 30);
        obj.add(12.5, 7.5);
    }
}
Output:
Sum = 30
Sum = 60
Sum = 20.0

4. Method Overloading practice program-2

This program finds the maximum value using overloaded methods. The appropriate method is selected based on the number of arguments passed.

class Maximum {

    int max(int a, int b) {

        if (a > b)
            return a;
        else
            return b;
    }

    int max(int a, int b, int c) {

        if (a > b && a > c)
            return a;
        else if (b > c)
            return b;
        else
            return c;
    }

    public static void main(String[] args) {

        Maximum obj = new Maximum();

        System.out.println("Maximum of Two Numbers = " + obj.max(25, 40));

        System.out.println("Maximum of Three Numbers = " + obj.max(25, 40, 35));
    }
}
Output:
Maximum of Two Numbers = 40
Maximum of Three Numbers = 40

Scroll to Top