• 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

Another doubt from Abhilash's exam.

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey all,
Can someone please help me understand these two questions.
Question 72.
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 Laungage with the above definition of the base class.
-----------------------------------------------------------------The correct answer to this one is 3.I could not understand why the method in answer 1 will not work!!!
Question 73.
Read the following piece of code carefully. (This is an extention to question 72.)
public abstact class Question73 extends Question72
{
public abstract void method();
}
An attempt to compile the above class definition
1.will cause a compiler error - non-abstract classes cannot be extended to abstract classes.
2.will cause a compiler error - a constructor must be provided which may or may not throw an IOException
3.will cause a compiler error - a constructor must be provided
which must throw an IOException or one of its super types.
4. will not cause any compiler error. The class definition is perfectly legal.
-----------------------------------------------------------------
The correct answer is 3 again.But why?
Waiting for help.
Thanks,
Gazala.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First one, you can't write super other than the first line.
Second question, your default constructor implicitly calls non-arg super();
Hope this helps. (Ignore my first reply if therw was.)
--Howard
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to write a constructor that throws and possible Exceptions that are thrown by the supers constructor. Even though the super has a no-arg constructor, not throwing the Exception is invalid and won't compile. You can't try catch it either, since super needs to be the first call in the sub class's constructor.
- David
 
Gazala Bohra
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Howard , I sure got the first one, but the second one is still not clear, can you elaborate on that.It does call the no arguement constructor of the super, that I understand but what is not clear is this statement of the answer
" must provide a constructor which must throw an IOException or one of its super classes"
Why so?
Thanks,
Gazala
 
reply
    Bookmark Topic Watch Topic
  • New Topic