Presentation is loading. Please wait.

Presentation is loading. Please wait.

Designing for Inheritance

Similar presentations


Presentation on theme: "Designing for Inheritance"— Presentation transcript:

1 Designing for Inheritance
As a major characteristic of object-oriented software, inheritance must be carefully considered during software design. A little thought about inheritance relationships can lead to afar better design, which is very important in the long term.

2 Designing for Inheritance
Inheritance should be carefully considered during software design Every derivation should be an is-a relationship Design a class hierarchy so that it can be reused in the future Use interfaces to create a class that serves multiple roles (simulating multiple inheritance) Override general methods such as toString and equals appropriately See page 388 for more items to keep in mind during design

3 Polymorphism A reference can be polymorphic, which can be defined as "having many forms" obj.doIt(); This line of code might execute different methods at different times if the object that obj points to changes Polymorphic references are resolved at run time; this is called dynamic binding Careful use of polymorphic references can lead to elegant, robust software designs Polymorphism can be accomplished using inheritance or using interfaces The term polymorphism means “having many forms.” A polymorphic reference is a reference variable that can refer to different types of objects at different times. The method invoked through a polymorphic reference can change from one time to the next. Take for example, the obj.doIt(); method. If there reference obj is polymorphic, it can refer to different types of objects at different times. If that line of code is in a lop in in a method that is called more than once, that line of code might call a different version of the doIt method each time it is invoked. At some point, the computer has to execute the code to carry out a method invocation. This is called binding a method invocation to a method definition. Most of the time binding happens at compile time. For polymorphic reference however, binding can’t be done until runtime because which object is being referenced can change. This is referred to as late binding or dynamic binding. It is less efficient than binding at compile time because the decision must be made during the execution of the program. But the flexibility that at polymorphic reference gives us makes up for that. We can create a polymorphic reference in Java in two ways: using inheritance and using intefaces.

4 References and Inheritance
An object reference can refer to an object of its class, or to an object of any class related to it by inheritance For example, if the Holiday class is used to derive a child class called Christmas, then a Holiday reference could be used to point to a Christmas object Holiday Christmas Holiday day; day = new Christmas();

5 References and Inheritance
Assigning a predecessor object to an ancestor reference is considered to be a widening conversion, and can be performed by simple assignment Assigning an ancestor object to a predecessor reference can be done also, but it is considered to be a narrowiang conversion and must be done with a cast The widening conversion is the most useful An Object reference can be used to refer to any object

6 Polymorphism via Inheritance
It is the type of the object being referenced, not the reference type, that determines which method is invoked Suppose the Holiday class has a method called celebrate, and the Christmas class overrides it Now consider the following invocation: day.celebrate(); If day refers to a Holiday object, it invokes the Holiday version of celebrate; if it refers to a Christmas object, it invokes the Christmas version

7 Polymorphism via Inheritance
Consider the following class hierarchy: StaffMember Executive Hourly Volunteer Employee The classes illustrated here represent kinds of employees at a company.

8 The firm class contains a main driver that create a staff of employees and invokes the payday method to pay them all. The program output includes information about each employee and how much each is paid (if anything.)

9 The staff class is an array of objects that represent individual employees. Note that the array is declared to hold StaffMember references, but it is actually filled with objects created from several other classes, such as Executive and Employee. These classes are all descendants of the StaffMember class, so the assignments are valid. The payday method of the Staff class scans through the list of employees, printing their information and invoking their pay methods to determine who much each employee should be paid. The innovation of the pay method is polymorphic because each class had its own version of the pay method. The StaffMember class (not shown here) is abstract. It does not represent a particular type of employee and is not meant to be instantiated. Rather, it serves as the parent of all employee classes and contains information that applies to all employees. Each employee has a name, address, and phone number. So variables to store these values are declared in the StaffMember class. The StaffMember class also contains a toString method to return the information managed by the StaffMember class. It also contains an abstract method called pay, which takes no parameters and returns a value of type double. We can’t define this method at the StaffMember lele because each type of employee gets paid in a different way. The descendants of StaffMember each provide their own definition for pay. Because pay is abstract in StaffMember, the payday method Staff can polymorphically pay each employee. The Volunteer class represents a person who does not get paid. We keep track only of a volunteer’s basic information, which is passed into the constructor Volunteer., which in turn passes it to the StaffMember constructor using the super reference. The pay method of Volunteer simply returns a zero pay value. If pay had not been overridden, the Volunteer class would have been considered abstract and could not have been instantiated.

10 Polymorphism via Interfaces
An interface name can be used as the type of an object reference variable Doable obj; The obj reference can be used to point to any object of any class that implements the Doable interface The version of doThis that the following line invokes depends on the type of object that obj is referencing obj.doThis(); As we’ve seen many times, a class name is used to declare the type of an object reference variable. In the same way, an interface name can be used as the type of a reference variable as well. An interface reference variable can be used to refer to any object of any class that implements that interface.

11 Designing for Polymorphism
During the design phase, opportunities for polymorphic solutions should be identified Use polymorphism when different types of objects perform the same type of behavior Identifying polymorphic opportunities comes easier with experience


Download ppt "Designing for Inheritance"

Similar presentations


Ads by Google