Reputation: 3
I have recently started learning Java and currently studying about polymorphism. Consider the following code :
public class Polygon{
public static void perimeter( ){
System.out.print("In Polygon perimeter");
}
public void area( ){
System.out.println("In Polygon area");
}
public void sides( ){
System.out.println("In Polygon sides");
}
public void angleSum( ){
System.out.println("In Polygon angleSum");
}
}
public class Pentagon extends Polygon{
public static void perimeter( ){
System.out.println("In Pentagon perimeter");
}
public void area( ) {
System.out.println("In Pentagon area");
}
public int sides( ){
return 5;
}
public void angleSum(int x) {
System.out.println("In Pentagon angleSum");
}
}
On executing this code, I find out that sides() method cannot be overridden. I understand in overriding, the method name and return type should be same but in this case return types are different. So, is there any way to execute it without causing overriding but keeping the same names and different types?
Upvotes: 0
Views: 241
Reputation: 1
As I know, you can not change the return type if you make an inheritance relation and you use the same signature for a method that exists already in the parent class except if you want to change the return type for a type that is an instance of the original type, otherwise which will cause a syntax error, so you need to change the method signature in the subclass or override it. Suppose that will be applicable, so a subclass will have two methods with the same signature one that is inherited and the other you explicitly write, so when you invoke it, the JVM will not know which one to invoke, so that is not applicable. Be careful to override a method it should be an instance method and the same signature and the same return type or an instance of the original type and the same access modifier or stronger, otherwise, it will cause a syntax error.
Upvotes: 0
Reputation: 19555
is there any way to execute it without causing overriding but keeping the same names and different types?
Only if the method in the parent class is declared as private
, no overriding takes place, but the method sides
has very limited access so the following code in Main.java
class fails:
class Polygon {
private void sides() {
System.out.println("In Polygon sides");
}
}
class Pentagon extends Polygon {
public int sides() {
return 5; // is not aware of `sides` in Polygon
}
}
Polygon p = new Pentagon();
p.sides(); // -> sides() has private access in Polygon
Thus, method sides
is accessible only for Pentagon
instances/references:
Pentagon p = new Pentagon();
System.out.println(p.sides()); // -> 5
If sides
methods are declared as static
in both classes having different return types, the code won't compile either:
class Polygon {
public static void sides() {
System.out.println("In Polygon sides");
}
}
class Pentagon extends Polygon {
public static int sides() {
return 5; // sides() in Pentagon cannot hide sides() in Polygon
// return type int is not compatible with void
}
}
Upvotes: 1
Reputation: 2169
In Java, a method signature is part of the method declaration. It's the combination of the method name and the parameter list.
When we talk about the same method signature in the superClass and the subClass + a covariant return type , so we are talking about the Overriding .
Upvotes: 0