Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

interview doubt  RSS feed

 
Ranch Hand
Posts: 397
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Ayub ali khan
Ranch Hand
Posts: 397
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shashank,


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

Thank you!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!