📚 Quick Study Guide
- 🔗 Inheritance Basics: Allows a class (subclass/child) to inherit fields and methods from another class (superclass/parent). This promotes code reusability.
- ➡️ The `extends` Keyword: Used to establish an inheritance relationship. A subclass `extends` a superclass, gaining access to its non-private members.
- ⬆️ The `super` Keyword: Used to refer to the immediate superclass object. It can call superclass constructors (`super()`) or access superclass methods/fields (`super.method()`, `super.field`).
- 🔄 Method Overriding: A subclass provides a specific implementation for a method that is already defined in its superclass. The method signature (name, parameters, return type) must be the same.
- 🏗️ Constructors in Inheritance: Superclass constructors are invoked from the subclass constructor, either implicitly (default no-arg `super()`) or explicitly using `super(...)`. This ensures proper initialization of the superclass portion of the object.
- 🔒 Access Modifiers & Inheritance: `private` members of a superclass are not directly inherited by the subclass. `protected` members are accessible within the same package and by subclasses in any package.
- 🛑 The `final` Keyword: If a class is declared `final`, it cannot be subclassed. If a method is `final`, it cannot be overridden. If a variable is `final`, its value cannot be changed once assigned.
📝 Practice Quiz
- What is the primary purpose of inheritance in Java?
A) To allow multiple inheritance of classes
B) To achieve runtime polymorphism
C) To promote code reusability and establish an 'is-a' relationship
D) To hide implementation details from the user - Which keyword is used to explicitly call a constructor of the superclass from the subclass constructor?
A) `this`
B) `new`
C) `super`
D) `extends` - If a method in a subclass has the same name, parameters, and return type as a method in its superclass, what concept is being applied?
A) Method overloading
B) Method hiding
C) Method overriding
D) Method shadowing - Which of the following statements about the `final` keyword in Java inheritance is TRUE?
A) A `final` class can be extended by a subclass.
B) A `final` method can be overridden in a subclass.
C) A `final` variable can be reassigned after its initial declaration.
D) A `final` class cannot be extended, and a `final` method cannot be overridden. - Consider the following code:
class Animal {
void eat() { System.out.println("Animal eats"); }
}
class Dog extends Animal {
void eat() { System.out.println("Dog eats bones"); }
}
public class Test {
public static void main(String[] args) {
Animal myPet = new Dog();
myPet.eat();
}
}
What will be the output?
A) "Animal eats"
B) "Dog eats bones"
C) Compile-time error
D) Runtime error - Which access modifier, when applied to a superclass member, makes it accessible within the same package and to subclasses in any package?
A) `private`
B) `default` (package-private)
C) `protected`
D) `public` - If a superclass has a constructor that takes arguments, and the subclass does not explicitly call a superclass constructor using `super()`, what happens?
A) The superclass's no-argument constructor is automatically called.
B) A compile-time error occurs because the superclass's constructor must be explicitly called.
C) The subclass constructor automatically calls the superclass's argument-based constructor.
D) The program will run, but the superclass will not be initialized.
Click to see Answers
1. C
2. C
3. C
4. D
5. B
6. C
7. B