• 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

Overriding Constructors?

 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I got this question in a mock exam...
(Incidentally from the educational services of a very large firm that sponsors the Java and starts with S..)
Which statement about using constructors while subclassing is true?
A
If the subclass constructor is overloaded, the superclass constructor must be overloaded as well.
B
If the superclass constructor is not overridden, the subclass must provide a constructor that overrides the superclass constructor.
C
If the subclass constructor is overridden with a constructor that takes arguments, the superclass constructor must include the same argument list.
D
If the superclass constructor is overridden with a constructor that takes arguments, the subclass must call the superclass constructor providing the correct arguments.
Answer: D
Now my problem is that I worked on the idea that constructors are not inherited as per normal methods and therefor can't be overridden. I can see D is right once you take out the overriding bit. Am I right is this a badly worded (ie confusing) question? or have I missed something on the way regarding constructors?
thanks Jim
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's badly worded. I first thought to replace "overridden" with "overloaded," but that would make D a false proposition too.
Your understanding is just fine.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My vote goes to poorly worded (incorrect???) question, what you say is correct, AFAIK. Somebody with more insight please....
- Manish
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that Constrctor can not be "overridden". This is a very bad worded question. How do you override (Const..) by adding argument to the signature. I think overriding MUST have the same signature.
 
Jim Petersen
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael, Manesh, Wasim - Thankyou!
Once I clear this exam I'll post a suggestion to the friendly folks at Sun and suggest this question be ammended. At the moment it will have to go on the 'to do' list...
rgds - Jim
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carl ???
Overloading is well understood, but overriding???
Are you sure?
- Manish
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey carl, you deleted that post.
Makes me look silly.
Nah, not fair
- Manish
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I always thought :
1)Methods are overloaded in the same class and methods can be overridden in its subclass.
2) Constructors also follow the above rule.
Does anyone agree?
Ruby.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manish Hatwalne:
Hey carl, you deleted that post.
Makes me look silly.
Nah, not fair
- Manish


LOL I read it too fast and realized that I was miss reading! Sorry
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ruby,


2) Constructors also follow the above rule.


No. As everybody has already said loud and clear, constructor
is not a member of a class. So, overriding
does not apply.
[This message has been edited by Nain Hwu (edited October 10, 2001).]
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ruby V


1)Methods are overloaded in the same class and methods can be overridden in its subclass.


Methods can be overloaded across classes.
See JLS 8.4.7 Overloading -
"If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but different signatures, then the method name is said to be overloaded. "
- Manish
 
Ruby V
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I agree with Manish that methods can be overloaded in the same class and also in its sub classes.
 
Ruby V
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nain Hwu
quote:
No. As everybody has already said loud and clear, constructor
is not a member of a class. So, overriding
does not apply.
----------------------------------------
I do think the constructors and methods follow the same rule and hence the wordings used (like overridden) applys to a constructor also.
Hence, overriding a superclass constructor means that you have a constructor in the subclass ,which has the same signature as that of the superclass.
 
Jim Petersen
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ruby
A constructor is a special type of method used to set the initial state of an object when the object is created with the new operator. However constructors must obey the following conditions
- Only public private protected or default access modifiers can't be static final etc
- Constructors can only have the same name as the class name
- Constructors cannot return a value else this makes them a normal method
- Contructors can only be called with the new operator
- as with methods constructors can be overloaded, but to quote Mughal and Rasmussen (pp 189) "Constructors cannot be overridden"
they can be chained however.
hope this helps Jim
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In regards to method overriding:
According to Mughal and Rasmussen, one requirement for a subclass to override a non-static method inherited from the superclass is that "the new method definition [defined in the subclass] must have the same method signature (i.e. method name and parameters) and the same return type." (emphasis in original).
As Jim has stated, a constructor must have the same name as it's enclosing class. Therefore, overriding a superclass constructor is impossible because the constructor names are different.
In short, the term "overriding" doesn't apply to constructors because of their status as "special" methods.
 
Nain Hwu
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ruby,
Just to assure you constructor is no a member and therefore
can not be overriden, here is what is said in JLS 8.2:


Constructors, static initializers, and instance initializers are not members and therefore are not inherited.



[This message has been edited by Nain Hwu (edited October 11, 2001).]
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ruby???
I thought that was already clear!!!
Constructors *can not* be overridden, they are not inherited either. The only reason this got us somewhat confused because it came from Sun, as Jim mentioned.
- Manish

[This message has been edited by Manish Hatwalne (edited October 11, 2001).]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends
Constructor can not be overriden but That can be overloaded in a class.
like
class A
{
A(){}
A(int c){}
A(int c,char p){}
}
class B extends A{
B{int p){}
B(int c,chat q){}
}

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can constructor throw exception? What is the rule about the constructor of subclass to specify the exception?
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

By Deepak B -
Constructor can not be overriden but That can be overloaded in a class.


Deepak???
Yes, very much so! We all agree on that. The issue of discussion here was regarding overriding, because of the poorly worded question.
- Manish
[This message has been edited by Manish Hatwalne (edited October 12, 2001).]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nancy
A constructor has to declare the same checked exceptions that the invocation of the super constructor may produce. Also can declare any others not mention in the base constructor.
A constructor can't catch those exceptions thrown by the invocation of a base constructor.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic