Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is inheritance in Java?
Inheritance is a mechanism where one class acquires the properties and behaviors (methods) of another class, allowing code reuse and establishing a relationship between classes.
Click to reveal answer
beginner
Why do we use inheritance in Java?
Inheritance is used to promote code reuse, reduce redundancy, and create a natural hierarchy between classes, making programs easier to maintain and extend.
Click to reveal answer
intermediate
How does inheritance help in code maintenance?
By centralizing common code in a parent class, changes need to be made only once, and all child classes automatically inherit the updated behavior, simplifying maintenance.
Click to reveal answer
beginner
What is an example real-life analogy for inheritance?
Think of a family where children inherit traits from their parents, like eye color or height. Similarly, a child class inherits properties and methods from a parent class.
Click to reveal answer
intermediate
Can inheritance help in extending functionality?
Yes, inheritance allows child classes to add new features or modify existing ones while keeping the base functionality from the parent class intact.
Click to reveal answer
What is the main purpose of inheritance in Java?
ATo reuse code and create a class hierarchy
BTo hide data from other classes
CTo execute multiple threads
DTo handle exceptions
✗ Incorrect
Inheritance allows classes to reuse code from other classes and establish a hierarchy.
Which keyword is used to inherit a class in Java?
Aimplements
Bsuper
Cinherits
Dextends
✗ Incorrect
The 'extends' keyword is used for class inheritance in Java.
Inheritance helps in which of the following?
ASlowing down program execution
BReducing code duplication
CIncreasing code size
DPreventing code reuse
✗ Incorrect
Inheritance reduces code duplication by allowing reuse of common code.
If class B inherits from class A, which class is the parent?
AClass A
BClass B
CBoth are parents
DNeither is parent
✗ Incorrect
Class A is the parent (superclass), and class B is the child (subclass).
Inheritance allows child classes to:
AChange the parent class name
BDelete parent class methods
CAdd or override methods from the parent class
DPrevent access to parent class
✗ Incorrect
Child classes can add new methods or override existing ones from the parent class.
Explain why inheritance is useful in Java programming.
Think about how sharing common features helps programmers.
You got /4 concepts.
Describe a simple real-life example that illustrates the concept of inheritance.
Consider how children get traits from their parents.
You got /3 concepts.
Practice
(1/5)
1. Why do we use inheritance in Java?
easy
A. To make programs run faster
B. To create unrelated classes
C. To reuse code from an existing class
D. To avoid using methods
Solution
Step 1: Understand inheritance purpose
Inheritance allows a new class to use code from an existing class, avoiding repetition.
Step 2: Compare options
Only To reuse code from an existing class correctly states code reuse. Others are incorrect because inheritance does not create unrelated classes, speed up programs directly, or avoid methods.
Final Answer:
To reuse code from an existing class -> Option C
Quick Check:
Inheritance = Code reuse [OK]
Hint: Inheritance means reusing code from another class [OK]
Common Mistakes:
Thinking inheritance speeds up the program
Believing inheritance creates unrelated classes
Confusing inheritance with method removal
2. Which of the following is the correct syntax to inherit class Animal in Java?
easy
A. class Dog inherits Animal {}
B. class Dog extends Animal {}
C. class Dog implements Animal {}
D. class Dog uses Animal {}
Solution
Step 1: Recall Java inheritance syntax
Java uses the keyword extends to inherit from a class.
Step 2: Check each option
class Dog extends Animal {} uses extends, which is correct. Options A, C, and D use wrong keywords.
Final Answer:
class Dog extends Animal {} -> Option B
Quick Check:
Inheritance keyword = extends [OK]
Hint: Use 'extends' keyword to inherit a class in Java [OK]
Common Mistakes:
Using 'inherits' instead of 'extends'
Confusing 'implements' with class inheritance
Using 'uses' keyword which doesn't exist
3. What will be the output of this Java code?
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Bark"); }
}
public class Test {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}
medium
A. Bark
B. Runtime error
C. Compilation error
D. Animal sound
Solution
Step 1: Understand method overriding and polymorphism
Dog overrides the sound() method of Animal. The object is of type Dog but referenced as Animal.
Step 2: Determine which method runs
At runtime, the Dog's sound() method runs due to polymorphism, printing "Bark".
Final Answer:
Bark -> Option A
Quick Check:
Overridden method runs = Bark [OK]
Hint: Overridden methods run from actual object type [OK]
Common Mistakes:
Expecting base class method output
Thinking it causes compile or runtime error
Confusing reference type with object type
4. Find the error in this inheritance code:
class Vehicle {
void start() { System.out.println("Vehicle started"); }
}
class Car Vehicle {
void start() { System.out.println("Car started"); }
}
medium
A. Class Vehicle should be abstract
B. Missing semicolon after class Vehicle
C. Method start() cannot be overridden
D. Incorrect inheritance syntax in class Car
Solution
Step 1: Check class inheritance syntax
In Java, to inherit a class, use extends keyword. The code misses extends in class Car Vehicle.
Step 2: Verify other options
No semicolon needed after class declaration, methods can be overridden, and Vehicle need not be abstract.
Final Answer:
Incorrect inheritance syntax in class Car -> Option D
Quick Check:
Use 'extends' keyword for inheritance [OK]
Hint: Inheritance needs 'extends' keyword in class declaration [OK]
Common Mistakes:
Forgetting 'extends' keyword
Adding semicolon after class header
Thinking methods can't be overridden
5. You want to create a class SmartPhone that has all features of Phone plus new features like camera and GPS. Which is the best way to do this using inheritance?
hard
A. Make SmartPhone extend Phone and add new methods
B. Make Phone extend SmartPhone and add new methods
C. Create SmartPhone and Phone as separate classes with no relation
D. Copy all Phone code into SmartPhone without inheritance
Solution
Step 1: Identify real-world relationship
A SmartPhone is a type of Phone with extra features, so it should inherit Phone.
Step 2: Apply inheritance correctly
SmartPhone should extend Phone and add new methods for camera and GPS.
Final Answer:
Make SmartPhone extend Phone and add new methods -> Option A
Quick Check:
Subclass adds features to superclass [OK]
Hint: Subclass extends superclass to add features [OK]