Menu
Topics Index
...
`

Creating classes and method Overriding


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating classes and method Overriding
What will be the output of the following program?
public class GetStarted {
    public static void main(String... args) {
        Hai obj1 = new Hi();
        Hello obj2 = (Hello) obj1;
        System.out.print(obj1.method1());
        if (true) {
            System.out.print(" ");
        } else {
            System.out.println();
        }
        System.out.print(obj2.bye);
    }
}
class Hai {
    int seeYou = 99;
    int method1() {    return seeYou;    }
}
class Hello extends Hai {
    int bye = 199;
    int method1() {    return bye;    }
}
class Hi extends Hello {
    int byee = 299;
    int method1() {    return byee;    }
}

A. 299 199
B. 199 199
C. 299 99
D. 199 99
E. Compilation Error
F. Runtime Error
Topic: Multilevel Inheritance In Java With Example Program

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 is A..first method1 in hi is called by obj1 and printed then method1 in hello is called by obj2 which returns 199..So the output is 299 199

Posted by Badam Swathi    2014-07-31 09:10:53


F.

Posted by Pothuluri Venkateswarlu    2014-07-31 09:12:59


Answer is A for the first time object of Hi is created and next it's casted to its super class for first print statement 299 is printed and for the next print statement bye will be returned to print 199.

Posted by Sai Avinash    2014-07-31 09:13:26


A

Posted by Arul Palanisamy    2014-07-31 09:49:38


B 199 199
Becauseat first time it will be call the method of hello class, so the call is made but it does not go to parent class because it has not used super keyword...........
And next 199 is as it can be call by object of hello so it is 199

Posted by Swapnil Kotwal    2014-07-31 11:48:56


A

Posted by Ananth Sathvick    2014-07-31 12:15:16


Ans is f why because to run java code it need main(String arg[]).but inthis there is no p s v main(String ar[]).

Posted by Govindarao Parella    2014-07-31 12:27:36


Congratulations Badam Swathi. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-08-01 05:16:16


© meritcampus 2019

All Rights Reserved.

Open In App