SCJP Java OOP and Inheritance 5 — Questions and Answers
Question 1: Which of the following correctly describes method hiding in Java?
- A subclass static method with the same signature as a superclass static method overrides it
- A subclass static method with the same signature as a superclass static method hides it (Correct answer)
- Instance methods and static methods can be interchangeably overridden or hidden
- Static methods participate in dynamic dispatch
Correct answer: A subclass static method with the same signature as a superclass static method hides it
Static methods are not overridden; when a subclass defines a static method with the same signature as a superclass static method, it hides it, and selection is based on the reference type, not the object.
Question 2: class Base { Base() { display(); } void display() { System.out.println("Base"); } } class Child extends Base { int x = 10; void display() { System.out.println(x); } } new Child(); — What is printed?
- Base
- 10
- 0 (Correct answer)
- Compile error
Correct answer: 0
When the Base constructor calls display(), the Child's overridden display() runs, but x has not yet been initialized (instance initializers run after super()), so x is 0.
Question 3: Which of the following is a valid use of the 'super' keyword?
- super.super.method() to skip two levels of hierarchy
- super() to call the superclass constructor from any method
- super.method() to invoke the direct superclass's overridden method (Correct answer)
- super can be used in static methods
Correct answer: super.method() to invoke the direct superclass's overridden method
super.method() can be used in a subclass instance method to explicitly call the direct superclass's version of an overridden method.
Question 4: An interface I declares a method m(). Class A implements I and provides m(). Class B extends A. Which statement is true about B?
- B must also implement m() explicitly
- B inherits A's implementation of m() and satisfies the interface contract (Correct answer)
- B must re-declare that it implements I
- B cannot extend A because A is concrete
Correct answer: B inherits A's implementation of m() and satisfies the interface contract
B inherits A's concrete implementation of m(), which satisfies the interface contract without B needing to redeclare it.
Question 5: What is the effect of casting a superclass reference to a subclass type when the actual object is of the superclass type?
- Compile error
- ClassCastException at runtime (Correct answer)
- The cast succeeds and the object gains subclass methods
- null is returned
Correct answer: ClassCastException at runtime
Downcasting a reference to a subclass type when the actual object is not of that subclass results in a ClassCastException at runtime.
Question 6: Which of the following interface features was introduced in Java 8 that affects how classes implement interfaces?
- Interfaces can now have private methods
- Interfaces can have default methods with concrete implementations (Correct answer)
- Interfaces can have instance fields
- Interfaces can have constructors
Correct answer: Interfaces can have default methods with concrete implementations
Java 8 introduced default methods in interfaces, allowing interfaces to provide concrete method implementations that classes inherit without being forced to override.
Question 7: class Shape { static String type = "Shape"; } class Circle extends Shape { static String type = "Circle"; } Shape s = new Circle(); System.out.println(s.type); — What is printed?
- Circle
- Shape (Correct answer)
- Compile error
- null
Correct answer: Shape
Static fields are accessed based on the reference type at compile time; s is declared as Shape, so s.type refers to Shape.type, printing 'Shape'.
Which of the following correctly describes method hiding in Java?