Bird
Raised Fist0
Javaprogramming~20 mins

Parent and child classes in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
πŸŽ–οΈ
Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of method overriding in parent and child classes

What is the output of the following Java code?

Java
class Parent {
    void show() {
        System.out.println("Parent show");
    }
}

class Child extends Parent {
    void show() {
        System.out.println("Child show");
    }
}

public class Test {
    public static void main(String[] args) {
        Parent obj = new Child();
        obj.show();
    }
}
AParent show
BRuntime error
CCompilation error
DChild show
Attempts:
2 left
πŸ’‘ Hint

Remember that method overriding uses the actual object's method, not the reference type's method.

❓ Predict Output
intermediate
2:00remaining
Output of accessing parent and child fields

What will be printed when running this Java code?

Java
class Parent {
    String name = "Parent";
}

class Child extends Parent {
    String name = "Child";
}

public class Test {
    public static void main(String[] args) {
        Parent obj = new Child();
        System.out.println(obj.name);
    }
}
AParent
BChild
CCompilation error
DRuntime error
Attempts:
2 left
πŸ’‘ Hint

Field access depends on the reference type, not the object type.

πŸ”§ Debug
advanced
2:00remaining
Identify the error in constructor chaining

What error will this Java code produce?

Java
class Parent {
    Parent() {
        System.out.println("Parent constructor");
    }
}

class Child extends Parent {
    Child() {
        this();
        System.out.println("Child constructor");
    }
}

public class Test {
    public static void main(String[] args) {
        new Child();
    }
}
AStackOverflowError at runtime
BCompilation error: recursive constructor call
CPrints "Parent constructor" then "Child constructor"
DPrints "Child constructor" only
Attempts:
2 left
πŸ’‘ Hint

Check how constructors call each other using this() and super().

❓ Predict Output
advanced
2:00remaining
Output of method call with super keyword

What is the output of this Java program?

Java
class Parent {
    void display() {
        System.out.println("Parent display");
    }
}

class Child extends Parent {
    void display() {
        System.out.println("Child display");
    }
    void show() {
        super.display();
        display();
    }
}

public class Test {
    public static void main(String[] args) {
        Child c = new Child();
        c.show();
    }
}
AChild display\nChild display
BChild display\nParent display
CParent display\nChild display
DParent display\nParent display
Attempts:
2 left
πŸ’‘ Hint

super.display() calls the parent method, display() calls the child method.

🧠 Conceptual
expert
2:00remaining
Number of objects created in inheritance

Consider this Java code:

class Parent {
    Parent() {
        System.out.println("Parent created");
    }
}

class Child extends Parent {
    Child() {
        System.out.println("Child created");
    }
}

public class Test {
    public static void main(String[] args) {
        Child c = new Child();
    }
}

How many objects are created when new Child() is executed?

AOne object of type Child
BOne object of type Parent
CTwo objects: one Parent and one Child
DNo objects, only constructors run
Attempts:
2 left
πŸ’‘ Hint

Remember that a child object includes the parent part; only one object is created.

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

  1. 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.
  2. Step 2: Identify the correct keyword

    The keyword extends is used to create a child class that inherits from a parent class.
  3. Final Answer:

    extends -> Option A
  4. 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

  1. Step 1: Recall Java class inheritance syntax

    In Java, the child class uses the keyword extends followed by the parent class name.
  2. Step 2: Match the correct syntax

    Only class Dog extends Animal {} is valid syntax for inheritance.
  3. Final Answer:

    class Dog extends Animal {} -> Option C
  4. 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

  1. 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.
  2. Step 2: Determine which method runs at runtime

    Java uses runtime polymorphism, so the child class's show() method is called.
  3. Final Answer:

    Child class -> Option D
  4. 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

  1. 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.
  2. Step 2: Identify the error in the code

    The line Child obj = new Parent(); causes a compile-time error due to incompatible types.
  3. Final Answer:

    Cannot assign Parent object to Child reference -> Option A
  4. 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();?

class Parent {
    void display() {
        System.out.println("Parent display");
    }
}
class Child extends Parent {
    void display() {
        super.display();
        System.out.println("Child display");
    }
}
hard
A. Child display
B. Parent display Child display
C. Parent display
D. Compilation error

Solution

  1. Step 1: Understand use of super in child method

    The child class's display() method calls super.display(), which runs the parent class's display() method first.
  2. Step 2: Determine output sequence

    First, "Parent display" is printed, then "Child display" is printed on the next line.
  3. Final Answer:

    Parent display Child display -> Option B
  4. Quick Check:

    super calls parent method before child output [OK]
Hint: super.method() runs parent method inside child method [OK]
Common Mistakes:
  • Ignoring the call to super.display()
  • Expecting only child output
  • Thinking super causes error without constructor