Re: acces of members of subclass
- From: Eric Sosman <Eric.Sosman@xxxxxxx>
- Date: Mon, 28 Jul 2008 15:27:49 -0400
Lew wrote:
On Jul 28, 1:37 pm, Mark Space <marksp...@xxxxxxxxxxxxxx> wrote:Lew wrote:Daniel Moyne wrote:Er, lots of classes have no zero-argument constructor, so Daniel is- Sub may have itw own typical constructor with its own parameters'super()' is always present, if only implicitly. If you do not provide
where of
course super(...) is not present.
one, the compiler inserts a call to the no-argument 'super()' for you.
correct. Any given sub-class, in the general case, cannot rely on the
presence of a super() call.
From the JLS:If a constructor body does not begin with an explicit constructor invocation and the
constructor being declared is not part of the primordial class Object, then the
constructor body is implicitly assumed by the compiler to begin with a superclass
constructor invocation "super();", an invocation of the constructor of its direct
superclass that takes no arguments.
The call to super() is *always* present. Any given class, in the
general case, can rely on the presence of super() call. Daniel and
you are not correct.
No, Mark is right, and Daniel and Lew are wrong.
Lew (and the JLS) are right in saying that the compiler inserts a
call to super() if the constructor does not begin with a call to one
of its superclass' constructors. But that does not guarantee that the
superclass actually *has* a no-arguments constructor that can be called
as plain super(). If it doesn't, there'll be a compile-time error.
Try it!
class Super {
// Explicit constructor suppresses automatic creation of
// a no-arguments constructor:
Super(String foo) {
System.out.println("Super: foo = " + foo);
}
}
class Sub extends Super {
Sub(String foo) {
// Implicit super() call inserted here; error message
// because Super has no super()
System.out.println("Sub: foo = " foo);
}
}
--
Eric.Sosman@xxxxxxx
.
- Follow-Ups:
- Re: acces of members of subclass
- From: Lew
- Re: acces of members of subclass
- From: Lew
- Re: acces of members of subclass
- References:
- [General]acces of members of subclass
- From: Daniel Moyne
- Re: [General]acces of members of subclass
- From: RedGrittyBrick
- Re: [General]acces of members of subclass
- From: Lew
- Re: [General]acces of members of subclass
- From: Daniel Moyne
- Re: [General]acces of members of subclass
- From: Lew
- Re: [General]acces of members of subclass
- From: Daniel Moyne
- Re: [General]acces of members of subclass
- From: Lew
- Re: [General]acces of members of subclass
- From: Mark Space
- Re: acces of members of subclass
- From: Lew
- [General]acces of members of subclass
- Prev by Date: Scrolling JList to Bottom
- Next by Date: Re: acces of members of subclass
- Previous by thread: Re: acces of members of subclass
- Next by thread: Re: acces of members of subclass
- Index(es):
Relevant Pages
|