Thinking in Java Fourth Edition Bruce Eckel Chapter 6 Exercise 5

Exercise 5: Create two classes, A and B, with default constructors (empty argument lists) that announce themselves. Inherit a new class called C from A, and create a member of class B inside C. Do not create a constructor for C. Create an object of class C and observe the results.

class A
{
    A()
    {
        System.out.println("A()");
    }
}

class B extends A
{
    B()
    {
        System.out.println("B()");
    }
}

class C extends A
{
    B b = new B(); // will then construct another A and then a B
    public static void main(String[] args)
    {
        C c = new C(); // will construct an A first
    }
}

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *