Re: [General]acces of members of subclass



Lew wrote:

Daniel Moyne wrote:
Apparently subclasses are designed for private members ? [sic]

RedGrittyBrick wrote:
I'm sorry I've no idea what you mean by this. You can have subclasses
which declare additional public fields (I find your "member" terminology
a little confusing).

Daniel was mistaken. Subclasses are not "designed for private members".

Changing the examples, since "My_Class" violates the naming convention by
having an underscore and having "Class" as a class name part is silly,

public class Super
{
public void foo() { ... }
}

public class Sub extends Super
{
public void bar() { ... }
}

later:

Super sup = new Sub();

This has no difficulty - perfectly legal, with public members and all.
However:

sup.bar();

is illegal, not because 'bar()' is public, but because 'bar()' is not
declared
for type 'Super'. The variable 'sup' is of type 'Super', therefore cannot
access anything but members of 'Super'.

So, Daniel, the correct explanation is that superclasses are not
"designed" to
access subclass members, irrespective of accessibility. Superclass
variables can access only superclass members.

That is a compile-time rule. The run-time dispatch through the superclass
member to overridden subclass functionality is a separate matter.

So Lew as superclasses cannot acces subclass members (in you example you
just provide methods "bar" in the subclass sub but no members) there is no
reasons to declare them as public and then only private makes sense ; am I
correct ?

Now coming back to method "bar" I have seen in the net some java code based
on method :
getMethod
and then invoke
that would allow to access this method from outside of the subclass but I
could not find this back for this topics.

The access of method of subclass was not on my original post ; it was only
about access to members of subclass "Sub".
Thanks


.



Relevant Pages

  • Re: Help with design please
    ... entities from the problem space that are cohesive. ... disconnects in how the solution is viewed by different team members. ... >>relationships with the arrow on the superclass end. ... >>relationship the union of subclass members must be a complete set of the ...
    (comp.object)
  • Re: UML "OR" Composition Question
    ... "Each superclass instance in D must have exactly one ... from the fact that the union of subclass members must be a complete set ... of the superclass members. ... known in data modeling as a 'direct instance' of the superclass. ...
    (comp.object)
  • Re: A hopefully interesting design question ...
    ... I will definitely look into the State Pattern ... AnswerBox: ... That is, members of OO ... it becomes obvious if one explicitly defines a subclass ...
    (comp.object)
  • Re: A hopefully interesting design question ...
    ... void RequestImputable; // from EditedAnswerBox ... AnswerBox: ... That is, members of OO ... it becomes obvious if one explicitly defines a subclass ...
    (comp.object)
  • Re: UML "OR" Composition Question
    ... the union of members of sibling subsets must be a complete ... > instantiate a superclass without specifying a leaf subclass as well.] ... Since the diagram is only a view to the model, it needs not to show the ... siblings of Y, Z. Additionally, these additional siblings aren't ...
    (comp.object)