Method Overriding

Method Overriding: Methods of parent class can be overridden by methods of child class. Method Overriding is methods with same name and same signature of parent class is overridden by child class (two or more classes should have IS-A relationship).

Example 1: Create a super-class A having two methods print() and show() and one sub-classes classes A has same print()?
A.java
package com.java.overriding;

public class A {
    public void print() {
        System.out.println("print() method of A class");
    }
    public void show() {
        System.out.println("show() method of A class");
    }
}

B.java
package com.java.overriding;

public class B extends A {
    public void print() {
        //super.print();        //print() method of A class
        System.out.println("print() method of B class");
    }
    public static void main(String[] args) {
        B b = new B();
        b.print();
        b.show();
        
        System.out.println("--------------------");
        A ref = new B();
        ref.print();
        ref.show();
        
        System.out.println("--------------------");
        A a = new A();
        a.print();
        a.show();
    }
}

Output:
print() method of B class
show() method of A class
--------------------------------
print() method of B class
show() method of A class
--------------------------------
print() method of A class
show() method of A class

Example 2: Create a super-class Shape having one method area() and 3 sub-classes classes Triangle, Rectangle and Square having area() such as area() methods of sub-classes are Overridden by area() method super class Shape?
Overriding.java
package com.java.overriding;

class Shape {
     public double area() {
        System.out.print("Area of Shape class: ");
        return 1.0;
    }
}

class Triangle extends Shape {
    public double base;
    public double height;

    public Triangle(double b, double h) {
        base = b;
        height = h;
    }
    @Override
    public double area() {
        return (base * height) / 2;
    }
}

class Rectangle extends Shape {
    public double length;
    public double breadth;

    public Rectangle(double l, double b) {
        length = l;
        breadth = b;
    }

    public double area() {
        return length * breadth;
    }
}

class Square extends Shape {
    public double side;

    public Square(double s) {
        side = s;
    }

    public double area() {
        return side * side;
    }
}

public class Overriding {
    public static void main(String[] args) {

        Shape shpObj = new Shape();
        System.out.println(shpObj.area());
        System.out.println("---------------------------------------------------------");
        
        Triangle triObj = new Triangle(5.0, 6.0);
        Rectangle recObj = new Rectangle(5.0, 6.0);
        Square sqrObj = new Square(5.0);

        System.out.println("Area of Triangle of base: " + triObj.base + " and Height: " + triObj.height + " is " + triObj.area());
        System.out.println("Area of Rectangle of length: " + recObj.length + " and Breadth: " + recObj.breadth + " is " + recObj.area());
        System.out.println("Area of Square of Side " + sqrObj.side + " is " + sqrObj.area());

        System.out.println("---------------------------------------------------------")
        Shape shape = new Triangle(10.0, 20.0);
        System.out.println("Area of Triangle is " + shape.area());
        
        shape = new Rectangle(5.0, 25.0);
        System.out.println("Area of Rectangle of is " + shape.area());
        
        shape = new Square(9.0);
        System.out.println("Area of Square is " + shape.area());
    }
}

Output:
Area of Shape class: 1.0
---------------------------------------------------------
Area of Triangle of base: 5.0 and Height: 6.0 is 15.0
Area of Rectangle of length: 5.0 and Breadth: 6.0 is 30.0
Area of Square of Side 5.0 is 25.0
---------------------------------------------------------
Area of Triangle is 100.0
Area of Rectangle of is 125.0
Area of Square is 81.0

Note:
  • final methods cannot be overridden
  • static methods cannot be overridden
  • private methods cannot be overridden
  • methods with covariant return type is overridden

What is difference between Method Overloading and Method Overriding?
Method Overloading Method Overriding
It is used to increase the readability of the program It is used to provide the specific implementation of the method that is already provided by its super class
It occurs in same class It occurs in two or more classes that have IS-A relationship
Method signature must be different Method signature must be same
It is also called compile time polymorphism It is also called run time polymorphism
return type can be same or different. It does not matter in method overloading return type must be same or covariant

No comments:

Post a Comment