0% found this document useful (0 votes)
34 views

OOP. Meaning of A Type

This document provides an introduction to object-oriented programming concepts including: 1) What is OOP and key concepts like types, encapsulation, inheritance, association, and the single responsibility principle. 2) Generalization and specialization are discussed as ways to reuse interfaces through inheritance and aggregation. 3) Encapsulation, inheritance, association, aggregation and composition relationships are explained in detail with examples.

Uploaded by

Kushner Serge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

OOP. Meaning of A Type

This document provides an introduction to object-oriented programming concepts including: 1) What is OOP and key concepts like types, encapsulation, inheritance, association, and the single responsibility principle. 2) Generalization and specialization are discussed as ways to reuse interfaces through inheritance and aggregation. 3) Encapsulation, inheritance, association, aggregation and composition relationships are explained in detail with examples.

Uploaded by

Kushner Serge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Continuous staff improvement project

INTRODUCTION TO OOP.

YURII HOHAN
INTRODUCTION
• WHAT IS OOP?
• MEANING OF A TYPE.
• SINGLE RESPONSIBILITY.
• ENCAPSULATION.
• UNDERSTANDING INHERITANCE. UNDERSTANDING
ASSOCIATION.
• INHERITANCE VS ASSOCIATION.
• CLASS DIAGRAM.
OOP
• Object-oriented programming is a method of implementation in which programs
are organized as cooperative collections of objects, each of which represents an
instance of some class, and whose classes are all members of a hierarchy of
classes united via inheritance relationships.
• (1) Object-oriented programming uses objects, not algorithms, as its fundamental logical
building blocks
• (2) each object is an instance of some class;
• (3) classes may be related to one another via inheritance relationships (the “is a” hierarchy).
THE POPULARITY OF OOP PARADIGM
• A form of abstraction that resonates with techniques people use to solve problems
in their everyday life.
• Induction (Abstract)
• From specialization to generalization
• From different dogs to create the word “dog”. Humans use induction
DOG
THE POPULARITY OF OOP PARADIGM
• Deduction(infer)
• From generalization to specialization
• From the word “dog” you have learnt to know an animal is or not a dog.

dog
not a dog
GENERALIZATION AND SPECIALIZATION
GENERALIZATION AND SPECIALIZATION
• Generalization creates a concept with a broader scope.
• Specialization creates a concept with a narrower scope.
• Reusing the interface!
GENERALIZATION AND SPECIALIZATION
• Inheritance: get the interface from the general class.
• Objects related by inheritance are all of the same type.
• “is-a” or “is-like-a” relationship
CLASSIFICATION AND EXEMPLIFICATION
• hat, 23, 34, mouse, telephone, book, 98, 45.34, hello
• numbers: 23, 34, 98, 45.34
• words: hat, mouse, telephone, book, hello
• mouse, tyrannosaurus rex, allosaurus, elephant, velociraptor
• dinosaur: tyrannosaurus rex, allosaurus, velociraptor
• mammal: mouse, elephant
TYPE AND INTERFACE
• An object has type and an interface.

• To get an object
Account a = new Account()
Account b = new Account()
• To send a message
a.withdraw();
b.deposit();
a.balance();
INTERACTION BETWEEN OBJECTS
• Interaction between objects happens by messages being send.
• A message activates a method on the calling object.
• An object O1 interacts with another object O2 by calling a method on O2 (must
be part of the client interface).
• “O1 sends O2 a message”
• O1 and O2 must be related to communicate.
• The call of a method corresponds to a function (or procedure) call.
AGGREGATION AND DECOMPOSITION
• Idea: make new objects by combining existing objects.
• Reusing the implementation!
• “Has a” keyword. Car “has-a” Gearbox and Car “has-an” Engine
ENCAPSULATION
• Encapsulation also can be described as a protective barrier that prevents the code
and data being randomly accessed by other code defined outside the class.
• To hide the internal implementation details of the class
• Keeps class tidy by keeping the visible fields to a minimum
• The main mechanism is the access modifiers: private, protected, public,
internal
ENCAPSULATION
1. Combining data and how it's manipulated in one place : This is achieved
through the state (the private fields) and the behaviors (the public methods)
of an object.

2. Only allowing the state of an object to be accessed and modified through


behaviors: The values contained within an object's state can then be strictly
controlled.

3. Hiding the details of how the object works: The only part of the object that
is accessible to the outside world is its behaviors. What happens inside those
behaviors and how the state is stored is hidden from view.
ACCESS MODIFIERS
• public: visible to all other classes
public class Clock
• private: visible only to the current class, its
methods, and every instance (object) of its class
• a child class cannot refer to its parent's private members!
private Time time;

• protected (this one's new to us): visible to the


current class, and all of its child classes
protected Hand HourHand;
ENCAPSULATION
INHERITANCE
• Allows sharing of the behavior of the parent class into its child
classes
• one of the major benefits of object-oriented programming (OOP) is this code
re-utilization
• Child class can add new behavior or override existing behavior from
parent
• Extremely powerful and extremely dangerous.
• “is-a” or “is-like-a” relationship
INHERITANCE. TERMINOLOGY
• superclass, base class, parent class: terms to describe the parent in
the relationship, which shares its functionality

• subclass, derived class, child class: terms to describe the child in the
relationship, which accepts functionality from its parent

• extend, inherit, derive: become a subclass of another class


IS-A
• a PersianCat object "is-a" Cat
• therefore, it can do anything a Cat can do
• it can be substituted wherever a Cat is needed
• a variable of type Cat may refer to a PersianCat
object
INHERITANCE. EXAMPLE
ALTERNATIVES TO CODE REUTILIZATION

• Association is a loose relationship in which objects


of one class “know” about objects of another class
(has-a)
• Aggregation is part-whole relationship (is-part-of)
• Composition is similar to aggregation, but is more
strict (contains)
ASSOCIATION
• Association is a loose relationship in which objects of one class “know” about
objects of the other class.
• Association is identified by the phrase “has a”.
ASSOCIATION
• Association is a loose relationship in which objects of one class “know” about
objects of the other class.
• Association is identified by the phrase “has a”.
• Association is a static relationship.
ASSOCIATION. MULTIPLICITY
AGGREGATION
• aggregation represent a whole-part relationship.
Aggregation is a relationship between part and whole in which:
• parts may be independent of the whole;
• parts may be shared between two whole instances.

― Accessibility: The part objects are only accessible through the whole object.
― Lifetime: The part objects are destroyed when the whole object is destroyed.
― Partitioning: The whole object is completely partitioned by part objects; it does not
contain any state of its own.
AGGREGATION
COMPOSITION
• Composition is a relationship between part and whole in which part is not be
independent of the whole.
• The contained object is destroyed once the container object is destroyed => No
sharing between objects.
• Stronger than aggregation.
COMPOSITION
INHERITANCE VS ASSOCIATION.
• Do not forget rule of thumb: “has a” vs “is a”
• Start with association and use inheritance only if:
1) Subclass behaves like parent class
2) Subclass can be provided instead of base class at no cost
• Don't use inheritance just to get code reuse If all you really want is to reuse
code and there is no is-a relationship in sight, use composition.
• Don't use inheritance just to get at polymorphism If all you really want is
polymorphism, but there is no natural is-a relationship, use composition with
interfaces.
SINGLE RESPONSIBILITY
• THERE SHOULD NEVER BE MORE THAN ONE REASON FOR A CLASS
TO CHANGE.
• In the context of the Single Responsibility Principle (SRP) we define a
responsibility to be “a reason for change.”

• Is there more than one responsibility?


• The first responsibility is connection management(dial-hangup). The second is
data communication(send-receive).
SINGLE RESPONSIBILITY
• Should these two responsibilities be separated? Almost certainly they should. The
two concepts have nothing in common.

• The SRP is one of the simplest of the principle, and one of the hardest to get
right. Conjoining responsibilities is something that we do naturally. Finding and
separating those responsibilities from one another is much of what software
design is really about.
SINGLE RESPONSIBILITY. PRACTICE
.NET Framework types:
1. DateTime. Is it violating SRP?
2. С# partial classes. What is it?
3. String. Is it violating SRP?
ASSIGNMENT
• CHOOSE A PREFERRED DOMAIN LIKE CARS, BANKS, COSMETICS, COSMOS,
ETC.
• CREATE A DIAGRAM WITH AN EXAMPLE OF INHERITANCE.
• CREATE A DIAGRAM WITH AN EXAMPLE OF ASSOCIATION.
• CREATE A DIAGRAM WITH AN EXAMPLE OF AGGREGATION.
• CREATE A DIAGRAM WITH AN EXAMPLE OF COMPOSITION.
• CONSIDER ENCAPSULATION.
• CONSIDER SRP.

• YOU MAY USE HTTP://CREATELY.COM/ OR VISUAL STUDIO OR ENTERPRISE


ARCHITECT OR DRAW.IO

You might also like