• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Constructor and Inheritance

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I though constructor are never inherited!!, According to me the answer should be 1), but again there is the limitation of keeping super on the first line.
Please help!!!
Read the following piece of code carefully.

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.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amish
Answer one is false because if the first line of a constructor is not this(xxx) or super(xxx) the compiler puts in a call to the default superclass constructor. So you can't call the super class constructor from within a try-catch block.
Answer 2 wont work because even if you dont explicitly call the base class constructor the call is still supplied by the compiler.
Given the requirment that you can't throw an exception from the new class' constructor and if you can't change the base class then it can't be done.
hope that helps
[ January 14, 2002: Message edited by: Dave Vick ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe helpful Amish:
Constructors are not inherited. But because they are chained together along the hierarquy up to Object, constructing an object of subclass will always throw the exceptions that the superclass throws because the base constructor will be called. That is why a constructor in the derived class must declare all the checked exceptions that the base class declare. Though, it can declare more, the ones that the construction of the proper derived object might throw.
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic