IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Cross Ref Guide
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesJavaOOP Basics  Print This     

Cross Ref > OOP Basics

By Mike Prestwood

Java versus Delphi: A side by side comparison between Java and Delphi.

 
OOP Basics
 

Some languages support object-based concepts such as Paradox, Access, and VB Classic. Other languages have OO extensions and fully support object orientation in a hybrid fashion (such as C++ and Dephi for Win32). Finally, some lanages such as C#, VB.Net, Prism, and Java are entirely written in OO. Meaning, every line of code written must occur within a class).

Base Class

[Other Languages] 

Languages Focus

When you create a class, it is either a base class or inherits from another class. Some languages require all classes to inherit from a common base class and some do not.

Java:   Object

The Object class is Java's single base class all classes ultimately inherit from.

Syntax Example:
public class Cyborg {
}

or you can specify the base class (or any other class):

public class Cyborg extends Object {
}
Delphi:   TObject

In Delphi programming language (Object Pascal), all classes ultimately inherit from the base class TObject.

Syntax Example:
//Specify both namespace and class:
TCyborg = class(System.TObject)
end;
  
//Use shortcut alias:
TCyborg = class(TObject)
end;
  
//None, default is System.TObject
TCyborg = class
end;




Class..Object

[Other Languages] 

Languages Focus

In short, a class is a data type, and an object is an instance of a class type. A class has methods (routines), properties (member variables), and a constructor. The current values of the properties is the current state of the object. The UML is one of the diagraming disciplines that allows you to document the various changing states of a series of objects.

Java:   class..new

Unlike languages such as C++ and Object Pascal, every line of code written in Java must occur within a class.

Syntax Example:
//Declare class.
public class Cyborg {
  //Fields.
  private String cyborgName;
  private int age;
 
  //Constructor.
  public Person() {
  cyborgName = "unknown";
  age = 0;
  }
}
//Create object from class.
Cyborg p = new Cyborg();
p.getClass(); //From the Object base class.
Delphi:   class..end..Create

Declare your class in the Interface section. Then implement the class in the Implementation section. To create an object instance, call the class constructor (usually named Create). Since Delphi does not have a garbage collector, you have to also free the object usually with either Free or FreeAndNil.

Syntax Example:
//Interface section:
TCyborg = class(TObject)
public
procedure IntroduceYourself;
end;
 
//Implementation section;
procedure TCyborg.IntroduceYourself;
begin
ShowMessage('Hi, I do not have a name yet.');
end;
 
//Some event like a button click:
var
T1: TCyborg;
begin
T1 := T1.Create;
T1.IntroduceYourself;
  FreeAndNil(T1);      //Be sure to clean up!
end;




Inheritance

[Other Languages] 

The concept of a class makes it possible to define subclasses that share some or all of the main class characteristics. This is called inheritance. Inheritance also allows you to reuse code more efficiently. In a class tree, inheritance is used to design classes vertically. (You can use Interfaces to design classes horizontally within a class tree.) With inheritance, you are defining an "is-a" relationship (i.e. a chow is-a dog). Analysts using UML call this generalization where you generalize specific classes into general parent classes.

Java:   extends ParentClass

Simple syntax example of class inheritance.

Syntax Example:

In the following example, a terminator T-600 is-an android. 

public class Android {
}
 
public class T-600 extends Android {
}
Delphi:   =class(ParentClass)

In Delphi, you use the class keyword followed by the parent class in parens. If you leave out the parent class, your class inherits from TObject.

Syntax Example:

In the following example, a terminator T-600 is-an android. 

TAndroid = class
end;
 
T-600 = class(TAndroid)
end;




Member Event

[Other Languages] 

A custom event added by a programmer to a class. Custom created events need to be processed, usually by an event dispatcher within a framework.

[Not specified yet. Coming...]
Delphi:  "Member Events"

In Delphi, member events are essentially properties of the type method pointer.

More Info / Comment




Member Field

[Other Languages] 

Also known as a Class Field.

A class variable defined with a specific class visibility, usually private visibility. A member property is different than a member field. A member property uses a member field to store values through accessor methods (getters and setters). For example, it is common to use a private member field to store the current value of a property. The current values of all the class member fields is the current state of the object.

Languages Focus

What modifiers apply to member fields, if any? Typical member field modifiers include scope modifiers (private, protected, etc.) and read-only. Can you initialize the value of a member field when declared ensuring a default value?

Java: 

In Java, you can set the scope of a field member to public, protected, or private. Additional modifiers are static, abstract, final (assign only once), strictfp (strict floating point values) transient (do not save to persistent storage), and volatile (all threads see same value).

