We Offer 100% Job Guarantee Courses (Any Degree / Diploma Candidates / Year GAP / Non-IT / Any Passed Outs). Placement Records
Hire Talent (HR):+91-9707 240 250

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

Besant Technologies WhatsApp