File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Hnadling in inheritence Concepts

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I know that when we are doing overriding , the child class method can deal with fewer exceptions than that are defined in Parent class Methods .

But why is it compulsary to deal with all exceptions when coming to constructors .

For example :



The above is not a valid code .

Its becomes valid when and only the Child constructor also declares SQLException.

Why is such a behaviour in constructors ??
 
Campbell Ritchie
Sheriff
Pie
Posts: 47222
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write down the path of control on a piece of paper and it will become obvious.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't get you campbell
 
Rob Spoor
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The child constructor always calls one of the parent constructors*; if you leave out the call to "super(...)" one will be added implicitly without any arguments. This whole process of a child class constructor calling its super class constructor is called constructor chaining, and it will only end when Object's non-argument constructor is called.

In this case, the parent constructor can throw an exception so the child constructor must, as always, either catch it or declare throw it. Since the first statement must always be a call to "super(...)" or "this(...)" (as said before, if you don't add it the compiler will) catching is not allowed so the only option is to throw it.

* Or another constructor of the same class if the first call is to "this(...)".
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the great explanation .

Can you please tell me actually what does this mean then :

Constructors are not inhereted!

Thank you .
 
Rob Spoor
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider this example:
Even though class TS has a constructor that takes a String, and you can call new T("Hello World"), class Child does not so calling new Child("Hello World") will not work.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Now i am understanding why do we need to override different constructors that are present inside the Exception class when extending the Exception class .


Thanks once again .
 
Campbell Ritchie
Sheriff
Pie
Posts: 47222
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Kiran V wrote:
Now i am understanding why do we need to override different constructors that are present inside the Exception class when extending the Exception class .


Thanks once again .
Look at the constructors. Why do you think they have to be overridden?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic