Lecture
No.04
Lecture Contents
·
Generalization
·
Sub typing (extension)
·
Specialization (restriction)
·
Overriding
·
Abstract classes
·
Concrete classes
Recap
– Inheritance
·
Derived
class inherits all the characteristics of the base class
·
Besides
inherited characteristics, derived class may have its own unique
characteristics
·
Major
benefit of inheritance is reuse
o
Generalization
o
Subtyping
(extension)
o
Specialization
(restriction)
In OO models, some classes may have common
characteristics.
We extract these
features into a new class and inherit original classes from this new class. There
are many objects with common characteristics in object model. The common
characteristics (attributes and behaviour) of all these objects are combined in
a single general class. Base class encapsulates the idea of commonality of
derived classes. Base class is general class representing common behaviour of
all derived classes.
This concept is known as Generalization.
It reduces the redundancy and gives us reusability,
using generalization our solution becomes less complex.
In generalization there should be “Is a Kind of
Relationship” (also called “Is A relationship”) between base and child classes.
Example: Line, Circle and Triangle
Line is
shape Circle is a shape Triangle is a shape
Common attributes
Color vertices
Common behaviour
Set Color, Move
Example: Student Doctor and Teacher
Common
attributes,
Name, age, gender
|
Common
behaviour
Eat, Walk
|
Sub-typing & Specialization
We want
to add a new class to an existing model
We have
developed an existing class hierarchy
Find an
existing class that already implements some of the desired state and behaviour
Inherit
the new class from this class and add unique behaviour to the new class
Sub-typing
means that derived class is behaviourally compatible with the base class
Derived
class has all the characteristics of base class plus some extra characteristics
Behaviourally
compatible means that base class can be replaced by the derived class
Sub-typing (Extension) - Example
Circle is extending the behaviour of shape, it is
extending attributes of shape by adding radius similarly it is extending
behaviour of shape by adding compute Circumference and compute Area.
|
Student has two extra attributes program and
studyYear
Similarly it has extended behaviour by adding
study and takeExam.
|
Subtyping and
generalization are related concepts, Subtyping (extension) and generalization
is a way to look same thing in two ways.
Sub typing is looking
at things from Top to bottom whereas in generalization we look at things from
bottom to top.
We want to add a
class to existing hierarchy of classes having many similarities to already
existing classes but some part of its behaviour is different or restricted. In
that case we will use the concept of specialization.
Specialization
means that derived class is behaviourally incompatible with the base class
Behaviourally
incompatibility means that base class can’t always be replaced by the derived
class
Derived
class has some different of restricted characteristics than of base class.
Example
– Specialization (Restriction)
Suppose we want
to add one more class of Adult for some special requirement like for ID card
generation such that it is a person but its age is greater than 18 and having
all other behaviour of that of person class. One solution is that we write
another class from beginning and write
all code of person again in it with age limit, but better solution is that we
derive adult class from person class and restrict age in that class as shown
below in diagram,
Similarly Natural Numbers are also Integers with the restriction that natural numbers set can NOT contain zero or
negative integers it consists of only positive integers so we can implement
this relationship also as specialization,
Add method behaviour is present in both base and
derived classes but derived class behaviour is different in derived class.
Derived class will not exhibit the behaviour of base class but it is overriding behaviour of base class with
its own behaviour.
A class
may need to override the default behaviour provided by its base class
Derived
class overrides the behaviour of its base class.
Reasons
for overriding
Provide
behaviour specific to a derived class (specialization)
Extend
the default behaviour (extension)
Restrict
the default behaviour (restriction)
Improve
performance
It
is used for the implementation of inheritance.
Example
– Specific Behaviour (Specialization)
Example
– Extention
Example
– Improve Performance
Class
Circle overrides rotate operation of class Shape with a Null operation.
In
our examples we made classes for shape and person. These are abstract concepts
and the classes we make against abstract concepts are called abstract classes. They are present at or near the top in the
class hierarchy to present most generalized behaviour.
An
abstract class implements an abstract concept
Main
purpose is to be inherited by other classes
Can’t
be instantiated
Promotes
reuse
Abstract
Classes - Example I
Here,
Shape is an abstract class
Abstract Class
|
Shape
|
|||
Concrete Classes
|
Circle
|
Line
|
Triangle
|
….
|
Abstract
Classes - Example II
Here,
Person is an abstract class
Abstract Class
|
Person
|
|||||
Concrete Classes
|
Student
|
Teacher
|
Doctor
|
Engineer
|
Director
|
….
|
Abstract
Classes - Example III
Here,
Vehicle is an abstract class
Abstract Class
|
Vehicle
|
|||
Concrete Classes
|
Car
|
Bus
|
Truck
|
….
|
Abstract
Classes can not exist standalone in an object model
While making object model we start by finding out
objects in our object model and then we find out objects having common
attributes and make them in the form of general classes at the top of class
hierarchies.
The entities that actually we see in our real world
are called concrete objects and classes made against these objects are called
concrete classes.
A
concrete class implements a concrete concept
These
are used to instantiate objects in our programs
Provides
implementation details specific to the domain context
Concrete Classes - Example I
Here
Student, Teacher and Doctor are concrete classes
Concrete Classes - Example II
Here Car,
Bus and Truck are concrete classes
·
A concrete class may exist in an object model
independently
·
Concrete classes mostly lie below the top of class
hierarchy in a good object model.
If there is an abstract class then hierarchy exists
in the object model as there will definitely be some concrete classes as well
derived from this abstract class otherwise there is no use of abstract class.
Glossary:
- Natural numbers: numbers from 1 to …….onwards
- Integers: all positive and negative numbers …..-3,-2,-1,0,1,2,3………
- Whole numbers: numbers from 0 ,1 ,2, 3 ….onwards (natural no’s including 0)
Some times whole
numbers are also called numbers without fractional part.
Natural
numbers: positive integers only (numbers from 1 to …….onwards)
Integers:
all positive and negative numbers (…..-3 , -2 , -1 , 0 , 1 , 2 , 3………)
Post a Comment
Don't Forget To Join My FB Group VU Vicky
THANK YOU :)