views:

201

answers:

3
+1  Q: 

Delphi Child Class

Hi everyone, it's a dumb question i think..But.. When a declare a child class of a other class in Delphi, is the childs gets directly the parents methods? explanations: A class named 'P' is parent of a class named 'C', the 'P' class has a method called 'Mth'. Is it possible to call 'C.Mth' or do i notice something in the declaration of 'C' (with the constructor maybe?)..

The question is the same with variables..

I hope to be clear enough.. thanks a lot for responses...

+5  A: 

A child class inherits all the protected, public and published properties, functions and procedures of its parent classes.

It can call them directly, without any special syntax, providing the child class has not overridden them.

For example:

type
  P = class
  public
    procedure Mth;
  end;

  C = class(P)
  public
    procedure Foo;
  end;

// ... implementation ...

procedure C.Foo;
begin
  Mth; // Calls the P.Mth procedure.
end;
Jon Benedicto
A: 

Yeah, you can invoke the parent methods as if they belong to the child. That's part of the power of OO hierarchies.

Jorge Córdoba
+7  A: 

Yes. This is called "inheritance." This means that all the attributes of the parent class are "inherited" by the child class. If you do nothing to change anything in the child class (override virtuals, add fields, add methods, etc...) then the child class would function identically to the parent class. You can pass the child class to other functions that expect the parent class since, by virtue of inheritance, the child shares all the qualities of the parent.

Allen Bauer
+1 with the addition that inherited methods and fields can only be accessed if not declared as private.
Smasher