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;
        compareHead(a, b);
    }
    public static void compareHead(Head ent1, Head ent2) {
        if ((ent1.getTotal() > ent2.getTotal())) {
            System.out.println("Total = " + ent1.getTotal());
        }
        else {
            System.out.println("Total = " + ent2.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: Passing Sub Class Object As Super Class Reference

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App