Lecture No.03                  
Lecture Contents:

·         Abstraction
·         Classes
·         Inheritance
·         Major benefits of inheritance (Reuse)

Real life objects have a lot of attributes and many kind of behaviors but most of the time we are interested in only that part of the objects that is related to the problem we are currently going to solve, for example in implementing a school system we don’t need to take care of the personnel life of a student or a teacher as it will not effect our system in any way so we will see these objects in the perspective of school system and will ignore their other characteristics, this concept is called “Abstraction”. Abstraction is a way to cope with complexity and it is used to simplify things.

Principle of abstraction:


“Capture only those details about an object that are relevant to current perspective”

Abstraction Example:


Suppose we want to implement abstraction for the following statement,

 “Ali is a PhD student and teaches BS students”

Here object Ali has two perspectives one is his student perspective and second is his teacher perspective.

We can sum up Ali’s attributes as follows,


Name
Age
Student Roll No
Year of Study
CGPA 
Employee ID
Designation
Salary


As you can see out of all these listed attributes some belong to Ali’s student perspective(Roll No, CGPA, Year of study) and some belong to Ali’s teacher perspective(Employee ID, Designation, Salary).

Similarly we can sum up Ali’s behavior as follows,

Study  
DevelopExam
GiveExam
TakeExam
PlaySports
Eat
DeliverLecture
Walk


As was the case with attributes of object Ali, its behavior can also be divided in Ali’s student perspective as well as Ali’s teacher perspective.

Student’s Perspective

Attributes:

- Name                                               - Employee ID
- Student Roll No                              - Designation
- Year of Study                                  - Salary
- CGPA                                               - Age


Behaviour:

- Study                                                - DevelopExam
- GiveExam                                        - TakeExam
- PlaySports                                       - Eat
- DeliverLecture                                 - Walk


Teacher’s Perspective


Attributes:
- Name                                               - Employee ID
- Student Roll No                               - Designation
- Year of Study                                   - Salary
- CGPA                                               - Age


Behaviour:
- Study                                                - DevelopExam
- GiveExam                             - TakeExam
- PlaySports                            - Eat
- DeliverLecture                                - Walk

A cat can be viewed with different perspectives

Ordinary Perspective
A pet animal with
Four Legs
A Tail
Two Ears
Sharp Teeth

Surgeon’s Perspective
A being with
A Skeleton
Heart
Kidney
Stomach


A car can be viewed with different perspectives


                        Driver’s View

Engineer’s View

Abstraction – Advantages

Abstraction has following major advantages,

  1. It helps us understanding and solving a problem using object oriented approach as it hides extra irrelevant details of objects.

  1. Focusing on single perspective of an object provides us freedom to change implementation for other aspects of for an object later.

Similar to Encapsulation Abstraction is also used for achieving information hiding as we show only relevant details to related objects, and hide other details.


In OOP we create a general sketch for each kind of objects and then we create different instances using this sketch we call this sketch or prototype or map as “class”.
All objects of same kind exhibit identical characteristics (information structure and behavior) however they have data of their own.

Class –Example 1

Consider the objects given below,
·         Ali studies mathematics
·         Anam studies physics
·         Sohail studies chemistry

Each one is a Student so we say these objects are instances of the Student class.

Class –Example 2

Consider the objects given below,

·         Ahsan teaches mathematics
·         Aamir teaches computer science
·         Atif teaches physics

Each one is a teacher so we say these objects are instances of the Teacher class

Class Representation:
we can represent a class using a rectangle as follows,

Class Example: Circle


Class Example: Person



A child inherits characteristics of its parents, besides inherited characteristics, a child may have its own unique characteristics

Inheritance in Classes

If a class B inherits from class A then it contains all the characteristics (information structure and behaviour) of class A
The parent class is called base class and the child class is called derived class
Besides inherited characteristics, derived class may have its own unique characteristics





Inheritance – “IS A” or “IS A KIND OF” Relationship

Each derived class is a kind of its base class



Here,
Student IS A Person
Teacher IS A Person
Doctor IS A Person




Here,
Circle IS A Shape
Line IS A Shape
Triangle IS A Shape

Inheritance – Advantages

  1. Reuse
  2. Less redundancy
  3. Increased maintainability

Reuse with Inheritance
Main purpose of inheritance is reuse, we can easily add new classes by inheriting from existing classes.
Select an existing class closer to the desired functionality, create a new class and inherit it from the selected class, add to and/or modify the inherited functionality.

Post a Comment

Don't Forget To Join My FB Group VU Vicky
THANK YOU :)

Previous Post Next Post