CSC/ECE 517 Fall 2010/ch3 S30 SK: Difference between revisions
No edit summary |
No edit summary |
||
Line 12: | Line 12: | ||
===Inheritance=== | ===Inheritance=== | ||
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects. In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects. | In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects. In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects. | ||
[[Image:Inheritance.jpg|frame|center|Inheritance Hierarchy]] | |||
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from Teacher and Student and in turn form Person. | In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from Teacher and Student and in turn form Person. |
Revision as of 00:19, 6 October 2010
Decomposition, message forwarding, and delegation versus inheritance in OOP
Introduction
Object oriented programming (OOP) paradigm is based upon what is known as "objects" which represent real world concepts. One of the principal advantages of object-oriented programming over procedural programming is that they enable programmers to create modules that do not need to be changed when a new type of object is added. Some basic concepts/features which are part of OOP are classes, methods, instances, encapsulation, inheritance, message forwarding, decomposition, delegation etc. This article discusses four of these concepts, namely, inheritance, message forwarding, decomposition and delegation. Specifically, comparison between inheritance and message forwarding, decomposition & delegation is made to understand when each of these can be considered as a potential alternative for inheritance, which forms core of OOP.
Elements of OOP
Some of the elements of OOP, relevant to the discussion of the topic are discussed below
Inheritance
In OOP, inheritance is a way to compartmentalize the code by creating objects which are based on previously created objects. In classical inheritance where objects are defined by classes, parent class is known as super class and the one which inherits behavior is known as sub class. Inheritance is used to relate two or more objects to each other. With the use of inheritance, methods and instance variables of the parent object are available for use in all of its child objects.
In the above example, student and teacher both inherit characters from Person object and Teaching Assistant inherits characteristics from Teacher and Student and in turn form Person.
Decomposition
Decomposition, also known as factoring is the process by which a big, complex problem is broken into smaller problems which are easier to attack, conceive and manage. Object Oriented Decomposition breaks large problem domain, based on classification entities, into smaller objects which represents some part of the problem. Object oriented decomposition works especially fine due to the concept of procedural abstraction which makes a problem easier to solve. Object-oriented decomposition results in a product that is more reusable.
For example, we could have an object referring to Drawer which supports addition of information and an object Letter which supports retrieval of content. In this case, a bigger problem domain (Drawer) refers to an object (Letter) which handles smaller problem domain. The fact that Drawer can have many letters makes decomposition relation of type "has a".
In the above example, Drawer object holds an instance variable which refers to Letter object. The bigger problem domain of "Drawer" has now been divided into smaller problem domains, one of which is Letter. Letter behavior is easier to maintain as compared to Drawer and any change in Drawer behavior will not affect Letter's behavior. Moreover, letter object can be part of some other big problem domain also(except of drawer).
Message Forwarding
Message Forwarding, also known as interfacing, describes the communication between objects using their public interfaces. Using message forwarding, objects can send and receive messages from each other, which help them to synchronize. Message passing is done using methods, properties and events. Message forwarding using method involves invoking an operation on an object. In response to the message the corresponding method is executed in the object and object may return some values. Message forwarding using property allows object to advertise and allow change in their state information. Events are raised by the objects in response to an internal action. Other objects can subscribe to these so that they can react to an occurrence of the event. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.
In some object-oriented programming languages, a message is the means to pass control to an object and if the object responds to the message, which means it has some way to handle that message. In pure object-oriented programming, message passing is performed exclusively through a dynamic dispatch strategy (process of mapping a message to a specific sequence of code at runtime.). Message forwarding techniques for communication between objects makes the interface descriptions with external systems much simpler.
Java employs message forwarding in the form of message calling. Objective C and Smalltalk heavily rely of message passing and use message sending instead of message calling.
As an example for message forwarding, when the object called "joe" (an instance of the driver class), presses the gas pedal, he literally passes an accelerate message to object "my Porsche", which in turn, invokes the "my Porsche" accelerate method.
Delegation
In OOP, two notions of delegation exist. The older usage refers to passing on the execution to some other object. While new usage refers to using method lookup rules to dispatch so-called self-calls as defined by Lieberman in his 1986 paper "Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems".
Example for old definition:
Suppose there are class A and B defined as follows:
class A { public: foo() { print("Object A doing the job"); } } class B { public: A a; foo() { a.foo(); } }
Here class B delegates the execution of its function foo to an object of class A.
Example of new definition:
class A { foo() { self.bar() // self is also known under the names "current" and "this" in other languages } bar() { print("a.bar") } } class B { delegationlink A a foo() { a.foo() // call foo() on the a-instance } bar() { print("b.bar") } } a = new A() b = new B(a) // establish delegation between two objects
calling b.foo() will result in b.bar to be printed. Older definition would have resulted in a.bar.
Example taken from http://www.wordiq.com/definition/Delegation_(programming)
It should be noted that support for delegation as new definition is not widely supported in programming languages, though exceptions exists in the form of the languages Self and Kniesels Lava.
Delegation can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate. Delegation is based on dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. Delegation forms the core of design patterns like wrapper design pattern.