{$MODE DELPHI} // Enable Class support type TFruit = Class(TObject) // This is an actual class definition : // Internal class field definitions - only accessible in this unit private isRound : Boolean; length : single; width : single; diameter : single; // Fields and methods only accessible by this class and descendants protected // Externally accessible fields and methods public // 2 constructors - one for round fruit, the other long fruit constructor Create(diameter : single); overload; constructor Create(length : single; width : single); overload; // Externally accessible and inspectable fields and methods published // Note that properties must use different names to local defs property round : Boolean read isRound; property len : single read length; property wide : single read width; property diam : single read diameter; end; // End of the TFruit class definition var apple, banana : TFruit; // Create a round fruit object constructor TFruit.Create(diameter: single); begin // Indicate that we have a round fruit, and set its size isRound := true; self.diameter := diameter; end; // Create a long fruit object constructor TFruit.Create(length, width: single); begin // Indicate that we have a long fruit, and set its size isRound := false; self.length := length; self.width := width; end; // Show what the characteristics of our fruit are procedure ShowFruit(fruit: TFruit); begin if fruit.round then Writeln('We have a round fruit, diameter = ',fruit.diam) else begin Writeln('We have a long fruit'); Writeln(' it has length = ',fruit.len); Writeln(' it has width = ',fruit.wide); end; end; Begin // Let us create our fruit objects apple := TFruit.Create(3.5); banana := TFruit.Create(7.0, 1.75); // Show details about our fruits ShowFruit(apple); ShowFruit(banana); end.