METHOD OVERRIDING IN JAVA -->>>

 what is method overriding in java?

we know that the subclass extending the parent class has access to all the non-private data members and methods of its parent class.

most of the time the purpose of inheriting properties from the parent class and adding new methods is to extend the behavior of the parent class.

sometimes, it is required to modify the behavior of the parent class for this we use method overriding.

*program to show how the member function of the parent class is overridden in a subclass. 


/* method overriding */

class A

 {

    double sidea;

    double sideb;


    A(double a, double b) 

{

        sidea = a;

        sideb = b;

    }


    A(double a)

 {

        sidea = a;

        sideb = a;

    }


    double area()

 {

        System.out.println("Area inside figure is undefined");

        return 0;

    }

}


class B extends A

 {

    B(double a, double b) {

        super(a, b);

    }


    double area()

 {

        System.out.println("The area of rectangle:");

        return sidea * sideb;

    }

}


class C extends A 

{

    C(double a) {

        super(a);

    }


    double area() 

{

        System.out.println("area of squre:");

        return sidea * sidea;

    }

}


public class Override

 {

    public static void main(String[] args)

 {

       A f = new A(20.9, 67.9);

        B r = new B(34.2, 56.3);

       C s = new C(23.1);

        System.out.println("************* welcom to overrid demo***********");

        f.area();

         f=r;

        System.out.println("  " + r.area());

        f=s;

        System.out.println("  " + s.area());


    }

}


Comments