Can Override Methods Differ In Return Type In Java?

In this Tutorial we will discuss Can Override Method differ in Return Type In Java? Actual Yes we can override the method with a different return type in java. As we can overload only with either by changing the return type of number of arguments, even by changing the return types of arguments.

Override Methods Differ In Return Type What Is Overloading And Override

Overloading is a condition when we use the name of a method with multiple types in the same class differing either in the return type or in a number of arguments of that function.

In such conditions, we can also differ two functions in the data types of arguments passed in the method so we can overload a function in multiple ways in some same manner we also have another concept that we call override.

Let’s discuss the override

It is also a way to use the same signature of function as of previous but override happens in child and parent classes mean in inheritance the override could occur.

it means when two methods have the same name in child and parent classes then overloading occurred.

Let us uderstand with an example.

class Base {
  Object get() { return null; }
}

class Derived extends Base {
  @Override
  Integer get() { return null; }
}

Here we can see how a method get() is overridden and we have used it in parent class as an object and in the child class we have used with same name but with different data type that is a wrapper class Integer

We can have number of such examples where we have used the same name of function in parent class and then in child class with same name just to override a function.

Learn More about Can overridden methods differ in return type?: at Return types

and also visit JAVA TUTORIALto learn more about java and solve all the problems related to java and keep  leaning

Leave a Comment

%d bloggers like this: