Which statement best describes the relationship between a subclass and its superclass?
4 Inheritance and Class Hierarchies Online Quiz Questions
Use this free practice quiz with 20 questions to review 4 Inheritance and Class Hierarchies, test your knowledge, and prepare for your next test or exam.
Which statement accurately describes constructors in an inheritance hierarchy?
- A
Constructors are always inherited exactly like methods
- B
A subclass cannot invoke a superclass constructor
- C
Constructors are generally not inherited, but a subclass can invoke a superclass constructor
- D
Only abstract classes can have constructors
Given Animal animal = new Dog();, what happens when animal.speak() is called if Dog overrides speak to print “Bark”?
- A
The Animal implementation is selected because the variable is declared as Animal
- B
The Dog override is selected because the object is a Dog
- C
Both implementations execute automatically
- D
The program fails because a superclass reference cannot refer to a subclass object
Which two conditions support using inheritance for a class hierarchy? Select all correct choices.
- A
The subclass genuinely is a specialized version of the superclass
- B
The only reason is to copy a few methods quickly
- C
The superclass's public behavior makes sense for every subclass
- D
The subclass must disable or reject several inherited operations
Which two problems are associated with deep inheritance hierarchies? Select all correct choices.
- A
They can hide important fields and methods defined far above the subclass
- B
They guarantee that every subclass has fewer dependencies
- C
They can make it difficult to determine which override executes
- D
They always make classes easier to test in isolation
True or false: An abstract superclass cannot normally be instantiated directly.
- A
True
- B
False
True or false: In Java, a subclass can call the superclass version of an overridden method with super.
- A
True
- B
False
What term describes a subclass supplying a new implementation with the same method signature as an inherited overridable method?
What relationship phrase describes the connection between a Car and an Engine when the Car contains or uses an Engine?
Complete both definitions: The class that acquires selected state and behavior is the , while the existing class from which it inherits is the .
In the example with abstract class Shape, Shape requires the operation, and the concrete subclass provides its implementation.
A team proposes making ReportWithExport inherit from Report solely because ReportWithExport can reuse two formatting methods. The subclass would also need to reject several other inherited operations. Should the team use inheritance? Explain your decision using the design principles for class hierarchies, and identify a better alternative if appropriate.
What is the main purpose of Java's @Override annotation on a subclass method?
- A
It makes every method in the subclass abstract
- B
It documents the intended override and helps the compiler detect mistakes
- C
It causes the superclass constructor to be inherited
- D
It prevents the method from participating in polymorphism
Which declaration correctly represents the is-a relationship described by the hierarchy in which a Dog is a specialized Animal?
- A
Dog extends Animal
- B
Animal extends Dog
- C
Dog has an unrelated relationship with Animal
- D
Animal uses Dog only as a utility object
True or false: In languages such as Java, constructors are generally inherited from a superclass by its subclass.
- A
True
- B
False
Given the code Animal animal = new Dog();, where Dog overrides Animal's speak() method to print Bark, what does animal.speak() print?
- A
Some sound
- B
Bark
- C
An error because Animal has no speak method
- D
No output
A Car contains and uses an Engine, but an Engine is not a kind of Car. Which design best models this relationship?
- A
Make Engine a superclass of Car
- B
Make Car a superclass of Engine
- C
Place an Engine object inside Car using composition
- D
Make Car and Engine unrelated even if Car needs engine behavior
What is the consequence of declaring Shape as an abstract superclass when it contains an abstract area() method?
- A
It can be instantiated only when all subclasses are absent
- B
It cannot normally be instantiated directly
- C
It automatically implements every abstract method
- D
It prevents all subclasses from adding behavior
In Java, a subclass overrides a method and needs to call the superclass implementation before adding its own behavior. Which keyword should it use?
In C#, a subclass overrides a method and needs to invoke the original superclass implementation before extending it. Which keyword provides access to that superclass implementation?