How much money in the Jack pocket ?

public class Main {
    public static void main(String[] args) {        
        RichJack jack = new RichJack();
        jack.showYourWallet();
    }
}

class Jack {
    Jack() {
        this.sayHello();
    }
    
    public void sayHello() {
        System.out.println("I'm Jack!");
    }
}

class RichJack extends Jack {
    private int money = 0;

    public RichJack() {
        money += 100;
    };
    
    public void sayHello() {
        money += 1000000000;   // one billion
        System.out.println("I'm rich Jack!");
    }
    
    public void showYourWallet() {
        System.out.println("I have quite some money, about $" + money);
    };
}
Explanation
Allocated memory for money will be initialized to zero, then the Jack's class constructor will be executed, then performed an explicit initialization of money field to 0, finally the RichJack constructor is executed.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes