0% found this document useful (0 votes)
18 views

Overriding

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Overriding

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

RunTime PolyMorphism./late Binding/Dynamic Polymorphism/Overriding.

Whenever parent class and child class having same method name and same parameters
then we can call it as method overriding.

Method Overriding:

Definition: Method overriding occurs when a subclass provides a specific


implementation for a method that is already provided by its superclass.
The method in the subclass has the same signature (name, return type, and
parameters) as the method in the superclass.
Key Points:
The methods in the superclass and subclass must have the same name, return type,
and parameter list.

Overriding is determined at runtime (dynamic polymorphism).

Example :

package methodOverriding;

public class Parent {

public void m1() {


System.out.println("m1 method of Parent class");
}
}

package methodOverriding;

public class Child extends Parent {

public void m2() {


System.out.println("m2 method of Child class");
}

public static void main(String[] args) {

Parent p = new Parent();


p.m1();
// p.m2();

Child c = new Child();


c.m1();
c.m2();

Parent p1 = new Child();


p1.m1();
// p1.m2();

// Child c1=new Parent();

}
}

}
Example :

package methodOverriding;

public class Parent {

public void m1() {


System.out.println("m1 method of Parent class");
}

package methodOverriding;

public class Child extends Parent {

public void m1() {


System.out.println("m1 method of Child class");
}

public static void main(String[] args) {

Parent p = new Parent();


p.m1();

Child c = new Child();


c.m1();

Parent p1 = new Child();


p1.m1();

// Child c1=new Parent();

}
}

Example : retrun must be same in method overriding

package methodOverriding;

public class Parent {

public int m1() {


System.out.println("m1 method of Parent class");
return 0;
}

package methodOverriding;
public class Child extends Parent {

public void m1() {


System.out.println("m1 method of Child class");
}

public static void main(String[] args) {

Parent p = new Parent();


p.m1();

Child c = new Child();


c.m1();

Parent p1 = new Child();


p1.m1();

// Child c1=new Parent();

}
}

Example :

package methodOverriding;

public class Parent {

public void m1(int a) {


System.out.println("m1 method of Parent class");
}

package methodOverriding;

public class Child extends Parent {

public void m1() {


System.out.println("m1 method of Child class");
}

public static void main(String[] args) {

Parent p = new Parent();


p.m1(12);

Child c = new Child();


c.m1();

Parent p1 = new Child();


p1.m1(12);

// Child c1=new Parent();

}
}

You might also like