Docsity

OOP II: Abstraction, Encapsulation, Reusability, Inheritance, Polymorphism, Study notes of Computer Science

Toggle sidebar
bg1
1
CS 162: Introduction to
Computer Science II
Object-Oriented Programming II
OOP (continued from last time)
• Behavior of objects is specified in terms of
responsibilities
– eg. Fred has the responsibility of delivering flowers to
Robin. How he does this is irrelevant.
• This permits greater independence between
objects, which is a critical factor in solving
complex problems
– How? You can break down the complex problem into
little pieces and make each object responsible for
each piece
pf3
pf4
pf5
pf8

Partial preview of the text

Download OOP II: Abstraction, Encapsulation, Reusability, Inheritance, Polymorphism and more Study notes Computer Science in PDF only on Docsity!

CS 162: Introduction to

Computer Science II

Object-Oriented Programming II

OOP (continued from last time)

  • Behavior of objects is specified in terms of responsibilities - eg. Fred has the responsibility of delivering flowers to Robin. How he does this is irrelevant.
  • This permits greater independence between objects, which is a critical factor in solving complex problems - How? You can break down the complex problem into little pieces and make each object responsible for each piece

Key OOP Concepts

1. Abstraction

2. Encapsulation

3. Reusability

4. Inheritance

5. Polymorphism

We will discuss this in a future lecture

1. Abstraction

  • Suppose you use maps.google.com to get directions from Corvallis, OR to Seattle, WA
  • You can zoom into the map and see every side street
  • If you zoom out a little, you only see the major highways
  • If you zoom out all the way, you only see the state boundaries

Which is the right level of detail?

2. Encapsulation

Encapsulation:

  • The hiding of unimportant details of a component and the control of access to information in a component through a well-defined interface
  • Treats a component like a black box (inner implementation is hidden)

Encapsulation requires a strict division between the interface and implementation

2. Encapsulation

  • Interface
    • Defines what a system is designed to do (service view that users need to understand)
    • Says nothing about how the assigned task is being performed
    • Needs to be matched to an implementation

2. Encapsulation

  • Implementation:
    • Used to fulfill the requirements of the interface
    • This is what programmers or designers need to understand

2. Encapsulation

  • Advantages of Encapsulation
    • Easier to understand a design at a high level (due to abstraction)
    • Efficiency in decision making (can ignore irrelevant details)
    • Permits Interchangeability:
      • If we keep interaction between the parts to a minimum, then we can then interchange components
      • Make sure the public interface is implemented as described but the private implementation can change
    • Allows Composition

3. Reusability

  • If there is a task to perform, find

someone else to do it!

  • Need to trust other agents to do this task

(in programming, you need to trust other

pieces of software)

  • Can also reuse code through inheritance

4. Inheritance

  • Classes can be organized into a

hierarchical inheritance structure

  • A child class (or subclass) will inherit

attributes from a parent class higher in the

tree

  • An abstract parent class is a class (such

as mammal) for which there are no direct

instances; it is used only to create

subclasses

4. Inheritance

Material Objects

Animal Plant

Flower

Carnation

Dog Human

Shopkeeper Dentist

Florist

Spot Fred Kenneth

Robin’s Flowers

Mammal

Phyllis

Platypus