Quantcast
Channel: Tech Tutorials
Viewing all articles
Browse latest Browse all 938

Method Overriding in Java

$
0
0

In a parent-child relation between classes, if a method in subclass has the same signature as the parent class method then the method is said to be overridden by the subclass and this process is called method overriding in Java.

Example of Method overriding in Java


class Parent {
private int i;
// Constructor
Parent(int i){
this.i = i;
}
// Method with no param
public void dispayData(){
System.out.println("Value of i " + i);
}
}

class Child extends Parent{
private int j;
// Constructor
Child(int i, int j){
// Calling parent class constructor
super(i);
this.j = j;
}
// Overridden Method
public void dispayData(){
System.out.println("Value of j " + j);
}
}

public class OverrideDemo {

/**
* @param args
*/
public static void main(String[] args) {
Child child = new Child(5, 7);
child.dispayData();
}

}

Output of the program

Value of j 7

It can be noticed here that when displayData() is invoked on an object of class Child, the overridden displayData() method of the child class is called.

Please note that in the case of parent-child relationship if both classes have the method with same name and signature then only it is considered as method overriding, otherwise it is method overloading only.

Benefit of method overriding in Java

Method overriding allows java to support run-time polymorphism which in turn helps in writing more robust code and code reuse.

Method overriding also helps in hierarchical ordering where we can move from general to specific.

If we use the same example to demonstrate run time polymorphism here.

class Parent {
private int i;
// Constructor
Parent(int i){
this.i = i;
}
// Method with no param
public void dispayData(){
System.out.println("Value of i " + i);
}
}

class Child extends Parent{
private int j;
// Constructor
Child(int i, int j){
// Calling parent class constructor
super(i);
this.j = j;
}
// Overridden Method
public void dispayData(){
System.out.println("Value of j " + j);
}
}

public class OverrideDemo {

/**
* @param args
*/
public static void main(String[] args) {
Child child = new Child(5, 7);
Parent parent = new Parent(8);
// calling parent displayData method
parent.dispayData();
// parent holding the reference of child
parent = child;
// child class displayData method will be called
parent.dispayData();
}

}

Output of the program


Value of i 8
Value of j 7

It can be noticed here that initially parent object calls displayData() method of parent class, later at run time the reference is changed and the parent is assigned the reference of child class. Now, when the displayData() method is called on the parent object, it calls the overridden method of the class Child.

Points to note -

  • If method in the sub-class has the same name and signature as the parent class method then only it is called method overriding otherwise it is method overloading.
  • Method overriding allows java to support run-time polymorphism.
  • Method overriding helps in hierarchical ordering where we can move from general to specific. In super class a method can have a general implementation where as the sub classes provide the more specific implementation by overriding the parent class method.
  • If a method is declared as final in parent class then it can not be overridden by sub classes.
  • If a method is declared as abstract in the parent class then the sub class has to provide implementation for those inherited abstract methods

That's all for this topic Method Overriding in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Method Overloading in Java
  2. Inheritance in Java
  3. Abstraction in Java
  4. Exception Handling And Method Overriding
  5. Java OOP Interview Questions

You may also like -

>>>Go to Java Basics Page


Viewing all articles
Browse latest Browse all 938

Trending Articles