Re: [General]acces of members of subclass
- From: Lew <com.lewscanon@lew>
- Date: Mon, 28 Jul 2008 08:46:17 -0400
Daniel Moyne wrote:
Great Lew for all theses clear explanations :
(1) first of all sorry for the use of "member" which is a direct translation
from French word "membre" that is for you "field", the other animals being
called "méthodes" in French.
The JLS is written in English, and it defines "member" to mean both fields and methods. I speak a little French and I thought "membre" was a direct translation of the English "member", and many people make the same confusion in English that you did, so I suspect it's not a translation problem.
(2) then from what you very well explain I want to go back to this point
using this piece of code :
===================
public class Client
{
public void main( String [] args )
{
Super sub = new Sub();
sub.bar(); // legal!
*Not* legal in the example I gave.
Sub sub1 = new Sub();
sub1.bar(); // legal!
}
}
===================
So with theses instanciations you have 2 objects and consequently you are
still not able to access "bar()" member of object "sub" which is the main
question of my original post ; if we want to go a little bit further am I
right saying that :
Yes, you are, if you use a subclass-typed variable.
- Sub class as an extended Super class may have a "super constructor" of its
The term "super-constructor" is not precise, but usually means the constructor of a superclass.
own with its own parameters if of course Super has one (Super may have more
than one constructor and hereafter I talk only about the one I am
interested in) ; such "super constructor" must start with super(...)
That is incorrect verbiage. Each constructor that invokes a non-zero-argument super-constructor must use a 'super()' invocation to do so.
"Super-constructor" is the constructor invoked by the 'super()' call.
- Sub may have itw own typical constructor with its own parameters where of
course super(...) is not present.
'super()' is always present, if only implicitly. If you do not provide one, the compiler inserts a call to the no-argument 'super()' for you.
- both constructors are present in class Sub like this :
===================
public class Sub extends Super
// Sub class extends class Super
{
// super constructor of Sub class
Sub(parameters) {
//question 1 : all parameters are to be in coherency with constructor of
// class Super ?
Only if you say so. There is no requirement for that.
//question 2 : can we say that here that this constructor overwrites Super
"overwrite" is not a term in Java parlance in this context. What do you mean?
Perhaps you meant "override"?
In any event, the answer is "no".
// class constructor (the one in coherency with parameters) ?
super(...); //first line of constructor
You need to provide better examples. The ellipsis '...' hides the details that would make the example make sense.
...;
} // subclass constructor of Sub class
Sub(other_parameters) {
//other_parameters have normally no connections with fields of class Super
...;
} ...;
}
===================
So at the end of the day when you do :
Super sub = new Sub(parameters);
Sub sub1 = new Sub(other_parameters);
you end up with ENTIRELY different objects that are like "a" and "b" in :
int a=1;
String b="eee":
They are "like 'a' and 'b'" in what way?
It is true that the objects are distinct. That is also true if you define
Object a = new Object();
Object b = new Object();
If by that shout of "ENTIRELY" you mean that there is no relationship, that is not true. In your example it would be legal to set 'sub = sub1;' It would not be legal to say 'a = b;'
--
Lew
.
- Follow-Ups:
- Re: [General]acces of members of subclass
- From: Mark Space
- Re: [General]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
- [General]acces of members of subclass
- Prev by Date: Re: where is the java preferences file located
- Next by Date: Trash appears in JSP page upon declaring error page
- Previous by thread: Re: [General]acces of members of subclass
- Next by thread: Re: [General]acces of members of subclass
- Index(es):
Relevant Pages
|