tags:

views:

228

answers:

4

Well,I have a parent class with a nested class declared in the "protected" tab with a protected class variable.In another unit I have a child class,which inherits from the parent class.When I try to access something protected/public from the parent class -it works,but when I try to access something protected from the nested class,it doesnt work.

type
  TParent = class(TObject)

  protected
    class var x:integer;
    type
      TNested = class(TObject)

        protected
          class var y:integer;
    end;
end;

My code in the child class:

x := 10; //works
y := 10; //undeclarated idenitifier 'y'.
TNested.y := 10; //undeclarated idenitifier 'y'

the declaration of the child class is:

type
  TChild = class(TParent);

How do I access y?

+3  A: 

y:integer is a protected field of TNested class, ie. can be only used by TNested and it's own inherited classes.
You probably may use TNested from TParent but this is beacause in Delphi you may have greater access than it should be if calling from the same unit. So TParent and TNested are in the same unit, so you may call TNested protected data from TParent. But since TChild is in different unit than TNested, it is impossible.

smok1
Not what is been asked here. TNested is a nested class (available in D2006? onwards), not another class in the same unit. It is referred to as TParent.TNested
Gerry
@Gerry, the issue here for me is calling protected elements of some class from outside of this class. No mater where this class resides, access from outside to protected fields of some class is somehow violationg encapsulation.
smok1
A: 

This will actuall work if TChild and TParent are in the same unit, because of the implicit friendship between classes within the unit.

To access y in your example, you'd need to do one of two things:

  1. Change the scope of y to public (or create a public property for it).
  2. Use y from a nested class that is derived from TNested.
Cobus Kruger
A: 
TParent.x := 10;
TParent.TNested.y := 10;
TOndrej
A: 

The example you are giving is using a nested class, not inheriting it.

Nested classed can be inherited in subclasses of the declaring class:

TSubParent = class(TParent)
protected
  type 
   TSubNested = class(TNested)
   public
     class var Z : integer;
   end;
end;
Gerry