Method Overriding in java

Whenever a method of Parent class is redefined with the same signature inside its child class . Then it is called Method overriding.When a method in a child class has the same name and its type signature as the method in Parent class . Then it is a Method Overriding and we can say that method in child class overrides method in parent class.

Parent class method whose body is redefined in child class is known as Overridden Method .

  Method overriding is a means of implementing Run time polymorphism in Java

  Method overriding is used for providing specific implementation to child if child is not satisfied by the parent implementation.


Rules of Overriding in Java

  In method overriding the method names and arguments must be same i.e signature must be same.

  In method overriding the return type must be same but this is applicable untill java version 1.4 from the 1.5 onwords Co-Varient return types are allowd. According to co-varient type , child class method return type need to same as parent class method return type or it should be a child class of parent method return type .

  We can not override parent class final method but we can use .

  We can not override parent class private method because it is not visible to child class.

  We can not override static methods because static methods are associated with class ,not with object so they are resolved at class loading time only.

  We can increase the accessibility of child class method but we can not reduce the accessibility of child class method. for example if method in parent class is public then we must declare child class method as public. we can not use other access modifiers.

  If child class method thows some checked exception then compulsory parent class method should thows the same exception or its parent type exception. otherwise we will get error.

Example of Method Overriding

Suppose there are two class A and B . A is a Parent of B.

// example of method overriding
class A {
	int i, j;

	A(int a, int b) {
		i = a;
		j = b;
	}

	// print i and j
	void display() {
		System.out.println("value of i =" + i);
		System.out.println("value of j =" + j);
	}

}

class B extends A {
	int p;

	B(int a, int b, int c) {
		super(a, b);
		p = c;
	}

	// print p
	// this method overrides display() of Parent class(A)
	void display() {
		System.out.println("value of p= " + p);
	}
}

public class Example {
	public static void main(String[] args) {
		B b = new B(2, 4, 5);
		b.display();// this class display() of B
	}
}
value of p= 5

Whenever display() method is called on object type B then display() of class B is executed.

If we want to access the parent class overridden (display() in this case) method . then we have to use super keyword to call overridden method .

Let us see the example of calling parent class overridden method in child class.

Example : using super

// example of method overriding
class A {
	int i, j;

	A(int a, int b) {
		i = a;
		j = b;
	}

	// print i and j
	void display() {
		System.out.println("value of i =" + i);
		System.out.println("value of j =" + j);
	}

}

class B extends A {
	int p;

	B(int a, int b, int c) {
		super(a, b);
		p = c;
	}

	// print p
	// this method overrides display() of Parent class(A)
	void display() {
		super.display();// this calls A's display
		System.out.println("value of p= " + p);
	}
}

public class Example {
	public static void main(String[] args) {
		B b = new B(2, 4, 5);
		b.display();// this class display() of B
	}
}
value of i =24
value of j =44
value of p= 50

Here, super.display( ) calls the parent class display( ).

Method overriding occurs only when the names and the type signatures of the two methods are identical. If they are not, then the two methods are simply overloaded. For example, consider this modified version of the preceding example:


class A {
	int i, j;

	A(int a, int b) {
		i = a;
		j = b;
	}

	// print i and j
	void display() {
		System.out.println("value of i =" + i);
		System.out.println("value of j =" + j);
	}

}

class B extends A {
	int p;

	B(int a, int b, int c) {
		super(a, b);
		p = c;
	}

	// print p
	//overloading ....
	void display(int a) {
		System.out.println("value of p= " + p);
	}
}

public class Example {
	public static void main(String[] args) {
		B b = new B(240, 44, 50);
		b.display();// this class display() of A
		b.display(12);// this class display() of B
	}
}
value of i =240
value of j =44
value of p= 50

The version of display( ) in B takes a int parameter. This makes its type signature different from the one in A, which takes no parameters. Therefore, no overriding (or name hiding) takes place. Instead, the version of display( ) in B simply overloads the version of display( ) in A.

Why Method Overriding?

Importance of Overriding

  Overriding method allows Java to support Runtime polymorphism.

  Runtime polymorphism allows child classes to define the specific implementation of its parent method by overriding .

  Method Overriding leads to code reusebility and robustness.