oo11100111

Computer Stores everything in binary

Welcome to Easy Learning. Place to learn stuff in a cool way.

Thinking ...

It gives you solutions !!!

Human Eye

The human eye sees basically three colors: red, green and blue. The white is a combination of the three, the black is their lack.

LAN Cables with RJ-45 Connectors

Ethernet Connectivity is enabled by using Cables.

Time

Time's precious. Don't waste it.

Showing posts with label OOP. Show all posts
Showing posts with label OOP. Show all posts

Abstraction

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.
From driver’s point of view, it has an accelerator, a brake, a clutch, a gear box etc.
From engineer’s point of view, it has an engine, a fuel pump, a rotary motor, shocks etc.
clip_image002
Driver’s View
clip_image004
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.


Information Hiding and Encapsulation in OOP

Information hiding is one of the most important principles of OOP inspired from real life which says that all information should not be accessible to all persons. Private information should only be accessible to its owner.
By Information Hiding we mean “Showing only those details to the outside world which are necessary for the outside world and hiding all other details from the outside world.”
Real Life Examples of Information Hiding
  1. Your name and other personal information is stored in your brain we can’t access this information directly. For getting this information we need to ask you about it and it will be up to you how much details you would like to share with us.
  1. An email server may have account information of millions of people but it will share only our account information with us if we request it to send anyone else accounts information our request will be refused.
  1. A phone SIM card may store several phone numbers but we can’t read the numbers directly from the SIM card rather phone-set reads this information for us and if the owner of this phone has not allowed others to see the numbers saved in this phone we will not be able to see those phone numbers using phone.
In object oriented programming approach we have objects with their attributes and behaviors that are hidden from other classes, so we can say that object oriented programming follows the principle of information hiding.
In the perspective of Object Oriented Programming Information Hiding is,
“Hiding the object details (state and behavior) from the users”
Here by users we mean “an object” of another class that is calling functions of this class using the reference of this class object or it may be some other program in which we are using this class.
Information Hiding is achieved in Object Oriented Programming using the following principles,
· All information related to an object is stored within the object
· It is hidden from the outside world
· It can only be manipulated by the object itself
Advantages of Information Hiding
Following are two major advantages of information hiding. It simplifies our Object Oriented Model:
As we saw earlier that our object oriented model only had objects and their interactions hiding implementation details so it makes it easier for everyone to understand our object oriented model. It is a barrier against change propagation. As implementation of functions is limited to our class and we have only given the name of functions to user along with description of parameters so if we change implementation of function it doesn’t affect the object oriented model.
We can achieve information hiding using Encapsulation and Abstraction, so we see these two concepts in detail now.

Encapsulation means “we have enclosed all the characteristics of an object in the object itself”.
Encapsulation and information hiding are much related concepts (information hiding is achieved using Encapsulation). We have seen in previous lecture that object characteristics include data members and behavior of the object in the form of functions. So we can say that Data and Behavior are tightly coupled inside an object and both the information structure and implementation details of its operations are hidden from the outer world.
Examples of Encapsulation
Consider the same example of object Ali of previous lecture we described it as follows.

Ali
Characteristics (attributes)
· Name
· Age
Behavior (operations)
· Walks
· Eats

You can see that Ali stores his personal information in itself and its behavior is also implemented in it. Now it is up to object Ali whether he wants to share that information with outside world or not. Same thing stands for its behavior if some other object in real life wants to use his behavior of walking it can not use it without the permission of Ali. So we say that attributes and behavior of Ali are encapsulated in it. Any other object don’t know about these things unless Ali share this information with that object through an interface. Same concept also applies to phone which has some data and behavior of showing that data to user we can only access the information stored in the phone if phone interface allow us to do so.
Advantages of Encapsulation
The following are the main advantages of Encapsulation,
  1. Simplicity and clarity
As all data and functions are stored in the objects so there is no data or function around in program that is not part of any object and is this way it becomes very easy to understand the purpose of each data member and function in an object.
  1. Low complexity
As data members and functions are hidden in objects and each object has a specific behavior so there is less complexity in code there will be no such situations that a functions is using some other function and that functions is using some other function.
  1. Better understanding
Everyone will be able to understand whole scenario by simple looking into object diagrams without any issue as each object has specific role and specific relation with other objects.



Interface and Implementation in OOP

Interface is a set of functions of an object that he wants to expose to other objects. Interface of an object provides us the list of available functions.
As we discussed previously that data and behavior of each object is hidden in that object itself so we have to use the concept of interface of the object to expose its behavior to outer word objects.
· Different objects may need different functions of an object so interface of an object may be different for different objects.
· Interfaces are necessary for object communication. Each object provides interface/s (operations) to other objects through these interfaces other objects communicate with this object.
Example – Interface of a Car
· Steer Wheels
· Accelerate
· Change Gear
· Apply Brakes
· Turn Lights On/Off
Example – Interface of a Phone
· Input Number
· Place Call
· Disconnect Call
· Add number to address book
· Remove number
· Update number

It is actual implementation of the behavior of the object in any Object Oriented language.
It has two parts,
· Internal data structures to hold an object state that will be hidden from us it will store values for an object data members.
· Functionality in the form of member functions to provide required behavior.
Examples of Implementation
  1. Gear Box in car system
