0
0
Javaprogramming~20 mins

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

Choose your learning style9 modes available
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.