I have an object of a subclass extending its superclass. There is an overridden method in subclass which can be called using the object.
Is that possible to call the superclass’s function using the subclass object?
package supercall;
public class Main {
public static void main(String[] args) {
SomeClass obj = new SubClass();
obj.go(); //is there anything like, obj.super.go()?
}
}
class SomeClass {
SomeClass() {
}
public void go() {
System.out.println("Someclass go");
}
}
class SubClass extends SomeClass {
SubClass() {
}
@Override
public void go() {
System.out.println("Subclass go");
}
}
Consider the code above.
Here it prints
Subclass go
. Instead I have to print
Superclass go
.
No, it’s not possible, and if you think you need it, rethink your design. The whole point of overriding a method is to replace its functionality. If a different class knows that much about that class’s internal workings, you’re completely killing encapsulation.