Don't let the Lockdown slow you Down - Enroll Now and Get 3 Course at 24,999/- Only. Explore Now!

General

Method Overriding in Java

Method Overriding in Java

Method Overriding in Java

  • If a derived class method has the same signature as that of the parent class method that means two methods are considered having same signature (when their return type , method name, Number and type of arguments are same) then that concept is called Method overriding.
  • Parent class method is called overriding method.
  • Child class method is called overridden method.

Example:

package Mypkg;
public class MethOver {
            /**
             * @param args
             */
            public static void main(String[] args) {
                        // TODO Auto-generated method stub
                        System.out.println("Entered MethOver-Main ()");
                        System.out.println("****************************");
                        mymeth2 obj=new mymeth2();
                        obj.calculation(100, 200);
                        System.out.println("****************************");
                        System.out.println("Exited  MethOver-Main ()");
            }
}
class mymeth1
{
            int a,b;// variables
            void calculation(int a, int b)// method at Base class
            {
            System.out.println("Entered mymeth1(Baseclass)-calculation(a,b) ");
            System.out.println("Area of rectangle");
            this.a=a;
            this.b=b;
            System.out.println("ARea is" + this.a * this.b);
            System.out.println("Exited mymeth1(Baseclass)-calculation(a,b) ");
            }
            }
class mymeth2 extends mymeth1  // sub class
{
            int c,d;
            void calculation(int c, int d)  // Method at derrived class
            {
            System.out.println("Entered mymeth2(derrived)-calculation(c,d) ");
            System.out.println("Area of triangle");
            this.c=c;
            this.d=d;
            System.out.println("ARea is" + (this.c * this.d)/2);
            System.out.println("Exited mymeth2(derrived)-calculation(c,d) ");
           }        

}

Output:

Entered MethOver-Main ()

****************************

Entered mymeth2(derrived)-calculation(c,d)

Area of triangle

ARea is10000

Exited mymeth2(derrived)-calculation(c,d)

****************************

Exited  MethOver-Main ()

Click Here-> Get Java Training with Real-time Projects

Scroll Up
Besant Technologies WhatsApp