You can initialize member fields as in:

int age = 0;
More Info / Comment
Delphi: 

In Delphi, it is common to start all member fields with "F" as in FName and FAge. You can initialize the value of member fields too.

Delphi member fields do not support static data. The workaround is to use the hybrid nature of Delphi and use a unit variable (a variable declared in the implementation section of a unit) and then access the unit variable with a member property.

Delphi doesn't support setting a member field to read-only. However, you can accomplish the task with a strict private member field and a read-only property.

Syntax Example:
TCyborg = class(TObject)
private
  FSerialNumber: String='A100';
public
FCyborgName: String;
FCyborgAge: Integer=0;

  FSeriesID: Integer=100;
end;




Member Method

[Other Languages] 

Also known as a Class Method.

A code routine that belongs to the class or an object instance (an instance of the class). Methods that belong to the class are called class methods or static methods. Methods that belong to an object instance are called instance methods, or simply methods.

When a method returns a value, it is a function method. When no value is returned (or void), it is a procedure method.

Methods frequently use method parameters to transfer data. When one object instance calls another object instance using a method with parameters, you call that messaging.

[Not specified yet. Coming...]
Delphi:   procedure, function

Delphi uses the keywords procedure and function. A procedure does not return a value and a function does.

Syntax Example:
//Interface section:
TCyborg = class(TObject)
public
  procedure IntroduceYourself;
end;
 
//Implementation section;
procedure TCyborg.IntroduceYourself;
begin
  ShowMessage('Hi, I do not have a name yet.');
end;
 
//Some event like a button click:
var
  T1: TCyborg;
begin
  T1 := T1.Create;
  T1.IntroduceYourself;
end;




Member Modifier

[Other Languages] 

Languages Focus

Traditional private, protected, public, etc. member modifiers are documented under the member visibility topic of the Cross Reference Encyclopedia. With member modifiers here, we address additional member modifiers such as method and field modifiers.

[Not specified yet. Coming...]
Delphi:  "Member Modifiers"

Specify Delphi member modifiers as follows:

reintroduce; overload; [binding modifier]; [calling convention]; abstract; [warning]

The binding modifiers are virtual, dynamic, or override.

The calling conventions are register, pascal, cdecl, stdcall, or safecall.

The warnings are platform, deprecated, or library.

Additional directives include reintroduce, abstract, class, static, overload, and message.

Syntax Example:
TCyborg = class(TObject)
public
  procedure Speak(pMessage: String); virtual;
end;
 
TSeries888 = class(TCyborg)
public
  procedure Speak(pMessage: String); override;
end;




Member Property

[Other Languages] 
[Not specified yet. Coming...]
Delphi:   property..read..write

Delphi uses a special property keyword to both get and set the values of properties. The read and write keywords are used to get and set the value of the property directly or through an accessor method. For a read-only property, leave out the write portion of the declaration.

You can give properties any visibility you wish (private, protected, etc). It is common in Delphi to start member fields with "F" ("FName" in our example) and drop the "F" with properties that manage member fields ("Name" in our example).

Syntax Example:
TCyborg = class(TObject)
private
  FCName: String;
public
  property CyborgName: String read FCName write FCName;
end;




Member Visibility

[Other Languages] 

General Info: Class Visibility Specifiers

In OOP languages, members of a class have a specific scope that indicates visibility. Standard visibility includes private, protected, and public. Private members are usable by the defining class only (fully encapsulated). They are invisible outside of the class except by friendly classes. Protected members are usable by the defining class and descendant classes only (plus friendly classes). Public members are usable wherever its class can be referenced.

Languages Focus

Traditional member visibility specifiers for fully OOP languages are private, protected, and public. Many modern OOP languages implement additional member visibilities.

Additional member modifiers are documented under the Member Modifiers topic.

[Not specified yet. Coming...]
Delphi: 

In Delphi, you group member declarations as part of defining the interface for a class in the Interface section of a unit.

Up until D2005, private and protected were not implemented strictly. Starting with D2005, a traditional strict versions of OOP are supported using the strict keyword. OO purist will want you to use strict private over private and strict protected over protected. I suggest you follow that advice until you both fully understand the differences and have a specific need.

Delphi offers a special published specifier which is the same as public members but runtime type information (RTTI) is generated.

Syntax Example:
TCyborg = class(System.Object)
private
//Don't use accept when you really want private friendly members.
strict private
//Use as your default private members.
  FName: String;
protected
//Don't use accept when you really want protected friendly members.
strict protected
//Use as your default protected members.
public
  

published
  //RTTI Info

end;




Sales Website: www.prestwood.com Or visit our legacy sales site: 
legacy.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]