Inheritance and Polymorphism

Slides:



Advertisements
Similar presentations
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Advertisements

Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Chapter 10: Introduction to Inheritance
More about classes and objects Classes in Visual Basic.NET.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
CC1007NI: Further Programming Week Dhruba Sen Module Leader (Islington College)
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Inheritance Extending Class Functionality. Polymorphism Review Earlier in the course, we examined the topic of polymorphism. Many times in coding, we.
Polymorphism, Abstraction and Virtual Functions. In this slide, we introduce virtual functions and two complex and powerful uses for derived classes that.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
Polymorphism Lecture - 9.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
ISBN Chapter 12 Support for Object-Oriented Programming.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
1 More About Derived Classes and Inheritance Chapter 9.
Inheritance and Polymorphism
Modern Programming Tools And Techniques-I
Object-Oriented Design
Web Design & Development Lecture 9
Advanced Programming in Java
Advanced Programming in Java
Sections Inheritance and Abstract Classes
Inheritance ITI1121 Nour El Kadri.
Inheritance and Polymorphism
EKT 472: Object Oriented Programming
One class is an extension of another.
Software Engineering Fall 2005
Object-Oriented Programming
An Introduction to Inheritance
CS250 Introduction to Computer Science II
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Class vs Abstract vs Interface
Object Oriented Programming
Inheritance B.Ramamurthy 11/7/2018 B.Ramamurthy.
Object Oriented Analysis and Design
Inheritance Chapter 7 Chapter 7.
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9: Polymorphism and Inheritance
Week 6 Object-Oriented Programming (2): Polymorphism
Java – Inheritance.
More About Inheritance & Interfaces
Advanced Programming in Java
Fundaments of Game Design
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Final and Abstract Classes
COP 3330 Object-oriented Programming in C++
Abstract Classes and Interfaces
Extending Classes Through Inheritance
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
Presentation transcript:

Inheritance and Polymorphism Chapter 2 1/12/2019 BR

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

Example: Bank Hierarchy 1/12/2019 BR

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

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

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

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

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

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

The Whole picture 1/12/2019 BR

Polymorphism Polymorphism comes many flavors: See Cardelli and Wegner’s paper (1985): http://research.microsoft.com/Users/luca/Papers/OnUnderstanding.pdf polymorphism universal ad-hoc parametric inclusion overloading coercion subtype 1/12/2019 BR

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