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
Parent and Child Classes
π Scenario: You are creating a simple program to represent vehicles. You want to show how a child class can inherit properties from a parent class.
π― Goal: Build a parent class called Vehicle and a child class called Car that inherits from Vehicle. Then create an object of Car and print its details.
π What You'll Learn
Create a parent class named Vehicle with a String field called brand.
Create a child class named Car that extends Vehicle and adds an int field called year.
Create a constructor in Vehicle to set the brand.
Create a constructor in Car to set both brand and year.
Create a method displayInfo() in Car that prints the brand and year.
Create a Car object with brand "Toyota" and year 2020, then call displayInfo().
π‘ Why This Matters
π Real World
Understanding parent and child classes helps organize code by sharing common features and adding specific details, like different types of vehicles.
πΌ Career
Inheritance is a key concept in object-oriented programming used in many software development jobs to build reusable and maintainable code.
Progress0 / 4 steps
1
Create the parent class Vehicle
Create a public class called Vehicle with a String field named brand. Add a constructor that takes a String brand parameter and sets the field.
Java
Hint
Think of Vehicle as a blueprint with a brand name. The constructor sets the brand.
2
Create the child class Car
Create a public class called Car that extends Vehicle. Add an int field named year. Create a constructor that takes String brand and int year parameters. Use super(brand) to call the parent constructor and set year.
Java
Hint
Use extends to inherit from Vehicle. Use super to call the parent constructor.
3
Add displayInfo method in Car
Inside the Car class, create a public method called displayInfo() that prints the brand and year in this format: "Brand: [brand], Year: [year]".
Java
Hint
Use System.out.println to print the message. Access brand directly because it is inherited.
4
Create Car object and print info
In a public class called Main, create the main method. Inside it, create a Car object with brand "Toyota" and year 2020. Call the displayInfo() method on this object to print the details.
Java
Hint
Remember the main method signature and how to create objects with new.
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();?