Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructors

 
Doit
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.IOException;
public class Question72
{
public Question72() throws IOException
{
throw new IOException();
}
}
public abstact class Question73 extends Question72
{
public abstract void method();
}

The answer given was
will cause a compiler error - a constructor must be provided which must throw an IOException or one of its super types.

I think constructors are not inherited and can not be overridden.
So this answer does not make sense to me. Please explain.
-Thanks.
 
yanish
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A parent class has a default constructor which throws IOException. But as the sub class has no any defined constructors, the compiler creates a default one but without the 'throws' clause. As parent's constructor throws a checked exception, it's needed to handle it correctly. You can do it by placing 'throws' clause in constructor definition or by using the 'try-catch' block. But created by the compiler default constructor does neither.
You can look at: http://www.javaranch.com/ubb/Forum24/HTML/003480.html
Some related discussion is there.
 
Doit
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Yanish.
One more doubt...
I think default constructors gets the access modifier which is same as class access modifier. Am i right?
- Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic