Menu
Topics Index
...
`

Pass SubClass Objects Where A SuperClass Is Needed


If you need explanation Read this topic

If you need Answer Take test on this topic

Pass SubClass Objects Where A SuperClass Is Needed
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;
}

A. Total = 100
B. Total = 120
C. Compilation Error
D. Runtime Error
Topic: Passing Sub Class Object As Super Class Reference

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans B
because all classes will inheritance and condition
if will be checked in which function gettotal will
be called twice.
at 1st calling it will 120 and 2nd calling it will
return 100 and if condition will be checked as
120>100 as it become trues.
and print 120..

Posted by Shubham Bansal    2014-10-24 12:45:01


Ans is B.
Initially objects for A and B are created which are inherited from Head class and the respective variables are initialized. Then making a call to compareHead() method passing 2 objects as parameters.

In compareHead() method we are getting the total of 2 object variables and we are comparing them and printing the highest total value.

The A object total is 120 and B object total is 100. 120 > 100 so it prints total = 120

Posted by Gopinath Manchikanti    2014-10-24 12:50:26


ans B
because all classes will inheritance and condition if will be checked in which function gettotal will be called twice.
at 1st calling it will 120 and 2nd calling it will return 100 and if condition will be checked as 120>100 as it become trues.
and print 120..

Posted by Shubham Bansal    2014-10-24 12:50:44


ans b.

here 4 classes used.
example main class.
class A B inherits all the features of Head class.

object for class A created and instance values of that object is initialized .
similarly for class B object and instances variables declared and initialized.

comparehead() func called.
both objects of class A and B paasd as an argument .

class A and B object .getTotal () method returns int value by using both objects instances values.

class A object .getTotal returns 120, class B returns 100.

comparehead method if case checks both value and bigger value get printed.

so class A object. getTotal() 120 get printd

Posted by Maheshwari Natarajan    2014-10-24 14:10:28


Ans is B..Here we have 4 classes Example,Head,A,B

here inheritance is involved...class A n B are extending  Head class so that all properties of class Head wil inherited itno class A n B..

in main class we are creating Object for class A and we assigning values to the varibles I,J which are inherited from the class Head

next we are creating Object for class B similary assigning values to the varibles i,j which are inherited from the class Head

we have static method named with compareHead()...method is static so that we can cal it directly without creating Object for it...from one static method another static method we can cal directly

for this method we are passing Objects of Head(a,b)

here we aer calling the getData() with these two objects..here get data is not present in the clss A n B  but it is inheritd from the class Head...this method is returning the some of the i,j..

class A some is grtes so that we are printing that sum in Sop()..

Posted by Uday Kumar    2014-10-24 16:45:49


This dose is now closed and the winners are Shubham Bansal,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Šâí Râm for the 'Popular Comment'. The 'lucky liker' is Vikas Tiyagi. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-10-25 03:26:19


© meritcampus 2019

All Rights Reserved.

Open In App