4D Blog

Home Product An intro to object-oriented programming in 4D: Classes

An intro to object-oriented programming in 4D: Classes

May 4, 2020

Product

Many of you have have been asking to be able to define an object type ever since the Object type became available. Thanks to object notation, many of you dream of having object functions.  Dream no more and say hello to classes in 4D v18 R3 project database! In this blog post, we’re introducing one of the most interesting concepts of object-oriented programming … along with a database example and a bonus video!

Example with “Person” class

Note: Starting with 4D v18 R3, all the HDIs will be hosted on GitHub. You can either clone the HDI or directly download the Zip file. Checkout this paragraph for details.

What’s a class?

A class is a user-defined data type that allows you to define the behavior of an object. It contains its own member functions that can be accessed and used when an instance of the class is created.

How to Create a class

You can create a class from the New menu in the toolbar or from the Explorer dialog. Now that the class editor is displayed, you can create the constructor and the functions.

How to create class functions?

To create a function, you must use the Function keyword followed by the name of the function. Then, you can pass parameters (as with methods).

Function methodName
  C_TEXT($0;$1)
  $0:="Hi "+$1

What is the class store?

You already know the ds command (which allows you to access the datastore). Now using the same logic, you have the cs command which returns the class store. All of the classes that you create are visible and available in the class store.

To create an object that is an instance of a class, you use the cs command and the new member function.

C_OBJECT($o)
$o:=cs.myClass.new()

What is a constructor?

A constructor is used to create and instantiate an object with default attributes. It’s called automatically when the object is initialized.

In the editor, simply write Class constructor. You can pass parameters to your constructor to initialize certain variables, just like you can do with a method using $1, $2, etc.

For the class:

Class constructor
  C_LONGINT($1)
  This.variable:=$1

For the method which instantiates an object of the class, pass the parameter to the new member function:

C_OBJECT($o)
$o:=cs.Class1.new(8)

Example

Let’s take a look at a concrete example with a “Person” class. I create:

  • a constructor,
  • a getFullName function,
  • a getAge function.

 

Here’s the content of my class:

Class constructor
  C_TEXT($1) // FirstName
  C_TEXT($2) // LastName
  C_DATE($3) // Birthdate
  This.firstName:=$1
  This.lastName:=$2
  This.birthdate:=$3
 
Function getFullName
  C_TEXT($0)
  $0:=This.firstName+" "+Uppercase(This.lastName)
 
Function getAge
  C_LONGINT($0)
  $0:=(Current date-This.birthdate)/365.25

Now, in a method, I create a “Person” object. Then, I read the attributes and execute the functions.

C_OBJECT($o)
$o:=cs.Person.new("John";"Doe";!1956-10-27!) // instantiate the object
 
$firstname:=$o.firstName // return John
$lastname:=$o.lastName // return Doe
$fullName:=$o.getFullName() // return John DOE
$age:=$o.getAge() // return 63

 

What’s next?

In the next blog post, we’ll discuss the notion of inheritance between classes.

Discuss

Tags Class, Programming, Project, Project database, v18 R3, v19

Latest related posts

  • May 20, 2025

    Automatic Refresh of OEM Build Licenses

  • May 16, 2025

    New class to handle incoming TCP connections

  • April 29, 2025

    Discover your AI-powered writing assistant in 4D Write Pro

Vanessa Talbot
Vanessa Talbot
• Product Owner •Vanessa Talbot joined 4D Program team in June, 2014. As a Product Owner, she is in charge of writing the user stories then translating it to functional specifications. Her role is also to make sure that the feature implementation delivered is meeting the customer need.Since her arrival, she has worked to define key features in 4D. She has worked on most of preemptive multi-threading new features and also on a very complex subject: the new architecture for engined application. Vanessa has a degree from Telecom Saint-Etienne. She began her career at the Criminal Research Institute as a developer for the audiovisual department. She has also worked in media and medical fields as expert in technical support, production as well as documenting new features.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor
Subscribe to 4D Newsletter

© 2025 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy