Object and methods :
Object oriented programs are made up of objects. An object contains both data and the procedures that operate on that data. The procedures are typically called methods or operations. An object performs an operation when it receives a request ( or message ) from a client.
Operation or method signature :
Every operation declared by an object specifies the operation's name, the objects or values it takes as parameters, and the operation's return value. This is known as the operation's signature.
Concept of Encapsulation :
Requests are the only way to get an object to execute an operation. Operations are the only way to change an object's internal data. Because of these restrictions, the object's internal state is said to be encapsulated. Object's internal data can not be accessed directly, and its representation is invisible from outside the object. Note: Data restrictions are defined using the data access specifier like, private, protected, internal, public keyword on methods based on language.
Interface to Object :
The set of all signatures defined by an object's operations is called the interface to the object. An object's interface characterizes the complete set of requests that can be sent to the object. Any request that matches a signature in the object's interface may be sent to the object.
Type :
A type is a name used to denote a particular interface. For example, if we define sets of operation of object A, as "INameOperation", we can say type of object A is INameOperation.
An object may have many types, and widely different objects can share a type.
Part of an object's interface ( means some set of operation of an object ) may be characterized by one type, and other parts by other types. That means, an object can be of different types.
Two different object can be of same type, when sets of operation is matched between two objects.
SubType :
Interfaces can contain other interfaces as subsets. We say that a type is a subtype of another if its interface contains the interface of its supertype. Often we call of a subtype inheriting the interface of its supertype.
Concept of Program to Interface ( P2I ) :
Interfaces are fundamental in object-oriented systems. Objects are known only through their interfaces. There is no way to know anything about an object or to ask it to do anything without going through its interface. An object's interface says nothing about its implementation that's why different objects are free to implement requests differently. That means two objects having completely different implementations can have identical interfaces.
Dynamic binding :
When a request is sent to an object, the particular operation that's performed depends on both the request and the receiving object. Different objects that fulfill these requests may have different implementations of the operations that fulfill these requests. The run-time association of a request to an object and one of its operations is known as dynamic binding.
For example :
Lets define interface called IShape :
interface IShape{
float calculateArea();
}
Let's define Rectangle class of type IShape :
class Rectangle implements IShape{
private float width = 10.2;
private float height = 4.8;
float calculateArea(){
return this.width * this.height;
}
}
Let's define Circle class of typpe IShape :
class Circle implements IShape{
private const float PI = 3.14;
private float radius = 10.2;
float calculateArea(){
return this.PI * this.radius * this.radius;
}
}
Let's define Client class, which uses the interface of object rectangle and circle (Note: this Client class has main method which initiate the program ) :
class Client{
public void main(string[] args){
IShape shapeObjType;
//Let's create a object of type IShape interface newing Rectangle class
IShape rectantleObj = new Rectangle();
//Lets create a object of same IShape type interface newing Circle class
IShape circleObj = new Circle();
//Now to calculate area of rectangle, let's call a interface method "calculateArea". At the run-time ( means ) when program is running, request is sent to an rectangle object. This run time association of a client request to an Rectangle object (or circle object) and it's operation "calculateArea" is called dynamic binding.
//Store a calculated result in areaOfRectangle variable
float areaOfRectangte = rectangleObj.calculateArea();
//Store calculated area of cicrle in areaOfCircle variable
float areaOfCircle = circleObj.calculateArea();
}
Note : During compilation time, only rectangle and circle class of type IShape is defined. In run-time, object of Rectangle and Circle class is of IShape type is created, and while calculating a area decision is made, which object, which operations the request is sent.
So now, What is Polymorphism?
Dynamic binding means that issuing a request doesn't commit you to a particular implementation until run-time. Consequently, we can write programs that expect an object with a particular interface, knowing that any object that has the correct interface will accept the request. Moreover, dynamic binding lets you substitute objects that have identical interfaces for each other at run-time. This sustainability is known as Polymorphism.
Reference:
1. Design Patterns, Elements of Reusable Object-Oriented Software. Author : Gang of Four ( Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides )
Object oriented programs are made up of objects. An object contains both data and the procedures that operate on that data. The procedures are typically called methods or operations. An object performs an operation when it receives a request ( or message ) from a client.
Operation or method signature :
Every operation declared by an object specifies the operation's name, the objects or values it takes as parameters, and the operation's return value. This is known as the operation's signature.
Concept of Encapsulation :
Requests are the only way to get an object to execute an operation. Operations are the only way to change an object's internal data. Because of these restrictions, the object's internal state is said to be encapsulated. Object's internal data can not be accessed directly, and its representation is invisible from outside the object. Note: Data restrictions are defined using the data access specifier like, private, protected, internal, public keyword on methods based on language.
Interface to Object :
The set of all signatures defined by an object's operations is called the interface to the object. An object's interface characterizes the complete set of requests that can be sent to the object. Any request that matches a signature in the object's interface may be sent to the object.
Type :
A type is a name used to denote a particular interface. For example, if we define sets of operation of object A, as "INameOperation", we can say type of object A is INameOperation.
An object may have many types, and widely different objects can share a type.
Part of an object's interface ( means some set of operation of an object ) may be characterized by one type, and other parts by other types. That means, an object can be of different types.
Two different object can be of same type, when sets of operation is matched between two objects.
SubType :
Interfaces can contain other interfaces as subsets. We say that a type is a subtype of another if its interface contains the interface of its supertype. Often we call of a subtype inheriting the interface of its supertype.
Concept of Program to Interface ( P2I ) :
Interfaces are fundamental in object-oriented systems. Objects are known only through their interfaces. There is no way to know anything about an object or to ask it to do anything without going through its interface. An object's interface says nothing about its implementation that's why different objects are free to implement requests differently. That means two objects having completely different implementations can have identical interfaces.
Dynamic binding :
When a request is sent to an object, the particular operation that's performed depends on both the request and the receiving object. Different objects that fulfill these requests may have different implementations of the operations that fulfill these requests. The run-time association of a request to an object and one of its operations is known as dynamic binding.
For example :
Lets define interface called IShape :
interface IShape{
float calculateArea();
}
Let's define Rectangle class of type IShape :
class Rectangle implements IShape{
private float width = 10.2;
private float height = 4.8;
float calculateArea(){
return this.width * this.height;
}
}
Let's define Circle class of typpe IShape :
class Circle implements IShape{
private const float PI = 3.14;
private float radius = 10.2;
float calculateArea(){
return this.PI * this.radius * this.radius;
}
}
Let's define Client class, which uses the interface of object rectangle and circle (Note: this Client class has main method which initiate the program ) :
class Client{
public void main(string[] args){
IShape shapeObjType;
//Let's create a object of type IShape interface newing Rectangle class
IShape rectantleObj = new Rectangle();
//Lets create a object of same IShape type interface newing Circle class
IShape circleObj = new Circle();
//Now to calculate area of rectangle, let's call a interface method "calculateArea". At the run-time ( means ) when program is running, request is sent to an rectangle object. This run time association of a client request to an Rectangle object (or circle object) and it's operation "calculateArea" is called dynamic binding.
//Store a calculated result in areaOfRectangle variable
float areaOfRectangte = rectangleObj.calculateArea();
//Store calculated area of cicrle in areaOfCircle variable
float areaOfCircle = circleObj.calculateArea();
}
Note : During compilation time, only rectangle and circle class of type IShape is defined. In run-time, object of Rectangle and Circle class is of IShape type is created, and while calculating a area decision is made, which object, which operations the request is sent.
So now, What is Polymorphism?
Dynamic binding means that issuing a request doesn't commit you to a particular implementation until run-time. Consequently, we can write programs that expect an object with a particular interface, knowing that any object that has the correct interface will accept the request. Moreover, dynamic binding lets you substitute objects that have identical interfaces for each other at run-time. This sustainability is known as Polymorphism.
Reference:
1. Design Patterns, Elements of Reusable Object-Oriented Software. Author : Gang of Four ( Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides )
Comments
Post a Comment