Author: S S B Magesh Puvananthiran
When can we create sub/super classes in an Object Oriented Design?
As every Object Pascal developer knows, inheritance is one of the fundamental
concepts in Object Oriented Design. Iím not going to give you any explanation on
what Inheritance is since everybody knows the definition already. Instead, I'm
going to give you some of the tips in designing classes in the early stages of
Object Oriented Design.
In any project development, the analysis and design phases will be given importance
in the initial stage. In Object Oriented Design/Visual Modeling, once the team
starts collecting information regarding the project, the team will identify the
objects involved in each of the activities.
At one stage, the team will have some sample classes for those objects identified.
As the design stage matures, there would be more and more classes coming.
Sometimes, you may need to inherit a new class from an existing one or you may need
to group two classes into one. At this time, you may use the following
concepts/techniques to create a sub/super class from existing classes:
What is a Sub Class and Super Class
It's a class inherited/derived from another class. The new class(sub-class) will
have all the properties/methods and events of the parent class(from which it
inherited) and can have additional properties specific to this sub-class. The
parent class is called Super Class.
Let me explain this concept with an example.
Let us suppose we have a class called TCitizen.
The structure of TCitizen is something like this:
1 TCitizen = class
2 SocialSecurityNo: string;
3 Name: string;
4 Age: integer;
5 Street: string;
6 City: string;
7 State: string;
8 Zip: integer;
10 ..................etc., }
The above attributes are some of the common attributes you can have for a Citizen.
This citizen could be anybody from a small child to an old man in a country.
Let us suppose that we have, in our analysis, some Veterans information also.
Veterans are people who were being in Army and/or some distinguished government
services and retired now. Those Veterans are also part of normal citizens but they
would have some special privileges. In this case, we can use the existing TCitizen
class by adding the special privileges attributes for a Veteran but that would not
be a better design. In this case, we can call this Veteran as a SPECIALIZED
Citizen. So we can create a new SUB CLASS derived from the TCitizen, called
The TVeteranCitizen class may look like something like this:
12 TVeteranCitizen = class(TCitizen)
13 NoOfYearsOfService: integer;
14 Rewards: string;
15 Ranks: string;
16 DateRetired: TDateTime;
18 ......................................etc., }
GENERALIZATION: Creating Super Classes
Let me explain this also thru some sample classes.
Let us suppose we are designing a library system and we identified two classes
TStudent, containing student information, and TProfessor, containing professor
information, among other classes. We take these two classes for our discussion.
The structure of those two classes would be as follows:
20 TStudent = class
21 StudentID: string;
22 Name: string;
23 Age: string;
24 Grade: string;
26 .....................etc., }
29 TProfessor = class
30 ProfID: string;
31 Name: string;
32 Age: string;
34 ................etc., }
The system will allow both the students and professors to login using their student
and professor ids and do the library related activities. The system will verify the
student and professor ids at the time of login.
Here we can GENERALIZE an information pertaining to both the classes as long as
they both agree in their structure and type. Iím talking about the two attributes
StudentID in TStudent and ProfID in TProfessor. In this case, they both are of same
type: String. The second thing is that they should be both of the same
structure/size string. If StudentID is of seven charactors length and ProfID is of
four charactors length, then we cannot generalize this info.
As long as they both agree on those two things, we can generalize.
They both serve as a way to login to the system after verification. So we can
GENERALIZE this information and create a SUPER CLASS with a name TUser and inherit
both TStudent and TProfessor from TUser.
Although this seems to be a simple issue, I just would like to share this with our Delphi Community.