Inheritance in Object-Oriented Programming
If a class does not explicitly extend another class in Java, what class does it implicitly extend?
No class; it is a root class.
The 'Main' class.
The 'Object' class.
The 'Super' class.
What is the primary purpose of the super()
keyword in a subclass constructor?
To create a new instance of the subclass.
To call a method within the subclass.
To refer to the constructor of the superclass.
To access private variables of the superclass.
When would you use super.method()
in a subclass?
When you want to prevent a method from being overridden.
When you want to completely replace the functionality of a superclass method.
When you want to extend the functionality of a superclass method in the subclass.
When you want to call a private method of the superclass.
Consider the following code:
java
class Vehicle {
int speed = 0;
public void accelerate() {
speed += 10;
}
public int getSpeed() {
return speed;
}
}
class Car extends Vehicle {
public void accelerate() {
super.accelerate();
speed += 20;
}
}
public clas...
10
20
30
0
Consider the following code:
java
class Animal {
public Animal(String name) {
System.out.println("Animal constructor: " + name);
}
}
class Dog extends Animal {
public Dog(String name) {
super(name);
}
}
public class Main {
public static void main(String[] args) {
Dog my...
Dog constructor: Buddy
Animal constructor: Buddy
No output (compilation error)
Animal constructor: null
In a multi-level inheritance hierarchy (e.g., ClassA extends ClassB extends ClassC), where should super()
be called to initialize variables defined in ClassC when constructing an object of ClassA?
Only in ClassA's constructor.
Only in ClassB's constructor.
In both ClassA's and ClassB's constructors.
In ClassA's, ClassB's and ClassC's constructors.
Given the following code snippet, which class is the superclass?
public class Animal {}
public class Dog extends Animal {}
Both Animal and Dog are superclasses.
Neither Animal nor Dog are superclasses.

How are we doing?
Give us your feedback and let us know how we can improve
Which of the following statements is true regarding the accessibility of superclass members by a subclass?
A subclass can access all members of its superclass, regardless of access modifiers.
A subclass can only access public members of its superclass.
A subclass can access public and protected members of its superclass.
A subclass cannot access any members of its superclass.
What is the correct syntax to create a subclass 'Car' that inherits from a superclass 'Vehicle'?
class Car inherits Vehicle {}
class Car extends Vehicle {}
class Car implements Vehicle {}
class Car : Vehicle {}
Which of the following statements best describes the relationship between a superclass and a subclass?
A subclass is a more general class than its superclass.
A superclass is a specialized version of its subclass.
A subclass inherits properties and behaviors from its superclass.
A superclass and subclass are unrelated classes with no shared properties.