Menu
Question Index
...

What will be the output of the following program?

public class Example {
    public static void main(String s[]) {
        A a = new A();
        a.i = 65;
        a.j = 55;
        B b = new B();
        b.i = 5;
        b.j = 95;
        Head head1;
        Head head2;
        head1 = a;
        head2 = b;
        if ((head1.getTotal() < head2.getTotal())) {
            System.out.println("Total = " + head1.getTotal());
        }
        else {
            System.out.println("Total = " + head2.getTotal());
        }
    }
}
class Head {
    int i;
    int j;
    int getTotal() {
        return i + j;
    }
}
class A extends Head {
    int k;
}
class B extends Head {
    int l;
}


Total = 100
Total = 120
Compilation Error
Runtime Error

Doubts

Problems

Topic: Assigning Sub Class Object To Super Class Reference 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