Javatpoint Logo

Why can't method overriding be possible for diiferent primitive return types?

By: archan*** On: Wed Apr 05 12:50:37 IST 2017     Question Reputation0 Answer Reputation0 Quiz Belt Series Points0  0Blank User
I can override a method if it has the same signature, according to the book the signature of a method is Method_Name + Parameters passed. Even Java tutorial provided by you also explains the same - Rules for Java Method Overriding:

method must have same name as in the parent class
method must have same parameter as in the parent class.
must be IS-A relationship (inheritance).

When I tried doing the method overriding it is throwing compile time error.
Example:
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
class Bike2 extends Vehicle{
int run(){System.out.println("Bike is running safely");
return 0;}

public static void main(String args[]){
Bike2 obj = new Bike2();
obj.run();
}


The only concern here is that if I can't overload the methods with the different primitive return type in java it means same rule applicable for method overriding but here return type is considered. why?

So i am guessing as it is run time polymorphism because of that even return type is also checked, but it throws error during compilation and not at runtime.
Help me with this concept. I am aware of Covariant Retrun Type.



Up0Down

 
class bank
{
void m1(int id,int ac)///METHOD OF PARENT CLASS
{
System.out.println("Employee ID= "+id);
System.out.println("Account Number= "+ac);
}
}
class det extends bank
{
void m1(int id,int sal)///METHOD OF CHILD CLASS
{
System.out.println("Balance= "+id);
System.out.println("salary= "+sal);
}
}
class main
{
public static void main(String arg[])
{
//bank b1=new bank(); ///polymorphism
//b1.m1(14548,154558);
//det d1=new det();
//d1.m1(1244545,78455);
det d1=new det(7894,48458);

}
}

///try this
Image Created0Down

By: [email protected] On: Wed Apr 05 15:14:47 IST 2017 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No
 
vehicle v1=new Bike2();//run time polymorphism
Image Created0Down

By: [email protected] On: Wed Apr 05 15:16:26 IST 2017 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No
 
Fine, but my question is why can't we override the method with different primitive return type?
Like in the Vehicle class return type is void, but the subclass Bike2 return type of method is int.

Try this:
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
class Bike2 extends Vehicle
{
int run(){System.out.println("Bike is running safely");
return 0;
}

public static void main(String args[]){
Vehicle obj = new Bike2();
obj.run();
}
}
---------------------------------------------------------------------------
output:

>javac Bike2.java
Bike2.java:7: error: run() in Bike2 cannot override run() in Vehicle
int run(){System.out.println("Bike is running safely");
^
return type int is not compatible with String
---------------------------------------------------------------------------

I know that method overriding with different primitive return type is not possible. I am looking for reason why not possible....

1 error
Image Created0Down

By: [email protected] On: Wed Apr 05 16:12:03 IST 2017 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No
 
Before Java 5.0, when you override a method, both parameters and return type must match exactly. In Java 5.0, it introduces a new facility called covariant return type. You can override a method with the same signature but returns a subclass of the object returned. In another words, a method in a subclass can return an object whose type is a subclass of the type returned by the method with the same signature in the superclass.Image Created0Down

By: [email protected] On: Sun Apr 09 09:56:05 IST 2017 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No
 
we cant change the return type of primitive type while overriding... covariant return type is used for non primitive typesImage Created0Down

By: [email protected] On: Thu May 25 21:22:34 IST 2017 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No