Fundamentals of Object Orientated Programming
Here we are going to discuss the fundamental concepts of object orientated analysis, design and programming. First we are going to look at inheritance, followed by polymorphism then abstraction and finally we will be covering encapsulation.
Take our previous example of ‘Person’, You could think of person as the highest level of our hierarchy, being at the top and below it you could have roles the person could be, let’s use a university as an example.
In our university we have teachers and students with other members of staff such as admin and librarians etc.
So our person is the top and that would mean that all the lower classes would inherit all the attributes and actions of ‘person’. Such as a teacher would inherit the ability to walk and talk along with student being able to do the same thing. Because a student and a teacher would have attributes and actions specific to them is the very reason why these actions and attributes are not considered in the person class.
Let’s look at an example
In this diagram you would call the person the super class or parent class and you would call the student and teacher classes the child classes. To model inheritance on a class diagram you draw an unfilled arrow head toward the parent class pointing away from the child class.
You could think of the talk method as a polymorphic method. Both the student and the teacher could both use the talk () method but the attributes that they would pass in to the method would be different depending on what type (this word is sometime used to define a class/object) is used to call the method. So for a teacher the talk method would be used to teach and the attributes that would be passed into it would be the subject. The student could also call the talk () method and this would be used to discuss the subject with fellow student and the tutor. This is also very similar to polymorphism. You would probably use this example more with polymorphism.
Abstraction would be like create a data structure that could be reused by classes that would benefit from the inheritance. If you took our example and created an attribute that for the tutor would measure the years of service and for the student measured how many year of study they had done, you could put this attribute in person and call it years but overload the attribute in the child classes. Also this means that is for the student the years was a list, where as for tutor is would be and integer. So for the student you would have 1st year, 2nd year, 3rd year, 1st year post grad, and so on, but for the tutor you would just count the years of service 1,2,3,4…..
Encapsulation would be the clearly defined boundary of our classes. This means that you would not have another class that could define the person, also child classes that specifically define a subset of people. For instance you would not have one class for a person that would define the height and a separate one that you define the eye colour a that person.