views:

625

answers:

3

I have an abstract class:

type
  TInterfaceMethod = class abstract
  public
    destructor Destroy;         virtual; abstract;
    function GetBasePlan: Word; virtual; abstract;
    procedure CountBasePlan;    virtual; abstract;
    procedure Calculate;        virtual; abstract;
    procedure PrepareForWork;   virtual; abstract;
  end;

and a derived class:

type
  TFogelMethod = class(TInterfaceMethod)
  private
    matrix: TFogelMatrix;
    BasePlan: Word;
  public
    constructor Create(var matr: TFogelMatrix);
    procedure Calculate;
    function GetBasePlan: Word;
    procedure CountBasePlan;
    procedure PrepareForWork;
    destructor Destroy;
  end;

The question is, can I place the implementation of GetBasePlan and CountBasePlan methods into base class, make them only virtual - not abstract as now - and also place member BasePlan there? So, can I do this:

type
  TInterfaceMethod = class abstract
  private
   BasePlan: Word;
  public
    destructor Destroy;         virtual; abstract;
    function GetBasePlan: Word; virtual; 
    procedure CountBasePlan;    virtual; 
    procedure Calculate;        virtual; abstract;
    procedure PrepareForWork;   virtual; abstract;
  end;

In case I can do it, will it be good from the point of view of object-oriented design, and how can I exactly access this member from derived classes?

+7  A: 

Yes you can. Abstract classes are classes and they can have implementations.

By adding the abstract keyword to a class, you prohibit the class to be instantiated. It does not require to have any abstract methods.

A class with absract methods can be instantiated, but this result in a warning at compile time and an exception if the method is called.

Interfaces have no implementation, they have to be implemented by classes (which can be abstract by the way).

Gamecat
+6  A: 

Just to add to Gamecat's answer, not only can you, but you should put your common code there.

Tim Jarvis
Thanks for the addition ;-)
Gamecat
how to access the base class private members from derived class? (above)
chester89
The way is found - I just need to declare base class private members as protected:)
chester89
You can access private members of a class that defined in the same unit. To avoid this use strict private (and strict protected)
Gamecat
Chester, Thats specifically what the protected specifier is for, so that your derived classes can use them, in fact it kinda implies that you expect derived classes to use that protected instance data.
Tim Jarvis
in fact Destroy should be always be overidden as it is virtual in TObject.destructor Destroy;override;
Gerry
A: 

I don't have an answer for you, but I'd just like to congratulate you for being the 400th question to be tagged "Delphi".

We Delphi users are building up quite a community here at stackoverflow.

lkessler
I only see 399 questions so tagged.
Rob Kennedy
Up to 403 now. ;-)
Nick Hodges
A number of people at stackoverflow.uservoice.com have mentioned inconsistencies in the number of Tags reported depending on where you see it on Stackoverflow. They claim they get synched overnight, but I still suspect a problem. If nothing else, I've declared Chester's our unofficial 400th post.
lkessler
I verified that pages showing "You're browsing through nnn questions tagged ttt" are correct. Whereas individual question pages like this one showing "You're viewing a single question tagged ttt × nnn" are wrong. The http://stackoverflow.com/tags page is also wrong. So yours was the 400th.
lkessler
I believe this is because of the [nolock] hints in queries on SQL Server here at SO - sometimes it gets inconsistent results. The correct ones I guess are queries without it (so it gets consistent state of database).
Fabricio Araujo