Chapter Java Inheritance and Abstract Classes
Chapter Java Inheritance and Abstract Classes
Eng. glmoyo
Object modelling
• Abstract object class – has undefined or incomplete methods (programme code)
Animal Cannot be fully defined or programmed here yet
because a particular animal hasn’t been chosen
Name: String
(identified/created/exemplified or instantiated)
This is just a higher stage (generalisation or
This method is definable at this indication that animals produce sound as one of
stage because it common to all their characteristics). At this stage animal
Eat() ProducesSound() code cannot run because its yet to
animals(they can inherit as it is):
ProduceSound() be written explicitly - under each particular animal
- Take_in_oxygen ()
Breath()
- Circulate_it_in_the_blood()
- Relearse_CO2() NB. Because of this (yet to be written code)
An abstract class contain one or more abstract methods or you CANNOT create new objects instances
programmes with incomplete code that is yet to be completed to from abstract classes like:
match a particular case or instance e.g. if the animal is to be a dog you Animal Dog = New Animal();
will add: Because what will it produce when you
ProduceSound() { write:
This makes it complete or concrete (animal)
Buck() ; } Dog.ProduceSound() its undefined ????
Object modelling
• Concrete object – all methods are defined (i.e. have complete code or programme)
Public Woman
(any one can utilise)
+Name: String + public: any class can use the feature (attribute or
Private operation);
(only she can use) -Teeth : integer # protected: any descendant of the class can use
the feature;
Protected #Mitochondrial_DNA : string - private: only the class itself can use the feature.
(only her and her kids)
ProduceSound() ProcuduceSound()
GiveBirth() {
Maternal inheritance Breath() Talk(), Cry(), Ululate()
(we all inherit this from our mothers) } (this is now particular to a human being ) thus we
repeat the same method in the supper class but
We inherit this as is so we do not write or rewrite the code override (i.e. modify or redefine or complete the code
for this method as it was already written in the supper class for) the part that was left abstract (incomplete) in the
animal object class
Relationship in classes
• Generally objects are related by nature
• The relationship can be expressed as shown below
Female
int Age Name of class
Base class String Gender Properties of a class
#Mitochondrial_DNA : string Operations of a class (class methods)
Breath()
ProduceSound() Inheritance (pointing line from subclass
superclass to superclass with unfilled arrow)