Thinking in Java Fourth Edition Bruce Eckel Chapter 7 Exercise 10

Exercise 10: Create a base class with two methods. In the first method, call the second method. Inherit a class and override the second method. Create an object of the derived class, upcast it to the base type, and call the first method. Explain what happens.

class A {
    protected void f() {
        System.out.println("A.f()");
        this.g();
    }

    protected void g() {
        System.out.println("A.g()");
    }
}

class B extends A {
    @Override
    protected void g() {
        System.out.println("B.g()");
    }
}

public class Ex10 {
    public static void main(String[] args) {
        B b = new B();
        // automatic upgrade to base-class to call base-class method A.f()
        // which,by polymorphism, will call the derived-class method B.g():
        b.f();
    }
}

Output:

Leave a Reply

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