Re: [General]acces of members of subclass
- From: Daniel Moyne <daniel.moyne@xxxxxxx>
- Date: Sun, 27 Jul 2008 22:29:15 +0200
Lew wrote:
Daniel Moyne wrote:So Lew as superclasses cannot acces subclass members (in you example you
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.
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
.
- Follow-Ups:
- Re: [General]acces of members of subclass
- From: Lew
- 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
- [General]acces of members of subclass
- Prev by Date: Re: adding a List of JTextField to the GUI
- Next by Date: Re: adding a List of JTextField to the GUI
- Previous by thread: Re: [General]acces of members of subclass
- Next by thread: Re: [General]acces of members of subclass
- Index(es):
Relevant Pages
|