abstraction and encapsulation

7
E.g. “A tall guy and a short guy, both with short hair and black suits.” Abstraction “A picture is worth a thousand words” • Abstraction Dictionary.com definition: the quality of dealing with ideas rather than events Abstraction: less say’s more. A concept which can encapsulate further details within itself. We could say that a picture of a person is ‘more abstract’ than his verbose account, e.g. Events – 1. Take a kettle – 2. Pour some water – 3. Heat the kettle for 10 minutes – 4. Pour hot water into a cup – 5. Add tea bags, sugar and milk Ideas Make some tea Note that events are mostly verbs (functions), while ideas are nouns (objects). VS

Upload: haris-bin-zahid

Post on 06-Aug-2015

21 views

Category:

Software


4 download

TRANSCRIPT

Page 1: Abstraction and Encapsulation

E.g. “A tall guy and a short guy, both with short hair and black suits.”

Abstraction“A picture is worth a thousand words”

• Abstraction– Dictionary.com definition: the

quality of dealing with ideas rather than events

– Abstraction: less say’s more. A concept which can encapsulate further details within itself.

– We could say that a picture of a person is ‘more abstract’ than his verbose account, e.g.

• Events– 1. Take a kettle– 2. Pour some water– 3. Heat the kettle for 10 minutes– 4. Pour hot water into a cup– 5. Add tea bags, sugar and milk

• Ideas– Make some tea

• Note that events are mostly verbs (functions), while ideas are nouns (objects).VS

Page 2: Abstraction and Encapsulation

Abstraction “A picture is worth a thousand words”

Definitions:

• The Harper Collins Dictionary of Mathematics definition– abstraction (noun): the process of formulating a generalized

concept of a common property by disregarding the differences between a number of particular instances

• Wikipedia definition– "Abstraction is the process of generalization by reducing the

information content of a concept or an observable phenomenon, typically in order to retain only information which is relevant for a particular purpose.“

Page 3: Abstraction and Encapsulation

Abstraction “A picture is worth a thousand words”

• Abstraction: a process of Generalizing things

– “Suzuki car is a car. Toyota car is a car. Honda car is a car. All 3 of these have front lights. So all cars must have front lights.” Here, we are generalizing that all cars have front lights based on common property (front lights) of 3 different instances (honda car, toyota car and suzuki car).

– We need to be careful when generalizing in real life or engineering, since some generalizations can be done wrong. E.g. “Apple is a fruit. Banana is a fruit. So apple is a banana”.

Page 4: Abstraction and Encapsulation

Why Abstraction

• Why should we go for abstraction?

– It is a process of simplification, both in real world and in code.

– In Development: Abstraction hides the irrelevant details in code. All you need to know is a public set of functions

• If a team responsible of maintaining a certain function may decide to change it (while keeping the final expectations same), you do not need to worry if your code uses their function.

• Exercise: Suppose a taxi driver object needs to take us from A D. Think of 2 different ways in which taxi driver may accomplish this task, while keeping the end goal same (reaching from A D)

– Abstraction is the heart of any decision making or logical thinking. E.g. When we look at an audience--customers, prospects, projects etc. we make decisions on the whole based on our assumptions about the individuals within the group.

Page 5: Abstraction and Encapsulation

Encapsulation

• Wikipedia definition– A language mechanism for restricting access to some of the object's

components.• It is a specialized case of ‘Abstraction’, such that it is a mechanism to

hide irrelevant details of one object from another, while exposing only the relevant details that the other object might need. E.g. a Manager object may only be concerned with professional skills and availability timings of an employee, not his personal life details.

• Separation of concerns– e.g. If your goal is to reach from A D, you should not be required to tell

the TaxiDriver Object to go from A to B to C to D. In good Object Oriented design, it would be the responsibility of the TaxiDriver object to find the best route from A to D, not yours. Similarly, it would be the responsibility of the TaxiDriver class’s developer team to write the best code to reach from A to D, not yours.

Page 6: Abstraction and Encapsulation

EncapsulationBenefits

• Helps the team of developers to organize the code better– Every developer is responsible for his/her own class(es) and methods. – Makes the overall coding effort easier, as you are only concerned with what another class does,

not how it does it. All you need is to utilize that class’s code by sending requests to its objects.– Helps the developers to be more ‘objective/goal oriented.’

• Helps the concept of ‘modularity’ in applications. If encapsulation principle is followed correctly, there should be low dependency b/w different software modules. Hence, changes in one module should not cause changes to be made into another.

• Guideline: whenever you find sending too many messages to another object to accomplish a certain scenario, consider encapsulation:

VS

Page 7: Abstraction and Encapsulation

EncapsulationSome pictorial examples

• Internal mechanics of changing gears are (private) hidden from us: all we need to do is to use the public interface/panel (the gear rod) provided to us when we want to change gears.

• The only parts of the above object that are accessible to other objects would be the ones marked in red.