Re: [General]acces of members of subclass



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.

--
Lew
.



Relevant Pages

  • Re: Accessing private member via subclass
    ... public class Sub extends Super ... does not have access to the private members of 'Super'. ... "Members are either declared in the type, ...
    (comp.lang.java.programmer)
  • Re: Accessing private member via subclass
    ... public class Sub extends Super ... does not have access to the private members of 'Super'. ... "Members are either declared in the type, ...
    (comp.lang.java.programmer)
  • Re: Directory is Done!
    ... I got it twice;-) cuz I must have gotten the first round ... before the bouncing address. ... Super good job on it, ... We treasure all of our members ...
    (rec.crafts.textiles.quilting)
  • Re: [General]acces of members of subclass
    ... public class Sub extends Super ... This has no difficulty - perfectly legal, with public members and all. ... variables can access only superclass members. ... member to overridden subclass functionality is a separate matter. ...
    (comp.lang.java.help)
  • Insert Parent-Child related data using OPENXML
    ... I have two classes Groups and Members and when I serialize (using ... public class Members ... INSERT INTO GroupUsers ...
    (microsoft.public.sqlserver.xml)