What happens when superclass and subclass has a methods that have same name?

Overloading occurs when two or more methods in one class have the same method name but different parameters.

Overriding occurs when two methods have the same method name and parameters. One of the methods is in the parent class, and the other is in the child class. Overriding allows a child class to provide the specific implementation of a method that is already present in its parent class.​

The two examples below illustrate their differences:

The table below highlights their key differences:

  • Must have at least two methods by the same name in the class.

  • Must have a different number of parameters.

  • If the number of parameters is the same, then it must have different types of parameters.

  • Overloading is known as compile-time polymorphism.

  • Must have at least one method by the same name in both parent and child classes.

  • Must have the same number of parameters.

  • Must have the same parameter types.

  • Overriding is known as runtime polymorphism​.

Example codes

Overriding

Take a look at the code below:

class Dog{ public void bark(){ System.out.println("woof "); } } class Hound extends Dog{ public void sniff(){ System.out.println("sniff "); } public void bark(){ System.out.println("bowl"); } } class OverridingTest{ public static void main(String [] args){ Dog dog = new Hound(); dog.bark(); } }

In this overriding example, the dog variable is declared to be a Dog. During compile-time, the compiler checks if the Dog class has the bark() method. As long as the Dog class has the bark() method, the code compiles. At run-time, a Hound is created and assigned to dog, so, ​ it calls the bark() method of Hound.

Overloading

Take a look at the code below:

class Dog{ public void bark(){ System.out.println("woof "); } //overloading method public void bark(int num){ for(int i=0; i<num; i++) System.out.println("woof "); } }

In this overloading example, the two bark methods can be invoked using different parameters. The compiler knows that they are different because they have different method signatures​ (method name and method parameter list).

RELATED TAGS

overloading

overriding

classes

Copyright ©2022 Educative, Inc. All rights reserved

A brief introduction, implementation, benefits, and limitations of method overriding

Method overriding is a feature that allows a subclass, or a child class, to specifically implement a method already given in one of its super-classes, or parent classes, in any object-oriented programming language.

Thus, the process of redefining a parent class’s method in a subclass is known as method overriding. It is also called run time polymorphism or dynamic binding because the compiler doesn’t really know the type of object passed on compilation.

When a method in a subclass has the same name, parameters or signature, and return type (or sub-type) as a method in its super-class, then the method in the subclass (the child class) overrides the method in the super-class (the parent class).

We can implement method overriding in any object-oriented programming language, but only when the classes involved have an ‘IS-A’ relationship of inheritance between them.

In the illustration above, the Rectangle and Circle classes are overriding the Shape of the class’s getArea() method. The purpose of overriding is achieved so that a sub class can provide its own implementation to a method that a superclass already provides.

class Animal { public void eat(){ System.out.println("Eat all eatables"); } } class Dog extends Animal { //eat() method overridden by Dog class. public void eat(){ System.out.println("Dog likes eating bones"); } public static void main(String[] args) { Dog d = new Dog(); d.eat(); } }

In the above example, the Dog class gives its own implementation of the eat() method. For method overriding, the method must have same name and same type signature in both the superclass and the subclass.

Method overriding and dynamic method dispatch

To accomplish Java runtime polymorphism, overriding methods are utilized. The object that is used to call the method determines the version of the method that is being run. When a method is called with an object from a superclass, the parent class’s version is executed, but when a method is called with an object from a subclass, the child class’s version is executed.

That is, which version of the overridden method is performed is determined by the type of the referenced object (not the type of the referenced variable). The practice of resolving overridden method calls at runtime is known as dynamic method dispatch. To understand this, see the example below:

class Mother{ //Overridden method public void smile(){ System.out.println("The Mother smiling"); } } class Baby extends Mother{ //Overriding method public void smile(){ System.out.println("The Baby is smiling"); } // New method is Baby public void tickle(){ System.out.println("The Baby is tickling"); } } class Main{ public static void main( String args[]) { Mother call = new Mother(); Mother call1 = new Baby(); //This will call the child class version of smile() call1.smile(); //This will call the Mother class version of smile() call.smile(); } }

In the example above, the smile() method call with the second object (call1) is runtime polymorphism (or dynamic method dispatch).

Note: In dynamic method dispatch, an object can call overriding methods of child classes and all non-overriding methods of base classes, but not newly declared methods in child classes. In the example above, object call1 calls smile(). However, when I try to call the tickling() method (newly declared in class Baby) using obj2, I get a compilation error with the following message: Thread “main” exception java.lang.Error: unresolved compilation Problem: Method tickle() is not defined for type Mother

Inheritance and final keyword

For method overriding to happen, there has to be an IS-A relationship between a baseclass and a subclass. This is known as inheritance in OOP concept. When there is an IS-A relationship between two classes, the child class can therefore override the methods it inherits from the base class.

During inheritance, we have to declare methods with the final keyword which we required to follow the same implementation throughout all the derived classes. When a method is declared as final, it cannot be overridden by subclasses. The classes that extend from a base class that have methods with final keywords can only implement the method same way it was in the super class.

Covariant in method overriding

If a subclass overrides any method with a Non-Primitive return type, the method can be overridden by modifying the return type.

It is possible for a child class to have a distinct return type for an overriding method, but the child’s return type must be a sub-type of the parent’s return type.

class Base{ Base get(){ return this; } } class Child extends Base{ @Override Child get(){ return this; } void message(){ System.out.println("welcome to covariant return type"); } public static void main(String args[]){ new Child().get().message(); } }

Can superclass and subclass have the same name?

what happens if both superclass and subclass have a field with same name? Sub class field will hide the Super class field. Hidden super class field in sub class can be accessed using super keyword.

Can a class be a superclass and a subclass at the same time?

Yes it can be!! For example if there are Class A, Class B and Class C then B can be subclass of A and supper class of C. So class B becomes Parent class as well as Child class.

Is superclass and subclass have same variable name?

If super class and subclass have same variable name, which keyword should be used to use super class? Explanation: Super keyword is used to access hidden super class variable in subclass.

When a method in a subclass has the same?

Explanation: When a method in a subclass has the same name and type signatures as a method in the superclass, then the method in the subclass overrides the method in the superclass.