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 a parent class in Java?
A parent class, also called a superclass, is a class that other classes (child classes) inherit from. It provides common properties and methods that child classes can use or override.
Click to reveal answer
beginner
What does it mean when a child class extends a parent class?
When a child class extends a parent class, it means the child inherits all the fields and methods of the parent. The child can use or change these inherited features and add new ones.
Click to reveal answer
intermediate
How can a child class override a method from its parent class?
A child class can provide its own version of a method by defining a method with the same name and parameters. This is called method overriding and allows the child to change behavior.
Click to reveal answer
intermediate
What keyword is used in Java to call a parent class constructor from a child class?
The keyword <code>super</code> is used inside a child class constructor to call the parent class constructor. This helps initialize the parent part of the object.
Click to reveal answer
intermediate
Can a child class access private members of its parent class directly?
No, private members of a parent class are not accessible directly by the child class. They can only be accessed through public or protected methods provided by the parent.
Click to reveal answer
Which keyword is used to create a child class from a parent class in Java?
Ainherits
Bimplements
Cextends
Dsuper
✗ Incorrect
The extends keyword is used to create a child class that inherits from a parent class.
What happens when a child class overrides a method from its parent?
AThe child class provides its own version of the method.
BThe parent method is deleted.
CThe method becomes private.
DThe child class cannot use the method.
✗ Incorrect
Overriding means the child class gives its own version of the method, replacing the parent's behavior when called on the child.
How can a child class call the constructor of its parent class?
AUsing the <code>extends()</code> keyword.
BUsing the <code>this()</code> keyword.
CUsing the <code>parent()</code> keyword.
DUsing the <code>super()</code> keyword.
✗ Incorrect
The super() keyword calls the parent class constructor from the child class.
Can a child class access private fields of its parent class directly?
AYes, always.
BNo, private fields are not accessible directly.
COnly if the child class is in the same package.
DOnly if the child class overrides them.
✗ Incorrect
Private fields are hidden from child classes and can only be accessed through public or protected methods.
What is the main benefit of using parent and child classes?
ACode reuse and organization.
BSlower program execution.
CMaking code harder to read.
DPreventing any changes to code.
✗ Incorrect
Inheritance allows child classes to reuse code from parent classes, making programs easier to organize and maintain.
Explain how inheritance works between parent and child classes in Java.
Think about how a child can get features from a parent and change them.
You got /4 concepts.
Describe how to use the super keyword in a child class constructor.
Remember how to connect child and parent constructors.
You got /3 concepts.
Practice
(1/5)
1.
What keyword is used in Java to create a child class from a parent class?
easy
A. extends
B. implements
C. inherits
D. super
Solution
Step 1: Understand class inheritance in Java
Java uses a specific keyword to link a child class to a parent class, allowing reuse of code.
Step 2: Identify the correct keyword
The keyword extends is used to create a child class that inherits from a parent class.
Final Answer:
extends -> Option A
Quick Check:
Inheritance keyword = extends [OK]
Hint: Remember: child class extends parent class [OK]
Common Mistakes:
Using 'implements' which is for interfaces
Using 'inherits' which is not a Java keyword
Confusing 'super' keyword with inheritance declaration
2.
Which of the following is the correct syntax to declare a child class Dog that inherits from a parent class Animal?
?
easy
A. class Dog implements Animal {}
B. class Dog inherits Animal {}
C. class Dog extends Animal {}
D. class Dog Animal {}
Solution
Step 1: Recall Java class inheritance syntax
In Java, the child class uses the keyword extends followed by the parent class name.
Step 2: Match the correct syntax
Only class Dog extends Animal {} is valid syntax for inheritance.
Final Answer:
class Dog extends Animal {} -> Option C
Quick Check:
Syntax for inheritance = extends [OK]
Hint: Use 'extends' keyword to link child and parent classes [OK]
Common Mistakes:
Using 'inherits' which is not a Java keyword
Using 'implements' which is for interfaces
Omitting the keyword between class names
3.
What will be the output of the following Java code?
class Parent {
void show() {
System.out.println("Parent class");
}
}
class Child extends Parent {
void show() {
System.out.println("Child class");
}
}
public class Test {
public static void main(String[] args) {
Parent obj = new Child();
obj.show();
}
}
medium
A. Parent class
B. Runtime error
C. Compilation error
D. Child class
Solution
Step 1: Understand method overriding and polymorphism
The child class overrides the show() method of the parent class. The object is declared as parent type but created as child type.
Step 2: Determine which method runs at runtime
Java uses runtime polymorphism, so the child class's show() method is called.
Final Answer:
Child class -> Option D
Quick Check:
Overridden method runs from child class [OK]
Hint: Object type decides method at runtime, not reference type [OK]
Common Mistakes:
Thinking parent method runs because of reference type
Expecting compilation or runtime errors
Ignoring method overriding rules
4.
Find the error in the following Java code snippet:
class Parent {
void greet() {
System.out.println("Hello from Parent");
}
}
class Child extends Parent {
void greet() {
System.out.println("Hello from Child");
}
}
public class Test {
public static void main(String[] args) {
Child obj = new Parent();
obj.greet();
}
}
medium
A. Cannot assign Parent object to Child reference
B. Method greet() is missing return type
C. Child cannot extend Parent
D. No error, code runs fine
Solution
Step 1: Analyze object assignment compatibility
In Java, a parent class object cannot be assigned to a child class reference because the parent may lack child-specific features.
Step 2: Identify the error in the code
The line Child obj = new Parent(); causes a compile-time error due to incompatible types.
Final Answer:
Cannot assign Parent object to Child reference -> Option A
Quick Check:
Parent object cannot be assigned to child variable [OK]
Hint: Child reference needs child or subclass object [OK]
Common Mistakes:
Thinking parent object can be assigned to child reference
Confusing method return types with assignment errors
Ignoring Java type compatibility rules
5.
Given the classes below, what will be the output when running new Child().display();?