Object-Oriented Concepts – An Introduction
In this section, first we will discuss the basic mechanisms in object-oriented paradigm. We will then discuss some key concepts and a few related technical terms.
Basic Entities
Classes, Objects, Attributes, and Methods: Similar objects constitute a class. This means, objects possessing similar attributes and displaying similar behaviour constitute a class. For example, all employee objects have similar attributes such as his name, code number, salary, address, etc. and exhibits similar behaviour as other employee objects. Once the class is defined, it serves as a template for object creation. Since each object is created as an instance of some class, classes can be considered as abstract data types (ADTs).
In the object-oriented approach, a system is designed as a set of interfacing objects. Normally, each object represents a tangible real-world entity such as a library member, an employee, a book, etc. Objects are basically class variables. However, at times some conceptual entities can be considered as objects (e.g. a scheduler, a controller, etc.) to simplify solutions to certain problems. When a system is analysed, developed, and implemented in terms of the natural objects occurring in it, it becomes easier to understand the design and implementation of the system. Each object essentially consists of some data that are private to the object and a set of functions that operate on those data as shown in fig.37.1. In fact, the functions of an object have the sole authority to operate on the private data of that object. Therefore, an object can not directly access the data internal to another object. However, an object can indirectly access the internal data of the other objects by invoking the operations (i.e. methods) supported by those objects.
Fig. 37.1 A Model of an object
The data internal to an object are called the attributes of the object, and the functions supported by an object are called its methods. Fig. 37.2 shows LibraryMember class with eight attributes and five methods.
Data Abstraction
Data abstraction means that each object hides (abstracts away) from other objects the exact way in which its internal information is organized and manipulated. It only provides a set of methods, which other objects can use for accessing and manipulating this private information of the object. Other objects can not directly access the private data of an object. For example, a stack object might store its internal data either in the form of an array of values or in the form of a linked list. Other objects would not know how exactly this object has stored its data and how it manipulates its data. What they would know is the set of methods such as push, pop, and top-ofstack that it provides to the other objects for accessing and manipulating the data.
Advantages of Data Abstraction
An important advantage of the principle of data abstraction is that it reduces coupling among the objects. Therefore, it reduces the overall complexity of a design, and helps in maintenance and code reuse.
LibraryMember |
Member Name Membership Number Address Phone Number E-Mail Number Membership Admission Date Membership Expiry Date Books Issued |
issueBook(); findPendingBooks(); findOverdueBooks(); returnBook(); findMembershipDetails(); |
Fig. 37.2 A class model with attributes and methods
Inheritance
The inheritance feature allows us to define a new class by extending or modifying an existing class. The original class is called the base class (or super class) and the new class obtained through inheritance is called as the derived class (or sub class). A base class is a generalization of its derived classes. This means that the base class contains only those properties that are common to all the derived classes. Again each derived class is a specialization of its base class because it modifies or extends the basic properties of the base class in certain ways. Thus, the inheritance relationship can be viewed as a generalization-specialization relationship. Using the inheritance relationship, different classes can be arranged in a class hierarchy (or class tree). In addition to inheriting all properties of the base class, a derived class can define new properties. That is, it can define new data and methods. It can even give new definitions to methods which already exist in the base class. Redefinition of methods which existed in the base class is called as method overriding. In fig. 37.3, LibraryMember is the base class for the derived classes Faculty, Student, and Staff. Similarly, Student is the base class for the derived classes Undergraduate, Postgraduate, and Research.
Each derived class inherits all the data and methods of the base class. It can also define additional data and methods or modify some of the inherited data and methods. The different classes in a library automation system and the inheritance relationship among them are shown in the fig. 37.3. The inheritance relationship has been represented in fig. 37.3 using a directed arrow drawn from a derived class to its base class. In fig. 37.3, the LibraryMember base class might define the data for name, address, and library membership number for each member. Though Faculty, Student, and Staff classes inherit these data, they might have to redefine the respective issue-book methods because the number of books that can be borrowed and the duration of loan may be different for the different category of library members. Thus, the issue-book method is overridden by each of the derived classes and the derived classes might define additional data max-number-books and max-duration-ofissue which may vary for the different member categories.
Fig. 37.3 Library Information System Example
Object-Oriented Vs. Object-Based Languages
Languages that support classes (Abstract Data Types) but do not support inheritance are called object-based languages. On the other hand, languages that support both classes as well as inheritance are called object-oriented languages.
Advantages of Inheritance
An important advantage of the inheritance mechanism is code reuse. If certain methods or data are similar in asset of classes, then instead of defining these methods and data each of these classes separately, these methods and data are defined only once in the base class and are inherited by each of its subclasses. For example, in the Library Information System example of fig. 37.3, each category of member objects Faculty, Student, and Staff need the data membername, member-address, and membership-number and therefore these data are defined in the base class LibraryMember and inherited by its subclasses.
Another advantage of the inheritance mechanism is the conceptual simplification that comes from reducing the number of independent features of the classes.
Multiple Inheritance
Multiple inheritance is a mechanism by which a sub class can inherit attributes and methods from more than one base class. Suppose research students are also allowed to be staff of the institute, then some of the characteristics of the Research class might be similar to the Student class and some other characteristics might be similar to the Staff class. Such a class hierarchy can be represented as in fig. 37.4. Multiple inheritance is represented by arrows drawn from the subclass to each of the base classes. The class Research inherits features from both the classes Student and Staff.
Fig. 37.4 Library Information System example with multiple inheritance.
Encapsulation
The property of an object by which it interfaces with the outside world only through messages is referred to as encapsulation. The data of an object are encapsulated within its methods and are available only through message-based communication. This concept is schematically represented in fig. 37.5.
Fig. 37.5 Schematic representation of the concept of encapsulation
Advantages Of Encapsulation
Encapsulation offers three important advantages:
Polymorphism
Polymorphism literally means poly (many) morphs (forms). Broadly speaking, polymorphism denotes the following:
Static Binding
An example of static binding is the following. Suppose a class named Circle has three definitions for the create operation. One definition does not take any argument and creates a circle with default parameters. The second definition takes the center point and radius as its parameters. In this case, the fill style values for the circle would be set to default “no fill”. The third takes the centre point, the radius, and the fill style as its input. When the create method is invoked, depending on the parameters given in the invocation, the appropriate method will be called. If create is invoked with no parameters, then a default circle would be created. If only the centre and the radius are supplied, then an appropriate circle would be created with no fill type, and so on. A class definition of the Circle class with the overloaded create method is shown in fig. 37.6. When the same operation (e.g. create) is implemented by multiple methods, the method name is said to be overloaded.
Class Circle
{
private: float x, y, radius:
int fillType;
public:
create();
create (float x, float y, float centre);
create (float x, float y, float centre, int fillType);
}
Fig. 37.6 Circle class with overloaded create method
Dynamic Binding
Using dynamic binding a programmer can send a generic message to a set of objects which may be of different types (i.e., belonging to different classes) and leave the exact way in which the message would be handled to the receiving objects. Suppose we have a class hierarchy of different geometric objects in a drawing as shown in fig. 37.7. Now, suppose the display method is declared in the shape class and is overridden in each derived class. If the different types of geometric objects making up a drawing are stored in an array of type shape, then a single call to the display method for each object would take care to display the appropriate drawing element. That is, the same draw call to a shape object would take care of drawing the appropriate shape. This code segment is shown in fig. 37.8.
Fig. 37.7 Class hierarchy of geometric objects
Traditional Code | Object-oriented Code |
if(shape == Circle) then draw_circle(); else if(shape == Rectangle)then draw_rectangle(); ____ | shape.draw(); |
Fig. 37.8 Traditional code and object-oriented code using dynamic binding
Advantages of Dynamic Binding
The main advantage of dynamic binding is that it leads to elegant programming and facilitates code reuse and maintenance. With dynamic binding, new derived objects can be added with minimal changes to existing objects. This advantage of polymorphism can be illustrated by comparing the code segments of an object-oriented program and a traditional program for drawing various graphic objects on the screen. It can be assumed that the shape is the base class, and the classes Circle, Rectangle, and Ellipse are derived from it. Now, shape can be assigned any objects of type Circle, Rectangle, Ellipse, etc. But, a draw method invocation of the shape object would invoke the appropriate method. It can be easily seen in fig. 37.8 that, because of dynamic binding, the object-oriented code is much more concise and intellectually appealing. Also, suppose in the example program segment, it is later found necessary to handle a new graphics drawing primitive, say Ellipse, then, the procedural code has to be changed by adding a new if-then-else clause. However, in case of the object-oriented program, the code need not change. Only a new class called Ellipse has to be defined.
Advantages of the Object-Oriented Design
In the last few years that OOD has come into existence, it has found widespread acceptance in industry as well as in academics. The main reason for the popularity of OOD is that it holds the following promises:
Out of all these advantages, the chief advantage of OOD is improved productivity – which comes about due to a variety of factors, such as
47 videos|69 docs|65 tests
|
|
Explore Courses for Computer Science Engineering (CSE) exam
|