OBJECT-ORIENTED APPROACH TERMINOLOGY
Object model is the conceptual framework for Object-Oriented approaches.
Four major elements of object model
1. Abstraction
2. Encapsulation
3. Modularity
4. Hierarchy
Three minor elements of object model
1. Typing
2. Concurrency
3. Persistence
Object-oriented Analysis (OOA) is a method of analysis that examines requirements from the perspective of the
classes and objects found in the vocabulary of the problem domain.
Object-oriented Design (OOD) is the method of designing the characteristics (logical, physical, static, dynamic, etc.)
of the system representing it as a collection of objects.
Object-Oriented Programming (OOP) is a programming approach in which programs are organized as cooperative
collections of objects.
An Object is an entity that has state, behaviour, and identity. The structure and behaviour of an object confirms to
the definition of its class. An object is said to be an instance of its class.
A Class defines common structure, behaviour and semantics for its instance objects.
An Abstraction is the set of essential characteristics of an object from the perspective of a viewer. It defines a crisp
conceptional boundary between the viewer and the object’s implementation. (page 44)
Example
Abstraction: Temperature Sensor
Important Characteristics:
temperature
location
Responsibilities:
report current temperature
calibrate
Encapsulation is the technique of hiding the details of the implementation of an object. It separates the object’s
contractual interface and its implementation. (page 52)
Contractual Interface Implementation
(behaviour) (structure)
Viewer sees ONLY this.
Implementation details
not exposed here.
Example
Stack Object
Stack Abstraction Stack Implementation
Push() Linked list implementation
Pop() Node definition
Peek() Top node pointer
IsEmpty()
Same abstraction but different implementation. Another object can use either of the two stack objects in exactly the
same way.
Stack Object
Stack Abstraction Stack Implementation
Push() Array implementation
Pop() Max array size
Peek() Top element array index
IsEmpty()
Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled
modules.
Hierarchy is a ranking or ordering of abstractions.
Typing is the enforcement of the class of an object, such that objects of different types may not be interchanged, or
at the most, they may be interchanged only in very restricted ways.
Concurrency is the property that distinguishes an active object from one that is not active.
Persistence is the property of an object through which its existence transcends time (i.e., the object continues to
exist after its creator ceases to exist) and/or space (i.e., the object’s location moves from the address space in which
it was created).
Inheritance
Inheritance refers to the ability of a class to take the characteristics (behaviour & structure) from other already
defined classes. A class that inherits characteristics from another class is called as a subclass (or derived class) of the
other class. The class it inherits the characteristics from is called as a superclass (or base class) of the inheriting class.
- Types of inheritance
o Single Inheritance – classes can have only a single base class
o Multiple Inheritance – classes can have multiple base classes
o Multilevel Inheritance – a subclass can be a base class for other subclasses
o Hierarchical Inheritance – a single based class from which multiple class inherit directly or indirectly
(through other classes).
o Hybrid Inheritance – A combination of one or more of the other inheritance types
o Repeated Inheritance (or Multipath inheritance) – Two or more peer superclasses share a common
superclass. In C++, virtual base classes are used for sharing of repeated base classes.
Multiple inheritance is supported in C++ and Python
C# and Java support only a single base class. But classes can implement any number of interfaces.
Polymorphism
Polymorphism refers to the approach of having a common name (or symbol) and access mechanism to invoke
multiple different operations defined present in one or more objects.
Types of polymorphisms
- Static (or compile time) polymorphism – The method called (or the class used) is fixed during code
compilation.
o Ad-Hoc polymorphism – the method invoked is decided based on the arguments passed for the
method call.
§ Method overloading – Multiple methods within the same class having the same name but
different method parameters. Method overriding without virtual functions come under this
category. Constructor overloading is also a kind of method overloading. Supported in: C++,
Java, C#.
§ Operator overloading – Methods defining behaviour of any of the existing C++ operators
(excluding a few) when used with user defined data types (classes, structures, etc).
Supported in: C++, C# (note: not supported in Java)
o Parametric polymorphism – Templates in C++. Behaviours of classes/methods are defined based on
type parameters that will be replaced with actual data types when the template classes/functions
are instantiated. Note: the same feature is called generics in Java and C#. Supported in: C++, Java, C#
- Dynamic (or runtime) polymorphism – The method called is known only at runtime depending on the object
reference (pointer) through which the method is called. This is done using virtual functions. Virtual call can
invoke overriding virtual functions in any derived class – even derived classes that did not exist when the
calling code is compiled. This type of polymorphism is also called subtype polymorphism or inclusion
polymorphism. Supported in: C++, Java, C#, Python
Dynamic polymorphism support in languages:
Language Dynamic polymorphism for methods Dynamic polymorphism for operators
C++ Yes Yes
C# Yes No (operator overloads defined as static)
Java Yes No (operator overloading not supported)
Python Yes Yes
Note:
Python has only dynamic polymorphism (all methods and operators are polymorphic by default)
Python does not support overloading.