Menu
Topics Index
...
`

Finally keyword2978


If you need explanation Read this topic

If you need Answer Take test on this topic

Finally keyword2978
What will be the output of the following program?
public class FinallyDemo {
    class Manhattan {
        void doStuff() {
            System.out.println("X ");
        }
    }
    class TimesSquares extends Manhattan {
        @Override
        void doStuff() {
        }
    }
    public static void main(String[] args) {
        new FinallyDemo().go();
    }
    void go() {
        new TimesSquares().doStuff();
    }
}

A. X
B. X X
C. (No output)
D. Compilation Error
E. Runtime Error
Topic: Java Finally Block In Exception Handling

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.

i'll go with option c... here the flow of execution starts from main() method .. creates a new "FinallyDemo " object and calls go method in it ...  then at  "go() method" TimesSquares() inner calss " object instance is created and it calls "doStuff() method of TimesSquare" which has no statements to excute (because overriden the Manhattan doStuff() method" .. So.. Here it again Comes to main and Excution Stops.. So.. , there is no output..

Posted by ?????????? ?????    2015-01-19 04:03:38


Ans is option C... Execution starts from main() method, after execution of first statement it will create object for FinallyDemo class and it will cal the non-static method go()... inside the method go() we have a single statement after execution of this statement it will create object for TimesSquares class and also the super class object Manhattan class  and it will cal the non-static method of class TimesSquares because if the method in super class and sub class same then first compiler will call the sub class method so it will call the TimesSquares class non-static method doStuff() inside method there are no statements so program will execute properly and with out output

Posted by Bhagi Bhagyasri    2015-01-19 04:04:12


Ans is C...here we have the inheritance concept where child class will get the properties of the Parent class..
here we have Mahattan class is Parent class which contains doStuff() and TimesSquare class as child class to the Mahattan class , in child class we are overiding the doStuff()..giving new defination in child class @override is an anotaion which tells to the system that this method is overriding from the Parent if it is not present in the Parent than error will raise

these 2 classes are inner classes in the FinallyDemo class..
Execution:

here we are calling the go() with the instance of the FinallyDemo class than go get invoked next in the go we are calling the doStuff() with the instance of the TimesSqure class than doStuff() in TimesSqure get invoked (if Parent n Child class has same names than system will check the Calling object type here instance is TimesSqure)so here we are not printing anything

Posted by Uday Kumar    2015-01-19 11:42:54


Ans is C as class finalldemo is created in which another class manhattan  is created in which dostuff() method is created  with some printf statement in the class timessquares  which extends manhattan  the method specified in manhattan class is overided ....execution starts with main here an instance of finallydemo  class then go get invoked  in the go method an instance of timessquares  get invoked dostuff () method it goes to overided dostuff() and as no stmts are present it prints nothing

Posted by Ramya Reddy Addulla    2015-01-19 13:00:23


ANS IS C(NO OUTPUT)

Here,the concept of "NESTED CLASS","METHOD OVERRIDING" ans "inheritance" is used.

--Here,three classes are defined:

1)FinallyDemo ---->Ouetr Class
2)Manhattan ---->Inner class(Parent/base/super class)
3)TimesSquares  ---->Inner class(child/subclass)

--dostuff() method is overridden in TimesSquares() so whenever it is called overridden method() of TimesSquares will be executed.so,To diffrentiate we can use the object name period operator with method name() to call the method of particular class.

Control Flow:

Execution starts from main():

1)go() method of FinallyDemo is called is called with class name as it is non-static method of FinallyDemo so,to use class name is necessary.

CONTROL GOES TO GO METHOD.

2)In non-static method of FinallyDemo,dostuff() method is called with the class name as TimesSquares.It is necessary to mention the class name otherwise "Error" will be displayed at compile time as "SYMBOL NOT FOUND." as this method is defined in different class.

CONTROL GOES TO dostuff() OF TimesSquares CLASS.

3) As nothing is written in dostuff() of TimesSquares class so nothing will get printed.

Posted by Mânïshå Mùlchåndânï    2015-01-20 17:51:00


This dose is now closed and the winners are ?????????? ?????,  for 'First Correct Comment', Bhagi Bhagyasri, Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Uday Kumar. 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    2015-01-21 07:44:16


© meritcampus 2019

All Rights Reserved.

Open In App