Parent and child classes in Java - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When working with parent and child classes, it's important to understand how the program runs as the input grows.
We want to know how the time needed changes when we create or use many objects from these classes.
Analyze the time complexity of the following 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 Main {
public static void main(String[] args) {
Parent[] arr = new Parent[1000];
for (int i = 0; i < arr.length; i++) {
arr[i] = new Child();
arr[i].greet();
}
}
}
This code creates an array of child objects and calls a method on each one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that creates and calls greet() on each object.
- How many times: It runs once for each element in the array, so 1000 times in this example.
Each time we add more objects, the loop runs more times, doing more work.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 creations and 10 greet calls |
| 100 | 100 creations and 100 greet calls |
| 1000 | 1000 creations and 1000 greet calls |
Pattern observation: The work grows directly with the number of objects; doubling objects doubles the work.
Time Complexity: O(n)
This means the time needed grows in a straight line with the number of objects created and used.
[X] Wrong: "Since greet() is overridden, it might take longer for each call as n grows."
[OK] Correct: Each greet() call takes the same small amount of time regardless of how many objects exist; the number of calls matters, not the override.
Understanding how loops over objects affect time helps you explain program speed clearly and confidently.
"What if we added a nested loop inside the main loop that also iterates over the array? How would the time complexity change?"
Practice
What keyword is used in Java to create a child class from a parent class?
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 keywordextendsis used to create a child class that inherits from a parent class.Final Answer:
extends -> Option AQuick Check:
Inheritance keyword = extends [OK]
extends parent class [OK]- Using 'implements' which is for interfaces
- Using 'inherits' which is not a Java keyword
- Confusing 'super' keyword with inheritance declaration
Which of the following is the correct syntax to declare a child class Dog that inherits from a parent class Animal?
?
Solution
Step 1: Recall Java class inheritance syntax
In Java, the child class uses the keywordextendsfollowed by the parent class name.Step 2: Match the correct syntax
Onlyclass Dog extends Animal {}is valid syntax for inheritance.Final Answer:
class Dog extends Animal {} -> Option CQuick Check:
Syntax for inheritance = extends [OK]
- Using 'inherits' which is not a Java keyword
- Using 'implements' which is for interfaces
- Omitting the keyword between class names
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();
}
}Solution
Step 1: Understand method overriding and polymorphism
The child class overrides theshow()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'sshow()method is called.Final Answer:
Child class -> Option DQuick Check:
Overridden method runs from child class [OK]
- Thinking parent method runs because of reference type
- Expecting compilation or runtime errors
- Ignoring method overriding rules
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();
}
}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 lineChild obj = new Parent();causes a compile-time error due to incompatible types.Final Answer:
Cannot assign Parent object to Child reference -> Option AQuick Check:
Parent object cannot be assigned to child variable [OK]
- Thinking parent object can be assigned to child reference
- Confusing method return types with assignment errors
- Ignoring Java type compatibility rules
Given the classes below, what will be the output when running new Child().display();?
class Parent {
void display() {
System.out.println("Parent display");
}
}
class Child extends Parent {
void display() {
super.display();
System.out.println("Child display");
}
}Solution
Step 1: Understand use of
The child class'ssuperin child methoddisplay()method callssuper.display(), which runs the parent class'sdisplay()method first.Step 2: Determine output sequence
First, "Parent display" is printed, then "Child display" is printed on the next line.Final Answer:
Parent display Child display -> Option BQuick Check:
super calls parent method before child output [OK]
- Ignoring the call to super.display()
- Expecting only child output
- Thinking super causes error without constructor
