What keyword is used to inherit a class?
What is "extends"?
What is method overriding?
What is the process of redefining a method in a subclass?
What does polymorphism mean in Java?
What is the ability of a single action to be performed differently based on the object?
Can a subclass call its superclass’s constructor?
Yes, by using the "super" keyword.
Predict the output: Animal a = new Dog(); a.speak();
Output depends on the speak method defined in Dog. If overridden, it calls the subclass method.
What is the superclass in Java?
The superclass is the class being inherited from
Can a private method be overridden? Why or why not?
No, private methods are not inherited and cannot be overridden.
Give an example of runtime polymorphism.
Example: Parent obj = new Child(); obj.method();
What is the purpose of a no-argument constructor?
A no-argument constructor provides a default way to instantiate an object.
Given a superclass and a subclass, which constructor is called first when creating a subclass object?
The constructor of the superclass is called first before the subclass constructor.
True or False: Java supports multiple inheritance.
What is false?
What is method overloading?
The creation of methods with the same name but different parameters.
What is dynamic method dispatch?
Dynamic method dispatch refers to the process of resolving a method call at runtime.
How are constructors invoked in an inheritance chain?
Constructors are invoked in the inheritance chain from superclass to subclass.
Write a simple code snippet demonstrating method overriding.
class Animal {
void speak() {
System.out.println("Animal speaks");
}
} class Dog extends Animal {
void speak()
{ System.out.println("Dog barks");
}
}
What is the purpose of the super keyword?
The "super" keyword is used to access members (methods or variables) of the superclass.
What happens if the return types of overridden methods differ?
A compile-time error occurs because the signatures must match exactly.
How does polymorphism enhance maintainability?
Polymorphism allows code to work with different object types interchangeably, making updates easier.
What happens if a superclass has no default constructor?
A compilation error occurs if the subclass does not explicitly call a valid superclass constructor.
What is the output when a final method is called in a subclass object?
The final method executes as defined in the superclass, regardless of the subclass object.
Explain the difference between is-a and has-a relationships
An "is-a" relationship denotes inheritance (e.g., Dog is-a Animal). A "has-a" relationship denotes composition (e.g., Car has-a Engine).
Why must the overriding method have the same signature as the method in the superclass?
Because the overriding method needs to maintain compatibility with the superclass version.
Can static methods exhibit polymorphism? Why or why not?
No, static methods belong to the class, not the object, and cannot exhibit polymorphism.
Explain the order of constructor execution in a multi-level inheritance hierarchy.
Constructors are executed from the top of the hierarchy (superclass first) down to the subclass.
Write a code involving a superclass reference pointing to a subclass object.
Parent p = new Child();
p.method();