Trooper
Trooper

Reputation: 75

Superclass reference access clarification

As per below code, if a superclass reference is pointing to subclass object and if the subclass has overridden a method of the superclass, the superclass reference will always have full access to the overridden method in the subclass and no access to other methods of the subclass. Is my assumption correct?

class ParentClass{

    public void Method(){
        System.out.println("Parent method");
    }

}

public class ChildClass extends ParentClass{

    public void Method(){
        System.out.println("child method");
    }

    public static void main(String[] args) {

        ParentClass obj2 = new ChildClass();

        obj2.Method();

    }

}

Upvotes: 0

Views: 28

Answers (3)

vishal paalakurthi
vishal paalakurthi

Reputation: 138

Yes your assumption is correct.
Java allows you to derive new classes from existing classes. ... The original class that is used to derive a new class is called a superclass, a parent class or a base class. The derived class is a subclass or a child class. Java uses the key word extends to indicate that a new class is being derived from a superclass.

public class A{
    public void method(){
        System.out.println("Hello A");
    }
}

public class B extends A{
    public void method(){
        System.out.println("Hello B");
    }
}

public class Program
{
    public static void main(String[] args) {
        A a= new B();
        a.method();
    }
}

here you will get output like "Hello B"

Upvotes: 0

jrtapsell
jrtapsell

Reputation: 7001

Part 1

the superclass reference will always have full access to the overridden method in the subclass

The reference does not have access, instead method calls to it are directed to the subclass method.

The methods will always have the same or weaker access rules because of the JLS, and also because of Liskov's Substitution Principle.

The access modifier (§6.6) of an overriding or hiding method must provide at least as much access as the overridden or hidden method... Source

Part 2

no access to other methods of the subclass

If you have an instance of X, which extends Y, in a variable of type Y you will only be able to access the methods of X, the only way to access the subclass methods would be to cast the variable back to X.

This is because of Liskov's Substitution Principle, which states that an instance of X should be able to be used where an instance of Y is (so all methods of Y should be available), and also because Z could extend Y, with none of Xs methods, so the compiler cannot be sure the extra methods will be available (so no extra methods should be available from X).

Likov's Substitution Principle states that if a program module is using a Base class, then the reference to the Base class can be replaced with a Derived class without affecting the functionality of the program module. Source

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074198

The type of the reference you have is the key. obj2 is of type ParentClass, so it only has access to the interface that ParentClass defines (including Method). At runtime, the Method that's run is the overridden one that the actual object obj2 refers to has, which is the one from ChildClass. If ChildClass defines a method that ParentClass does not, you're correct that you do not have access to it from your obj2 reference, because it's not part of the type of obj2 (ParentClass). (You'd have to cast your reference to ChildClass to access it.)

Upvotes: 1

Related Questions