This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes interview doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "interview doubt" Watch "interview doubt" New topic
Author

interview doubt

Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 380
    
    2
Hi,

I tried the below code. The only thing I found was with the exception not being declared in the sub class.

I would appreciate if any one could help me understand whats wrong with the below code:

class Xx{

Xx() throws Exception{

System.out.println("Hello");

}
public static void main(String args[]){

new yy();

}

}

class Yy extends Xx{

}

------------------------------

When I modified Yy class as below I did not find any errors...

class Yy extends Xx{

Yy() throws Exception{

}

}

I appreciate all your comments...

Thank you.

Ayub.


SCEA part I,TOGAF Foundation
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
If you call a constructor or a method that throws an exception, you must either
1. put the function/constructor call in try/catch block
2. or declare the method with throws...

In your example, the compiler automatically puts super() as the first statement, so its calling the super class constructor. So you should either use try/catch in sub class constructor or you should declare the constructor with throws...


SCJP 1.4, SCWCD 1.4, SCBCD 1.5
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 380
    
    2
Hi Shashank,


Thank you for answering my question. I understood the concept you told.

Thank you!!
 
Don't get me started about those stupid light bulbs.
 
subject: interview doubt
 
Similar Threads
Making a class non extensible without using final
Why I cannot extend the inner class with enclosing class
method overloading question
I think D is right, but someone told me C is right.
Runtime vs Compile time