Re: [General]acces of members of subclass
- From: Lew <com.lewscanon@lew>
- Date: Sun, 27 Jul 2008 17:48:54 -0400
Daniel Moyne wrote:
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 ?
Good question.
First a terminology point: both methods and fields are "members" of a class, or instance. Fields are also sometimes called "attributes", especially when associated with getter and setter methods ('getFoo()' and 'setFoo( value )'). Anyhow, the method 'bar()' in my example is a member of 'Sub', and makes the same point that a field member would - that subclass members can and often should be public, or protected, not necessarily private.
So 'private' access makes sense for the explicit purpose of hiding implementation, one of those Good Things you hear about. However, some members, usually just methods and not fields, will have wider access than that. If you want to make a field public, it is legal if not always desirable to do so.
The reason to declare a subclass member 'public' is to give it wide access to all users, but those users will have to declare their variables *to the subclass type* to benefit from those members.
So in the upthread example:
===================
public class Super
{
public void foo() { ... }
}
public class Sub extends Super
{
public void bar() { ... }
}
public class Client
{
public void main( String [] args )
{
Super sup = new Sub();
sup.bar(); // illegal!
}
}
===================
the class 'Client' made a mistake - it used a superclass variable but it wanted subclass functionality. It is not enough that the method is public - the variable has to be aware of it, too. Like this:
===================
public class Client
{
public void main( String [] args )
{
Sub sub = new Sub();
sub.bar(); // legal!
}
}
===================
Variables are doorkeepers to the real objects, not the objects themselves. They only let through the references that are on their list, and if you're not on the list, you don't get in.
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.
You are probably referring to polymorphism and method overrides. This would apply to the method 'foo()' in the example, but not to fields. Once the compile-time doorkeeper lets the call in, say to 'foo()', the actual object behind the door executes the call. Because the object's actual run-time type is 'Sub', then it executes the subclass's version of the method.
This does not work for 'bar()' because the superclass variable doesn't have awareness of that call to even let it through.
The access of method of subclass was not on my original post ; it was only
about access to members of subclass "Sub".
The exact same logic applies to member fields as member methods - the variable type must be aware of the member in order to refer to the member. Supertype variables cannot refer to subtype members. If you need awareness of the subtype member, then the client code would declare the variable of the subtype.
This is commonly done with 'ArrayList' as a subtype of 'List', for example. Most of the time, one uses the 'List' type, but every so often one needs the specific characteristics of the subtype. At those times, the client declares its variable 'ArrayList', or some other suitable specific type.
--
Lew
.
- Follow-Ups:
- Re: [General]acces of members of subclass
- From: Daniel Moyne
- 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
- [General]acces of members of subclass
- Prev by Date: Re: [General]acces of members of subclass
- Next by Date: Re: [NetBeans]changing menuitems
- Previous by thread: Re: [General]acces of members of subclass
- Next by thread: Re: [General]acces of members of subclass
- Index(es):
Relevant Pages
|