Designing for Inheritance

Slides:



Advertisements
Similar presentations
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 211 Inheritance AAA.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
More Inheritance Abstract Classes Interfaces Briana B. Morrison CSE 1302C Spring 2010.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 9: Polymorphism Coming up: Binding.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
1 Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L 9.1 – 9.3.
Abstract Classes.
1 Inheritance and Polymorphism Inheritance (Continued) Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
ECE122 L22: Polymorphism Using Inheritance April 26, 2007 ECE 122 Engineering Problem Solving with Java Lecture 22 Polymorphism using Inheritance.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Aalborg Media Lab 15-Jul-15 Polymorphism Lecture 12 Chapter 9.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
1 Polymorphism  A reference can be polymorphic, which can be defined as "having many forms" obj.doIt();  This line of code might execute different methods.
© 2006 Pearson Education Chapter 7: Inheritance Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
Chapter 7: Inheritance Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java Software.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
Chapter 9: Polymorphism Coming up: Creating Objects Revisited.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
© 2004 Pearson Addison-Wesley. All rights reserved April 17, 2006 Polymorphism (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
1 Advanced Polymorphism Polymorphism Review Comparator Interface Sorting with Comparators Selection Sort Insertion Sort.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Polymorphism What is it, why it is needed? Dynamic binding Sorting and searching.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Modern Programming Tools And Techniques-I
Chapter 5: Enhancing Classes
Polymorphism November 27, 2006 ComS 207: Programming I (in Java)
Chapter 9 Polymorphism.
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Polymorphism.
Chapter 7: Inheritance Java Software Solutions
Interfaces and Inheritance
Object-Oriented Programming: Polymorphism
Object Oriented Programming
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Overriding Methods & Class Hierarchies
Review of Previous Lesson
Chapter 9 Polymorphism.
Chapter 8 Inheritance Part 2.
Lecture 0311 – Polymorphism
Programming in C# CHAPTER 5 & 6
Presentation transcript:

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.

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

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.

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();

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

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

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

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.)

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.

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.

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