Re: [General]acces of members of subclass



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
.



Relevant Pages

  • VB.NET101 - Constructors and Methods (2)
    ... argument constructor is never printed. ... Sub Main ... Public Sub Sum ... Console.WriteLine("Value mSumOfTwo after entering empty constructor ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Error trying to display array member
    ... instantiates the array within the constructor, ... Inherits CTwoDimensionalShape ' A triangle is a 2d shape ... Public Sub New ... Public Sub New ...
    (microsoft.public.dotnet.languages.vb)
  • Re: what is the RIGHT THING to make a constructor of a subclass, using super()
    ... If you are left with that requirement as a necessary or desirable part of the design, then the usual approach is probably to write a static method in the class that you call as an argument to the call to super() from your constructor. ... public Customer(String firstName, String lastName) { ... public class SlashSeparatedCustomer extends Customer { ...
    (comp.lang.java.programmer)
  • Re: what is the RIGHT THING to make a constructor of a subclass, using super()
    ... If you are left with that requirement as a necessary or desirable part of the design, then the usual approach is probably to write a static method in the class that you call as an argument to the call to super() from your constructor. ... public Customer(String firstName, String lastName) { ... public class SlashSeparatedCustomer extends Customer { ...
    (comp.lang.java.programmer)
  • Re: VB.NET-101 Constructors and Methods
    ... disappeared after adding the empty constructor. ... 'Public Sub New ... 'Public Sub New(ByVal first As Integer, ByVal second As Integer, ByVal ... This gives each of them a unique "signature", ...
    (microsoft.public.dotnet.languages.vb)