aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding Constructors? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding Constructors?" Watch "Overriding Constructors?" New topic
Author

Overriding Constructors?

Jim Petersen
Ranch Hand

Joined: Jul 24, 2001
Posts: 241
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

- Jim Petersen <br />SCJP2<br />SCWCD<p>- but then again, I could be wrong...
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

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


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2579

My vote goes to poorly worded (incorrect???) question, what you say is correct, AFAIK. Somebody with more insight please....
- Manish
Wasim Ahmed
Ranch Hand

Joined: Jan 31, 2001
Posts: 90
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

Joined: Jul 24, 2001
Posts: 241
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

Joined: Sep 22, 2001
Posts: 2579

Carl ???
Overloading is well understood, but overriding???
Are you sure?
- Manish
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2579

Hey carl, you deleted that post.
Makes me look silly.
Nah, not fair
- Manish
Ruby V
Greenhorn

Joined: Sep 27, 2001
Posts: 13
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.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
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

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Nain Hwu
Ranch Hand

Joined: Sep 16, 2001
Posts: 139
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

Joined: Sep 22, 2001
Posts: 2579

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

Joined: Sep 27, 2001
Posts: 13
Yes, I agree with Manish that methods can be overloaded in the same class and also in its sub classes.
Ruby V
Greenhorn

Joined: Sep 27, 2001
Posts: 13

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

Joined: Jul 24, 2001
Posts: 241
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
Ted J Schrader
Greenhorn

Joined: Oct 10, 2001
Posts: 28
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

Joined: Sep 16, 2001
Posts: 139
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

Joined: Sep 22, 2001
Posts: 2579

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).]
Deepak B
Greenhorn

Joined: Jun 19, 2001
Posts: 17
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){}
}

Nancy Tang
Greenhorn

Joined: Oct 10, 2001
Posts: 8
Can constructor throw exception? What is the rule about the constructor of subclass to specify the exception?
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2579

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).]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Overriding Constructors?