Download presentation
Presentation is loading. Please wait.
1
Inheritance and Polymorphism
Chapter 2 1/12/2019 BR
2
Inheritance Inheritance is a relationship between classes where one class is the parent class of another. Inheritance provides a classification for types of objects and allows for the common properties and behaviors of objects to be explicitly taken advantage of in modeling and implementation object systems. Inheritance implements “is-a-kind-of” relationship among classes. Inheritance exploits common features among abstractions and this leads to hierarchy, polymorphism, generalization-specialization, solution patterns. 1/12/2019 BR
3
Example: Bank Hierarchy
1/12/2019 BR
4
Inheritance (contd.) The class at the general end of a inheritance hierarchy is called a base class, and the classes that are derived from it “derived” classes. When a class in a hierarchy has incomplete specification in its behavior definition we call it an “abstract class” and the incomplete behavior an abstract method. When all the methods are abstract then we have special class called the “interface”. In fact, an interface specifies just what to do without any details about how to do it. In fact, while designing systems you ought to start with its interfaces. In design by contract methodology “interface” serves as the contract. Interface is a contract of services offered by the system. Interface could specify a set of policies without defining how to implement them. 1/12/2019 BR
5
Abstract Class (ABC) and methods
An abstract class is a class in which one or more methods are declared but not defined. Declaration of the methods can be omitted since it makes no sense to implement them in the super class. Am abstract method has “abstract” modifier in front of it. An abstract base class (ABC) cannot be instantiated. Sole purpose of ABC is to provide an appropriate super class from which classes may inherit. Classes from which objects can be instantiated are called concrete classes. 1/12/2019 BR
6
Abstract Classes Point, circle, cylinder are related and well defined concrete classes. But consider a class TwoDimensionalShape. Can you visualize anything? You know that it has an area, perimeter (and not volume) That is, you know that it has some length and width, area, and perimeter. But you cannot compute these unless a shape name is given. This is an example of an ABC. 1/12/2019 BR
7
Abstract class and Interface
public abstract class aEPA { public abstract void emissionControl(); public abstract clearAir(); public abstract planOperation();} public interface iEPA { public void emissionControl(); public clearAir(); public planOperation();} 1/12/2019 BR
8
EPA Hierarchy (Part 1: CA)
public interface EPAPolicy { public void emissionControl(); public clearAir(); public planOperation();} class abstract CAEPA implements iEPA { public void emissionControl () { // CA implementation } public void cleanAir() {// CA implementation} public void abstract planOperation(); //don’t know} class SanJoseEPA extends CAEPA { public void planOperation() { // implementation1 complete } class SacrementoEPA extends CAEPA { public void planOperation() { // implementation2 complete } 1/12/2019 BR
9
EPA Hierarchy (Part 2: NY)
public interface EPAPolicy { public void emissionControl(); public clearAir(); public planOperation();} class abstract NYEPA implements EPAPolicy { public void emissionControl () { // NY implementation } public void cleanAir() {// NY implementation} public void abstract planOperation(); //don’t know} class BuffaloEPA extends NYEPA { public void planOperation() { // implementation3 complete } class AlbanyEPA extends NYEPA { public void planOperation() { // implementation4 complete } 1/12/2019 BR
10
The Whole picture 1/12/2019 BR
11
Polymorphism Polymorphism comes many flavors: See Cardelli and Wegner’s paper (1985): polymorphism universal ad-hoc parametric inclusion overloading coercion subtype 1/12/2019 BR
12
Polymorphism: Many forms
Overloading is a simple form. Ex: “+” operator Overriding is a method in sub class replacing a method in super class. Subtyping is applicable to interfaces as well as classes. Pure subtype polymorphism is when a method call is dynamically dispatched based on the runtime type of the object. We will look at examples of each of these in the next example. 1/12/2019 BR
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.