Introduction on Overriding in OOPs
While implementing the inheritance concept in oops, all the parent class functions are used by all its derived classes. If one derived class wants the specific implementation of the function declared in the parent class but using the same name, same signature, and same return type, an overriding feature is being used. Overriding allows the child class to redefine the function that has been already defined in its parent class or superclass. In this way, one can use both the definition by specifying the object of the class while calling the method. For example, let there is a sum function in both parent and child class, and while calling the function we use the object of child class then the method present in the child class will be called and instead if you use parent class object then the method present in the parent class will be called. this method in the child class is said to override the method present in its superclasses.
Need for Method Overriding
Following are the methods explained.
- Runtime polymorphism method overriding helps to achieve runtime polymorphism in object-oriented programming languages like c++, java,c#, etc.
- Polymorphism means having many forms, i.e. one signature and multiple definitions. in object-oriented languages, when both derived and parent class has the same name and signature of a function, then at one-time compiler resolves definition does it need to call. Runtime polymorphism means binding a method definition to a method called occurs at the runtime of a program. It allows implementing the “one signature multiple definition” aspect of polymorphism. This feature uses dynamic method dispatch, which has a powerful object-oriented design concept to bring code reuse and robustness. This method allows us for a method call to any of its derived classes without knowing the type of object of the derived class.
- Using this feature, one can implement a specialised definition to a generic function that has been inherited from its superclass.
How does it work in OOPs?
The below explanation says that how it works:
- OOPs, help us to implement runtime polymorphism while inheriting a class features into another one. Let’s take a look at the working of overriding in OOPs. Later suppose we have a superclass name animal that has two functions speak and eat. There are two subclasses, cat and dog, that extends the superclass name animal.
- These two subclasses use the same functions, speak and eat but change the implementation but keep the methods’ signature the same. Now in our main method, if you call these functions choosing the reference variable, then at runtime only, it will be decided which function does it need to call. For example, if the object holds the difference of child class that is cat or dog, then the function of a respective class will be called otherwise; if the object holds the parent class’s reference, then the function of an animal class will be called.
- Here we should see that the function is called does not depend on the type of reference variable. The compiler decides this at runtime; only it helps to implement runtime polymorphism. It also helps to implement the specialised definition of the method of the generic definition of method in the parent class.
Code:
class Animal{
//Overridden method
public void eat()
{
System.out.println("Animal is eating");
}
public void speak()
{
System.out.println("Animal is speaking");
}
}
class Cat extends Animal{
//Overriding method
public void eat()
{
System.out.println("Cat is eating");
}
public void speak()
{
System.out.println("Cat is meowing");
}
}
class Dog extends Animal{
//Overriding method
public void eat()
{
System.out.println("Dog is Barking");
}
public void speak()
{
System.out.println("Dog is speaking");
}
}
public class Test{
public static void main(String[] args){
Animal an=new Dog();
Animal an1 = new Cat();
Animal an2 = new Animal();
an.speak();
an.eat();
an1.speak();
an1.eat();
an2.eat();
}
}
Output:
Rules for Overriding a Method
Following are the rules that are to be kept in mind:
Rule #1
A list of the parameters declared in the function of parent class should match the list of the parameters mentioned in the definition of overriding method in child class.
For Example:
The method in the parent class
public int test1(int a);
A method in child class – public string test1();// method overriding will not be supported here as return type and arguments are different, but no compile-time error will come. This method will be considered as a new method of a child class.
Rule #2
The return type of the method present in the child class must be the same or a subtype of the method’s return type being overridden.
For Example:
The method in a parent class
public Object test1(int a);
The method in the child class
public String test1();
Incorrect
The method in the parent class
public String test1(int a);
The method in the child class
public Object test1();
Rule #3
The access specifier present in the method of parent class must be equally restrictive or more restrictive than the method that is overriding it in the child class.
Incorrect
The method in the parent class
public int test1(int a);
A method in the child class
private int test1() ; // more restrictive than public one
Rule #4
Only the instance method with access specifier as public, protected, or default can be overridden. This is because only this instance method can be inherited by its subclass and thus said to be overridden. In case the instance method specified in the parent class is more restrictive example public method, then it will not be inherited in its subclass and does if anyone is defining the same method in a subclass that scenario is not taken as a method overriding as public methods cannot be inherited this cannot be overridden.
For Example:
The method in the parent class
public int test1(int a); //can be inherited and overridden
The method in the child class
private int test1();
Incorrect
The method in the parent class
private int test1(int a); //can not be inherited and overridden
The method in the child class
private int test1();//not overriding method of the parent class
It is considered as a new private method of the child class
Rule #5
The super keyword can be used to call the parent class’s function that its subclass has overridden.
class Animal{
//Overridden method
public void eat()
{
System.out.println("Animal is eating");
}
public void speak()
{
System.out.println("Animal is speaking");
}
}
class Dog extends Animal{
//Overriding method
public void eat()
{
System.out.println("Dog is Barking");
}
public void speak()
{
super.speak(); // Here super keyword is used to call the speak function of the super class.i.e.Animal
}
}
public class Test{
public static void main(String[] args){
Animal an=new Dog();
Animal an2 = new Animal();
an.speak();
an.eat();
an2.eat();
}
}
Output:
Rule #6
Its subclasses cannot override a class’s constructor as a constructor of two classes cannot be the same.
Rule #7
Exception Handling
- Incase the parent class method does not throw an exception, then the overriding method present in the subclass is allowed to throw an only unchecked exception. In case the overriding method shows a checked exception, then the compiler will show an error.
- If the parent class method that is being overridden throws an exception tandem overriding method present in the subclass must through an exception of the same level or its subtype in the exception hierarchy or no exception at all.
Rule #8
Method overriding in multilevel inheritance is also possible.
Code:
class Creature {
//Overriding method
public void eat()
{
System.out.println("Creature is eating");
}
public void speak()
{
System.out.println("Creature is speaking");
}
}
class Animal extends Creature{
//Overridden method
public void eat()
{
System.out.println("Animal is eating");
}
public void speak()
{
System.out.println("Animal is speaking");
}
}
class Dog extends Animal{
//Overriding method
public void eat()
{
System.out.println("Dog is Barking");
}
public void speak()
{
System.out.println("Dog is speaking");
}
}
public class Test{public static void main(String[] args){
Creature an=new Dog();
Animal an1 = new Animal();
Animal an2 = new Dog();
an.speak();
an.eat();
an1.speak();
an1.eat();
an2.eat();
}
}
Output:
Rule #9
Overriding Method rules in Java
- A synchronized method can easily be overridden by a non-synchronized method and vice versa.
- Abstract method present in abstract classes or interfaces is meant to be overwritten to provide specific implementation to the declared methods; otherwise, it will show a compile-time error.
- Defining a static method with the same name as a static method present in the parent class is known as method hiding, not method overriding, as it does not implement runtime polymorphism.
- Final methods cannot be overridden.
Rule #10
Method overriding in C++
Method overriding in c plus is achieved by declaring a method as virtual. A virtual keyword is used to achieve runtime polymorphism in c++. Whereas in Java, all the functions are by default taken as virtual.
Rule #11
It is different from method overloading as it is a process of runtime polymorphism, whereas overloading and method is an example of compile-time polymorphism. In method overriding, it is mandatory to keep the signature of the method the same. when we make any change in the method return type or parameter list, then the compiler treats it as method overloading.
Method Overriding | Method Overloading |
|
|
Output:
When to use it?
Let’s discuss how we can use it.
- OOPs provides a wonderful feature of inheritance to form a great hierarchy between superclass and subclass is from lesser specialization to greater specialization.
- Does we can use the superclass to provide a framework that can be inherited in all of its subclasses, and the subclasses we can define the different functioning of those methods. This is achieved by overriding feature where specialized functioning can be provided to the inherited methods init subclasses.
- It also helps to provide a framework for multiple classes by providing a collection of variables and the method that needs to be implemented in all its subclasses.
Conclusion
OOPs, concepts make code robust and easy to reuse when they are used effectively. This feature enhances the usage of inheritance to provide a specific implementation to a parent class’s generic method in its subclasses. This uses the runtime polymorphism concept to trigger the correct definition of the method. Dynamic method dispatch is done at runtime only, which checks the reference of class the object is holding to call the method. This feature is guided through various rules and does not apply to static methods. We can implement this in any object-oriented language and make our program faster and robust.
Recommended Articles
We hope that this EDUCBA information on “Overriding in OOPs” was beneficial to you. You can view EDUCBA’s recommended articles for more information.