Difficulty: Medium
Correct Answer: no, a superclass reference can invoke only methods that are declared in the superclass type, even if it points to a subclass object
Explanation:
Introduction / Context:
Polymorphism in Java allows a reference of a superclass type to point to an object of a subclass. This is a powerful feature that enables flexible APIs and runtime behavior substitution. However, it is important to understand what methods are visible through such a reference. This question explores whether subclass specific methods can be called directly on a reference that is typed as the superclass, even though the actual object is an instance of the subclass.
Given Data / Assumptions:
- There is a superclass, for example Animal, and a subclass, for example Dog, where Dog extends Animal.
- A reference is declared with the superclass type, such as Animal a = new Dog().
- The subclass defines additional methods that do not exist in the superclass, such as bark().
- We consider normal Java typing rules without using casts or reflection tricks.
Concept / Approach:
In Java, the compile time type of a reference determines which member names are accessible. The runtime type of the object determines which implementation of an overridden method is executed. A superclass reference can point to a subclass instance, but the compiler only allows calls to methods declared in the superclass type (or its own ancestors). Subclass specific methods that do not appear in the superclass cannot be called using the superclass typed reference without an explicit cast. Therefore, even though the object is a subclass instance, the reference type limits direct access to subclass only members.
Step-by-Step Solution:
Step 1: Consider Animal a = new Dog(); where Dog extends Animal and defines a method bark().
Step 2: At compile time, the reference a is of type Animal, so the compiler checks only the Animal class for available methods.
Step 3: Since bark() is not declared in Animal, the expression a.bark() will not compile, even though the object is really a Dog.
Step 4: If you first cast the reference to Dog, such as ((Dog) a).bark(), then the compiler will allow the call, but this is because the reference type after casting is Dog.
Step 5: Therefore, without casting, a superclass reference can invoke only methods that are declared in the superclass type, not subclass only methods.
Verification / Alternative check:
To verify, write a small Java program that defines a superclass with a method speak() and a subclass with an extra method bark(). Compile and try to call bark() on a reference declared as the superclass type pointing to a subclass instance. The compiler error will clearly indicate that the method bark() is undefined for the type of the reference. Only after casting the reference to the subclass type will the method be available. This confirms that the correct statement is that superclass references cannot directly call subclass specific methods.
Why Other Options Are Wrong:
Option B claims that a superclass reference can always call any subclass method without casting, which contradicts compile time type checking rules in Java.
Option C says this is possible only if subclass methods are private, which is incorrect because private methods are not even visible outside the subclass itself.
Option D states that a superclass reference can never point to a subclass object, which is false because polymorphic assignments like Animal a = new Dog() are valid and common in Java.
Common Pitfalls:
A common mistake is to confuse dynamic dispatch with access to subclass specific members. Developers sometimes expect that because the actual object is a subclass, all subclass methods should be callable through any reference. In reality, dynamic dispatch only affects which implementation of an overridden method runs, not which method names are visible. Another pitfall is forgetting to perform safe casts and checks, potentially causing ClassCastException at runtime. Clear understanding of reference types and polymorphism helps avoid these errors.
Final Answer:
The correct statement is no, a superclass reference can invoke only methods that are declared in the superclass type, even if it points to a subclass object.
Discussion & Comments