• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why this is not done in Java?

 
Ranch Hand
Posts: 339
  • 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 Number1
{
public Number1() throws IOException
{
throw new IOException();
}
}


Assume that the definition of Number1E begins with the line

public class Number1E extends Number1


It is required that calls to any of the constructors of Number1E will not cause any checked exceptions to be thrown. How can this be achieved?


a>By placing the call to the superclass with a super keyword in a try block, with a catch block to handle the IOException thrown by the super class.
b>It can be achieved by avoiding explicit calls to the base class constructor.
c>It cannot be done in the Java Language with the above definition of the base class.


Answer to this question is given is c>
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pradeepdon Singh
I have checked it out

a>By placing the call to the superclass with a super keyword in a try block, with a catch block to handle the IOException thrown by the super class.
The above option is not possible. We cannot place super() in try. You will get a compiler error... super must the first statement in the constructor.
If super() was allowed to be put in try block, this would have been the only solution. So b is incorrect. We are left with C

Regards

Srilatha Reddy
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pradeepon, if you copy a question from a book or mock exam, then please quote your sources.
 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jesper
I have taken this question from a mock exam at http://www.javablackbelt.com/QuestDefListing.wwa



I am not agree with Srilatha Reddy because it is possible in java IF we place a throws clause (which throws super class of Exception declare in above super class) at the declaration of subclass constructor.But this is not given as the option in above question.

import java.io.IOException;

class Number1
{
public Number1() throws IOException
{
throw new IOException();
}
}

class Number1E extends Number1
{
Number1E()throws Exception
{}
}
[ January 04, 2008: Message edited by: pradeepdon singh ]
reply
    Bookmark Topic Watch Topic
  • New Topic