OOP. Meaning of A Type
OOP. Meaning of A Type
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.
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;
• subclass, derived class, child class: terms to describe the child in the
relationship, which accepts functionality from its parent
― 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.”
• 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.