Hyderabad Jobs Book Website FREE PowerBuilder Training I Love Hyderabad Hyderabad Colleges
Home Business Emails Hyderabad Classifieds Contact Us
7 Wonders of Hyderabad Web Hosting Yellow Pages Our Network

 
Webpowerbuilder.hyderabad-colleges.com

Advanced PowerBuilder

HomePrevious Lesson: Objects Basics
Next Lesson: Using Inheritance in PowerBuilder

Inheritance

Inheritance is a mechanism which allows objects to share attributes and services based on a predefined relationship; it allows you to define a hierarchy of objects. The inherited object is called a descendant object, while the object from which the descendant is inherited is called an ancestor.

If we define a vehicle on certain attributes such as height, weight, speed, maximum load and so on. Even though vehicles share these common attributes, they are not same. We might have a car, a truck and an emergency vehicle, each with their own specific attributes.

The first thing we need to do is define a class, called Vehicle, which has the general attributes of our vehicles. From there on, we can derive other classes such as a Car class and a Truck class by inheriting from this Vehicle class. The Vehicle class is called the ancestor, and the Car and Truck classes are called descendants.

Inheritance hierarchies are a means of classifying objects. They are inverted trees of common subroutines. We use the term inverted, to convey the sense that, the commonalty of objects are portrayed upwardly as in a tree as compared to the downward representation of common modules in a structure chart. Each higher object represents a set of services and attributes common to all levels to which it is connected below.

Each object in the tree is called the class of the objects below it. An object will inherit services and attributes as common characteristics from the class to which it belongs. This means that you need not redesign these characteristics every time you use them in an object, yet can reference them as if they were a part of the object.

Base Classes

In this example, Vehicle class is called a base class. Base classes are not meant to be used directly, but they are used to define the general attributes that appear in the derived objects. As you descend the hierarchy, you define more specific attributes and functionality.

The descendant class, a Taxi, inherits all attributes, variables, structures, functions and behavior from the ancestor, the Car class. An ancestor can have any number of descendant classes, and a descendant class can become an ancestor to other descendants. The attributes defined at the ancestor level are not copied into the descendant class, but are passed down to the descendant for reference.

If you change attributes at the ancestor level, the changes reflect in descendent objects immediately. At the same time, changes done at the descendent level are not reflected at the ancestor level. In the above example, additional functionality defined at Car class is available to Taxi, but not to the Vehicle class. This allows you to localize changes and localize testing.

Inheriting the Code

You can design class hierarchies just for code reuse or for sharing the interface or a mix of these two. In this section let's examine inheritance for code reuse.

Let's consider a program for designing data entry forms, where user fills out fields on the screen. The data entry forms may be for different purposes, i.e., entering a record in the inventory master, or entering a transaction into a transaction file, or for a purchase order. Note that, all these forms share functionality, such as, querying the form, printing the form. As a result, you can implement this functionality in the base class. You can save yourself the effort and as well as reduce the size of the program by defining a base class called data-entry-form that implements the above mentioned common functionality. Such a class hierarchy also reduces the effort required to fix bugs or add features, since the changes have to be done in one place only.

A class hierarchy, designed for code sharing has most of its code in the base class ( near the top of the hierarchy ). This way the code can be reused in many classes. The derived classes represent the specialized classes, i.e., extend the base class's functionality.

Inheriting the Interface

Using this strategy, you can design class hierarchy to inherit just the interface by the derived classes, and not the code. You write the actual code in the derived class. By this, the derived class has the same interface ( in other words, same function names and arguments ) as the base class, but behave differently for the same method.

Consider the previous example, every data-entry-form class, i.e., master entry, transaction entry have some common functionality, saving the data entered by the user. Since all these classes have common functionality, you may want to define the method "Save" at the base class. However, the implementation of saving the data is different from form to form. For example, master entry form is saving the data to the master file, while transaction entry form has to check for existence of the information in the master file and also update the master file whenever it writes to the transaction file. Thus, individual form objects may exhibit different behaviors, but they all share the same interface and so can be treated as 'data-entry-form' objects.

In this situation, you can define a method "Save" at the base class with no code in it and write the actual code in the derived classes. To do this, you need to use polymorphism, which is explained later in this chapter.

A class hierarchy designed for interface sharing has most of its code in the derived classes ( near the bottom of the hierarchy ). The derived classes represent working versions of an abstract model defined by the class.

Some times, you may need to design a class hierarchy, which has to implement both code and interface inheritance. If you take the examples from both the topics, and combine them, i.e., some methods, and  some interfaces are common. So, the final diagram would look like the one shown below:

Multiple Inheritance

Inheritance hierarchies need not be limited to single inheritance. Multiple inheritance is complex to understand, but it follows the same principles as single inheritance. Multiple inheritance is a set of many-to-many parent/child relationships among the classes associated with objects. Multiple inheritance allows an object to inherit functionality from more than one object.

In the above example, the Ambulance class not only inherits functionality from the Car, but also from the Emergency class. It therefore gets all the features of an Emergency vehicle, together with those on offer from the Car.

Multiple inheritance is useful in situations where there are multiple groups of common characteristics, which partially overlap one another.

Benefits of Inheritance

Allows reuse of code.
Reduces development time.
Improves consistency, both internally within the application and between them.
Reduces the chances of error.
Makes maintenance easier.
Reduces space usage.
HomePrevious Lesson: Objects Basics
Next Lesson: Using Inheritance in PowerBuilder

Copyright © 1996 - 2006 HamaraShehar.com Pvt. Ltd. All Rights Reserved.
Domain Registration, Website Design, Website Hosting by HamaraShehar.com