Menu
Question Index
...

What will be the output of the following program?

class CompareObjects {
    public static void main(String[] args) {
        A a = new A();
        A a2 = new A();
        a2.i = 68;
        B b = new B();
        B b2 = new B();
        b2.i = 40;
        b2.j = 28;
        print(a == a2);
        print(b == a2);
        print(b.equals(a2));
        print(a2 == b2);
        print(a2.equals(b2));
        print(b == b2);
        print(b.equals(b2));
        a2.i++;
        print(b.equals(a2));
    }
    public static void print(boolean value) {
        System.out.print(value ? "T " : "F ");
    }
}
class A {
    int i = 45;
    int sum() { return i; }
    public boolean equals(Object obj) {
        return sum() == ((A) obj).sum();
    }
}
class B extends A {
    int j = 23;
    int sum() { return i + j; }
}


F F F T F T F F
F F T F T F T T
F F T F T F T F
Some other output not shown here
Runtime Error

Doubts

Problems

Topic: Object As A Super Class In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App