Skip to main content

Method Overriding

  • Method overriding is a process of implementing superclass methods in subclass with the same signature.

Lab493.java

class Hello {

void m1() {
System.out.println("Hello -> m1()");
}

void m2() {
System.out.println("Hello -> m2()");
}
}

class Hai extends Hello {

void m2() {
System.out.println("Hai -> m2()");
}

void m3() {
System.out.println("Hai -> m3()");
}
}

class Lab493 {

public static void main(String[] args) {
Hai hai = new Hai();

hai.m1();

hai.m3();

hai.m2();
}
}

Explanation of Above Program

  • Hai class has implemented m2() method of Hello class with the same signature.
  • So, m2() method of Hello class is overridden in Hai class.
  • m1() method of Hello class is not implemented in Hai class, therefore it is inherited to Hai.
  • m3() method is the newly added method in Hai class.

Method Overriding Flow

3.8.1 Why to Override the Method?

  • According to Object-Oriented best practices:

"Classes are closed for modifications"

i.e., if you want to modify the functionality of an existing class, then you should not disturb the existing class.


  • It is always better to write a subclass and provide the required implementations in subclass.

Subclasses can be written for following purposes:

  • To add new functionality.
  • To modify existing functionality.
  • To inherit existing functionality.

3.8.2 Rules to Override the Method

1.

Subclass method name must be same as superclass method name.


2.

Subclass method parameters:

  • type
  • order
  • number

must be same as superclass method parameters.


3.

Subclass method return type must be same as superclass method return type.

Note

If the superclass method return type is a class type, then while overriding the method in subclass, you can use:

  • same class type
  • or its subclass as return type

(Available from Java 5)


Overriding vs Inheritance

FeatureInherited MethodOverridden Method
Method available in subclass
Same implementation as superclass
New implementation in subclass
Requires same signature

Important Point

Method overriding works only for methods, not for variables.

class Hello {

Am1() {
...
}
}

class Hai extends Hello {

Am1() { ... }// Valid

// OR

Bm1() { ... }// Valid from Java 5

Cm1() { ... }// Invalid
}
class A { }

class B extends A { }

class C { }

4)

4) Subclass Method Access Modifier

Subclass method access modifier must be same or higher than superclass method access modifier.

Super ClassSub Class
publicpublic
protectedprotected, public
defaultdefault, protected, public
privateprivate, default, protected, public

5) Instance Method Rule

When superclass method is an instance method, then you have to override it in subclass as an instance method only.


6) Static Method Rule

When superclass method is a static method, then you have to override it in subclass as a static method only.


7) Checked Exception Rules in Overriding

When superclass method throws a method-level checked exception, then subclass method can do the following:

a)

Subclass method can ignore that method-level exception.

b)

Subclass method can throw the same exception.

c)

Subclass method can throw the exception which is subclass to superclass method exception.

d)

Subclass method cannot throw the exception which is superclass to superclass method exception.

e)

Subclass method cannot throw the exception which is non-subclass to superclass method exception.

f)

Subclass method can throw any unchecked exception.


8) Unchecked Exception Rules in Overriding

When superclass method throws a method-level unchecked exception, then subclass method can do the following:

a)

Subclass method can ignore that method-level exception.

b)

Subclass method can throw the same exception.

c)

Subclass method can throw any other unchecked exception.

d)

Subclass method cannot throw any checked exception.


Note

Rule No. 7 and Rule No. 8 will be fully explored with Exception Handling.

Lab494.java

class Lab494 {

public static void main(String args[]) {
B bobj = new B();

bobj.show();
bobj.SHOW();
}
}

class A {

void show() {
System.out.println("A -> show()");
}
}

class B extends A {

void SHOW() {
System.out.println("B -> SHOW()");
}
}

Lab495.java

class Lab495 {

public static void main(String args[]) {
B bobj = new B();

bobj.show(99);
bobj.show("JavaWorld");
}
}

class A {

void show(int ab) {
System.out.println("A -> show(int)");
}
}

class B extends A {

void show(String ab) {
System.out.println("B -> show(String)");
}
}

Lab496.java

class Lab496 {

public static void main(String args[]) {
B bobj = new B();

bobj.show();
}
}

class A {

long show() {
return 0;
}
}

class B extends A {

int show() {
return 0;
}
}

Lab497.java

class Lab497 {

public static void main(String args[]) {
Hai hai = new Hai();

hai.m1();
}
}

class Hello {

Am1() {
return new A();
}
}

class Hai extends Hello {

Bm1() {
return new B();
}
}

class A {}

class B {}

Lab498.java

class Lab498 {

public static void main(String args[]) {
Hai hai = new Hai();

hai.m1();
}
}

class Hello {

Am1() {
return new A();
}
}

class Hai extends Hello {

Am1() {
return new A();
}
}

class A {}

class B extends A {}

Lab499.java

class Lab499 {

public static void main(String args[]) {
Hai hai = new Hai();

hai.m1();
}
}

class Hello {

Am1() {
return new A();
}
}

class Hai extends Hello {

Bm1() {
return new B();
}
}

class A {}

class B extends A {}

Lab500.java

class Lab500 {

public static void main(String args[]) {
B bobj = new B();

bobj.m1();
}
}

class A {

final void m1() {}
}

class B extends A {

void m1() {}
}

Lab501.java

class Lab501 {

public static void main(String args[]) {
B bobj = new B();

bobj.m1();
}
}

class A {

void m1() {}
}

class B extends A {

final void m1() {}
}

Lab502.java

class Lab502 {

public static void main(String args[]) {
B bobj = new B();

bobj.m1();
}
}

class A {

void m1() {}
}

class B extends A {

static void m1() {}
}

Lab503.java

class Lab503 {

public static void main(String args[]) {
new B().m1();
}
}

class A {

static void m1() {}
}

class B extends A {

void m1() {}
}

Lab504.java

class Lab504 {

public static void main(String args[]) {
B.m1();
}
}

class A {

void m1() {}

static void m2() {}
}

class B extends A {

void m1() {}

static void m2() {}
}

Lab505.java

class Lab505 {

public static void main(String args[]) {
new B().m1();
}
}

class A {

native void m1();

void m2() {}
}

class B extends A {

void m1() {}

native void m2();
}

Lab506.java

class Lab506 {

public static void main(String args[]) {
new B().m1();
}
}

class A {

private void m1() {}
}

class B extends A {

int m1() {
return 0;
}
}

Lab507.java

class Lab507 {

public static void main(String args[]) {
new B().m1();
}
}

class A {

void m1() {}
}

class B extends A {

private void m1() {}
}

Lab508.java

class Lab508 {

public static void main(String args[]) {
new B().m1();
}
}

class A {

void m1() {}

void m2() {}

void m3() {}
}

class B extends A {

void m1() {}

protected void m2() {}

public void m3() {}
}

Lab509.java

class Lab509 {

public static void main(String args[]) {
new B().m1();
}
}

class A {

protected void m1() {}

protected void m2() {}
}

class B extends A {

private void m1() {}

void m2() {}
}

Lab510.java

class Lab510 {

public static void main(String args[]) {
new B().m1();
}
}

class A {

protected void m1() {}

protected void m2() {}
}

class B extends A {

protected void m1() {}

public void m2() {}
}

Lab511.java

class Lab511 {

public static void main(String args[]) {
new B().m1();
}
}

class A {

public void m1() {}
}

class B extends A {

protected void m1() {}
}

Lab512.java

class Lab512 {

public static void main(String args[]) {
new B().m1();
}
}

class A {

public void m1() {}
}

class B extends A {

public void m1() {}
}

Note

When the method is overridden in the subclass and if you are using subclass object to invoke the method, then implementation from subclass will be invoked.


Lab513.java

class Lab513 {

public static void main(String args[]) {
B bobj = new B();

bobj.m1();
}
}

class A {

void m1() {
System.out.println("A -> m1()");
}
}

class B extends A {

void m1() {
System.out.println("B -> m1()");
}
}

Lab514.java

class Lab514 {

public static void main(String args[]) {
B bobj = new B();

bobj.m1();
}
}

class A {

void m1() {
System.out.println("A -> m1()");
}
}

class B extends A {

void m1() {
System.out.println("B -> m1()");

super.m1();
}
}

Lab515.java

class Lab515 {

public static void main(String args[]) {
B bobj = new B();

bobj.m1();
}
}

class A {

static void m1() {
System.out.println("A -> m1()");
}
}

class B extends A {

static void m1() {
System.out.println("B -> m1()");
}
}

Hello.java

packagecom.javaworld.p1;

public class Hello {

protected void display() {

}
}

Lab516.java

packagecom.javaworld.p2;

import com.javaworld.p1.Hello;

class Xyz extends Hello { }

class Hai extends Hello {

void show() {

Xyzre f=new Xyz();

ref.display();
}
}

class Lab516 {

public static void main(String[] args) {

Hai hai=new Hai();

hai.show();
}
}

Lab517.java

packagecom.javaworld.p2;

import com.javaworld.p1.Hello;

class Xyz extends Hello {

protected void display() {

}
}

class Hai extends Hello {

void show() {

Xyzre f=new Xyz();

ref.display();
}
}

class Lab517 {

public static void main(String[] args) {

Hai hai=new Hai();

hai.show();
}
}