When can we create sub/super classes in an Object Oriented Design?
Object Oriented Design/Visual Modeling
THIS ARTICLE MAY HELP YOU IN THE EARLY AND INTERMEDIATE STAGES OF ANALYSIS AND 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:
SPECIALIZATION: Creating Sub 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:
TCitizen = Class
SocialSecurityNo : string;
Name : string;
Age : integer;
Street : string;
City : string;
State : string;
Zip : integer;
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 TVeteranCitizen.
The TVeteranCitizen class may look like something like this:
TVeteranCitizen = class(TCitizen)
NoOfYearsOfService : integer;
Rewards : string;
Ranks : string;
DateRetired : TDateTime;
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:
TStudent = Class
StudentID : string;
Name : string;
Age : string;
Grade : string;
TProfessor = Class
ProfID : string;
Name : string;
Age : string;
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.