Object-oriented programming in 4D: Manage class inheritance

Automatically translated from English

In a previous blog post, we introduced a very important concept in object-oriented programming: Classes. Now we’ll go through another core concept: Inheritance, the mechanism that allows a class to acquire the behavior of another class.

Example database

Remember the example in the previous blog post with the “Person” class ? Let’s imagine a school and we want to manage students and teachers. Both are persons, but with different characteristics. So we create a “Teacher” class and a “Student” class, and both classes inherit characteristics from the “Person” class.

 

How do you inherit A class from another?

A new Class extends keyword is now available to facilitate inheritance. Using our school example, we create a “Student” class that inherits the “Person” class.

Class Extends Person

And the constructor?

Then for the constructor (if you haven’t defined a specific constructor for the “Student” class), the constructor of the “Person” class is automatically called.

You can also override a constructor. For example in the case of “Student”: a student has a name, surname, and date of birth as a person, but a student also has a grade and a school. In this case, you can use the new Super command which allows you to call the constructor of the parent class. Then in the “Student” constructor, you can add the specific code for grade and school.

Class constructor
 C_TEXT($1) // FirstName
 C_TEXT($2) // LastName
 C_DATE($3) // Birthdate
 C_TEXT($4) // SchoolName
 C_TEXT($5) // Grade
 Super($1;$2;$3) // Call the "Person" constructor
 This.SchoolName:=$4
 This.Grade:=$5

And the functions?

You can add functions specific to the “Student” class from which you can call a function of the parent class (“Person”).

In the following example, we have a function that returns a string containing the student’s name, grade, and school. In the class “Person”, we already have a function that returns the full name of a person. So we call this function, getFullName, and add the information specific to a student.

Function getIdentity
 C_TEXT($0)
 $0:=Super.getFullName()+", "+This.Grade+" grade in "+This.SchoolName

In a method, we instantiate an instance of the “Student” class. We call the getFullName function from the parent class, “Person”. Then, we call the getIdentity function from the “Student” class. As you can see, there are no differences between the getFullName and getIdentity functions.

C_OBJECT($s)
$s:=cs.Student.new("Joe";"Doe";!2002-02-20!;"Waco High School";"10th")

$name:=$s.getFullName() //return John Doe
$identity:=$s.getIdentity() //return Joe DOE, 10th grade in Waco High School

A live demonstration to summarize

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.