• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

from abhilash - constructors

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the following piece of code carefully.
import java.io.IOException;

public class Question72
{
public Question72() throws IOException
{
throw new IOException();
}
}
Assume that the defination of Question72E begins with the line
public class Question72E extends Question72
It is required that none of the constructors of Question72E should throw any checked exception.

1.It can be achived by placing the call to the superclass with a super keyword , which is placed in a try block with a
catch block to handle the IOException thrown by the super class.
2.It can be achived by avoiding explicit calls to the base class constructor.
3.It cannot be done in the Java Language with the above definition of the base class.
// i want to ask why 1 isn't correct?
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
super is to be the first line and compiler will object if u write try before super call
now if we dont call super explicitly and provide a try catch block still super is executed before any code is executed in the constructor and even if u write the try catch block super will be called before that.
Cherry
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer would be
3.It cannot be done in the Java Language with the above definition of the base class.

super or this has the be the first statement of the constructor. If not an implicit call (super())is inserted by the compiler before the first statement. This makes catching checked exceptions thrown by the super class constructor impossible in the derived class constructor.
-
Siva
 
Anshuman Acharya
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it! just slipped out of my mind... thanx!
 
Something about .... going for a swim. With this tiny ad ...
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic