Accessing super class function using subclass object
Posted
by bdhar
on Stack Overflow
See other posts from Stack Overflow
or by bdhar
Published on 2010-03-12T11:10:08Z
Indexed on
2010/03/12
11:17 UTC
Read the original article
Hit count: 430
java
|inheritance
I have an object for a subclass who is of it's superclass type. There is a overridden function in subclass which gets executed when called using the object. How to call the parent's function using the same object? Is there any way to achieve this?
package supercall;
public class Main {
public static void main(String[] args) {
SomeClass obj = new SubClass();
obj.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
.
© Stack Overflow or respective owner