Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing super class function using subclass object

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

.

like image 574
bdhar Avatar asked Mar 12 '10 11:03

bdhar


People also ask

Can we call super class method using subclass object?

Yes, you can call the methods of the superclass from static methods of the subclass (using the object of subclass or the object of the superclass).

How do you access super class method in subclass?

We can invoke the overridden method of the parent class with the help of the super keyword. super() is used for executing the constructor of the parent class and should be used in the first line in the derived class constructor.

Can subclass access superclass fields?

A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass. A nested class has access to all the private members of its enclosing class—both fields and methods.

Can a subclass variable reference a superclass object?

First approach (Referencing using Superclass reference): A reference variable of a superclass can be used to a refer any subclass object derived from that superclass. If the methods are present in SuperClass, but overridden by SubClass, it will be the overridden method that will be executed.


3 Answers

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.

like image 88
Michael Borgwardt Avatar answered Oct 23 '22 11:10

Michael Borgwardt


Here it prints Subclass go . Instead I have to print Superclass go

Alright, then do not override go method @ subclass, it will call superclass implementation.

If you want to run super implementation, and have some other additional code @ subclass, you call super.go(); and then run some other statements.

It's ok, as you are reusing already written code, you shouldn't copy-paste code from superclass and put it into subs as it's code duplication. But if your goal is to change behaviour completely, then don't call super

like image 40
EEE Avatar answered Oct 23 '22 10:10

EEE


Instead of:

System.out.println("Subclass go");

Write

super.go();

(Or, you know, just don't implement that method ...).

like image 2
Noon Silk Avatar answered Oct 23 '22 11:10

Noon Silk