Consider object Gear Box in car system it has a certain structure and functionality. When this object will be implemented it will have two things,
· Physical structure of the gear box
· Functionality implemented in this structure to change gear.
Both these things are part of implementation.
So it has,
· Data Structure in the form of Mechanical structure of gear box
· Functionality mechanism to change gear

  1. Address Book in a Phone
Similarly take the example of contact details saved in the SIM of a phone, In that case we can say physical structure of SIM card as Data Structure
And Read/write operations provided by the phone as Functionality.
 
As discussed earlier we only show interface of an object to outside world and hide actual implementation from outside world. The benefit of using this approach is that our object interface to outside word becomes independent from inside implementation of that interface. An object may have more than one interface.
This is achieved through the concepts of encapsulation and information hiding. Interface and implementation are separated from each other to achieve Information Hiding.
 
Real Life example of separation of interface and implementations
Driver has a standard interface to drive a car and using that interface he drive can drive any car regardless of its model or type whatever engine type it has or whatever type of fuel it is using.
There is another important phenomena in OOP , i.e. ‘messages’.  
Objects communicate through messages they send messages (stimuli) by invoking appropriate operations on the target object. The number and kind of messages that can be sent to an object depends upon its interface. Objects communicate with each other using messages.
Examples – Messages
A Person sends message (stimulus) “stop” to a Car by applying brakes
A Person sends message “place call” to a Phone by pressing appropriate button



Object-Orientation

Object oriented programming is an essential part of our today’s programming methodology. It is being extensively used in all programming languages due to its key aspects and benefits which will be discussed later on. It is a technique in which we visualize our programming problems in the form of objects and their interactions as happens in real life.

Examples:
We have different objects around us in our real life that interact with each other to perform different operations For example, Human beings, cars, tree, house etc.
Take an example of a person lives in a house. He drives a car. All these are objects and have their own unique characteristics. Take another example of a School; the objects in a school are student, teacher, books, pen ,school bag, classroom, parents, playground etc.

Suppose we want to develop a fee collection system for a school for this we will need to find out related objects and their interactions as happens in real life. In this way we can say that object orientation makes it easier for us to solve our real world problems by thinking solution of the problem in terms of real world objects.
So we can say that in our daily life everything can be taken as an object that behaves in a certain way and has certain attributes. In object orientation we move our concentration to objects in contrast to procedural paradigm in which we simply write our code in functions and call them in our main program.

What is a Model?
A model is an abstraction of something real or conceptual. We need models to understand an aspect of reality.

Model Examples
Highway maps
Architectural models
Mechanical models

OO Models:

In the context of programming models are used to understand the problem before starting developing it. We make Object Oriented models showing several interacting objects to understand a system given to us for implementation.
Object Oriented Model

Objects
Ali, Car, House, Tree


Interactions
Ali lives in the house
Ali drives the car

clip_image025
Object Oriented Model (A School Model)
Objects
Teacher, Student, School Bag, Pen, Book Playground
 
Interactions
Teacher teaches Student.
Student has School Bag, Book and Pen

clip_image029

Object-Orientation - Advantages

As Object Oriented Models map directly to reality as we have seen in examples above therefore,
We can easily develop an object oriented model for a problem.
Everyone can easily understand an object oriented model.
We can easily implement an object oriented model for a problem using any object oriented language like c++ using its features like classes, inheritance, virtual functions etc.

What is an Object?
An object is,
  1. Something tangible (Human being, School, House, Car).
  2. Something conceptual (that can be apprehended intellectually for example time, date and so on…).
An object has,
  1. State (attributes)
  2. Well-defined behavior (operations)
  3. Unique identity

Tangible and Intangible Objects

Examples of Tangible Objects:
Ali is a tangible object, having some characteristics (attributes) and behavior as given below,
Characteristics (attributes)
Name
Age
Behaviour (operations)


Walks
Eats


We will identify Ali using his name.
Car is also a tangible object having some characteristics (attributes) and behavior given below,

State (attributes)

Color
Model

Behavior (operations)
Accelerate
Start Car
Change Gear


We can identify Car using its registration number.

Examples of Intangible Objects (also called as conceptual objects):

Time is an intangible (conceptual) object

State (attributes)

Hours
Seconds
Minutes


Behavior (operations)
Set/Get Hours
Set/Get Seconds
Set/Get Minutes
We will assign our own generated unique ID in the model for Time object. 


Date is also an intangible (conceptual) object

State (attributes)
Year
Day
Month

Behavior (operations)
Set/Get Year
Set/Get Day
Set/Get Month
We will assign our own generated unique ID in the model for Date object.

Let us look at some of the key aspects of object orientation.

Key Aspects:
  • Model is the abstraction of some real word scenario. It helps us to understand that scenario.
  • Object oriented model of any scenario (problem) describes that scenario (problem) in the form of interacting objects.
  • We use Object Orientation because it helps us in mapping real world problem in a programming language.
  • Object Orientation is achieved using objects and their relationships.
  • Properties of an object are described using its data members and behavior of an object is described using its functions.
  • Objects may be tangible (physical) or intangible (also called conceptual or virtual).
  • Generally when we have given a certain problem description, nouns in that problem description are candidates for becoming objects of our system.
  • There may be more than one aspects of an object
  • It is not necessary that every object has a specific role in implementation of a problem there may be some objects without any role, like school parking in our school.
  • It is easier to develop programs using Object Oriented Programming because it is closer to real life.