Object Oriented Programming: Inheritance Chapter 9.

Slides:



Advertisements
Similar presentations
Outline 1 Introduction 2 Base Classes and Derived Classes 3 protected Members 4 Relationship between Base Classes and Derived Classes 5 Case Study: Three-Level.
Advertisements

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.
COP 3003 Object-Oriented Programming - Inheritance Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Dale Roberts Object Oriented Programming using Java - Inheritance Overview Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
C++ Inheritance Systems Programming.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance Deitel &Deitel Java SE 8.
Dale Roberts Object Oriented Programming using Java - Inheritance Constructors Dale Roberts, Lecturer Computer Science, IUPUI
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10 Classes Continued
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Chapter 8 More Object Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
More on Object-Oriented Programming: Inheritance 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Spring CISC6795: Object Oriented Programming II.
1 Inheritance Chapter 9. 2 What You Will Learn Software reusability (Recycling) Inheriting data members and functions from previously defined classes.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Object Oriented Programming: Inheritance Chapter 9.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Coming up: Inheritance
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Inheritance ndex.html ndex.htmland “Java.
Object-Oriented Programming: Inheritance and Polymorphism.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Object Oriented Programming: Inheritance Chapter 9.
Object Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Class Inheritance Part I
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Week 4 Object-Oriented Programming (1): Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Object-Oriented Programming: Polymorphism
Chapter 9 Object-Oriented Programming: Inheritance
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
Object-Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Java Programming, Second Edition
Object-Oriented Programming: Inheritance and Polymorphism
Recitation Course 0610 Speaker: Liu Yu-Jiun.
Chapter 9 Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Presentation transcript:

Object Oriented Programming: Inheritance Chapter 9

2 What You Will Learn  Software reusability (Recycling)  Inheriting data members and methods from previously defined classes

3 Introduction  Software Reusability  saves time in program development  encourages use of proven, debugged code  reduces problems  Write programs in general fashion  Enables software designers to deal with complexity of modern software

4 Introduction  When creating a new class …  designate that class to inherit data members, functions of previously defined superclass  result is a subclass  Subclass adds new data members and functions  Replace and refine existing members

5 Base Classes & Derived Classes  Superclass is more general  student, shape, loan  Subclass is more specific  grad student, undergrad  circle, triangle, rectangle  carloan, home improvement, mortgage  Some languages talk of  Base class (Superclass)  Derived class (Subclass)

6 Superclass and Subclass  Inheritance produces tree like structures

7 Superclass and Subclass  Inheritance produces tree like structures

8 Design Tip  Important link between subclass and superclass  The “IS-A” relationship  Examples  A checking account IS-A banking account  A savings account IS NOT a checking account  If there is no IS-A relationship, do not use inheritance Sun comments on the concept of inheritance Sun comments on the concept of inheritance

9 protected Members  protected access  Intermediate level of protection between public and private  protected members accessible by  superclass members  subclass members  Class members in the same package  Subclass access to superclass member  Keyword super and a dot (.)

10 Comments on Private vs. Protected  Use protected when  Superclass should provide a service only to its subclasses  Should not provide service to other clients  Use private so that  Superclass implementation can change without affecting subclass implementations  Author advocates avoiding protected  Instead provide set and get methods to access private data items (see Figures 9.12, 9.13 in text) Figures Figures

11 Relationship between Superclasses and Subclasses  Superclass and subclass relationship  Example: CommissionEmployee/ BasePlusCommissionEmployee inheritance hierarchy  CommissionEmployee  First name, last name, SSN, commission rate, gross sale amount  BasePlusCommissionEmployee  First name, last name, SSN, commission rate, gross sale amount  Base salary

12 Creating and Using a CommissionEmployee Class  Class CommissionEmployee  Extends class Object  Keyword extends  Every class in Java extends an existing class  Except Object  Every class inherits Object ’s methods  New class implicitly extends Object  If it does not extend another class

13 Creating and Using a CommissionEmployee Class  Class CommissionEmployee  Extends class Object  Keyword extends  Every class in Java extends an existing class  Except Object  Every class inherits Object ’s methods  New class implicitly extends Object  If it does not extend another class  View class Figure 9.4 Figure 9.4Figure 9.4  View test program, Figure 9.5 Figure 9.5Figure 9.5

14 Creating a BasePlusCommissionEmployee Class without Using Inheritance  Class BasePlusCommissionEmployee  Implicitly extends Object  Much of the code is similar to CommissionEmployee  private instance variables  public methods  constructor  Additions  private instance variable baseSalary  Methods setBaseSalary and getBaseSalary View class Figure 9.6 View test program Figure 9.7

15 Creating a CommissionEmployee- BasePlusCommiionEmployee Inheritance Hierarchy  Class BasePlusCommissionEmployee2  Extends class CommissionEmployee  Is a CommissionEmployee  Has instance variable baseSalary  Inherits public and protected members  Constructor not inherited  Note these points plus invalid references, Figure 9.8 – note compiler error messages Figure 9.8 error messages Figure 9.8 error messages

16 Using protected Instance Variables  Use protected instance variables  Enable class BasePlusCommissionEmployee to directly access superclass instance variables  Superclass’s protected members are inherited by all subclases of that superclass  View version that now works, Figure 9.10 Figure 9.10Figure 9.10  Test program, Figure 9.11 Figure 9.11Figure 9.11

17 Using protected Instance Variables Advantages  Subclasses can modify values directly  Slight increase in performance  Avoid set/get method call overhead

18 Using protected Instance Variables Disadvantages  No validity checking  subclass can assign illegal value  Implementation dependent  subclass methods more likely dependent on superclass implementation  superclass implementation changes may result in subclass modifications  Fragile (brittle) software

19 Reexamine Hierarchy  Use the best software engineering practice  Declare instance variables as private  Provide public get and set methods  Use get method to obtain values of instance variables  View new version of CommissionEmployee, Figure 9.12 Figure 9.12 Figure 9.12  New version of BasePlusCommissiionEmployee, Figure 9.13 Figure 9.13Figure 9.13  Test program, Figure 9.14 Figure 9.14Figure 9.14

20 Instantiating Subclass Object Chain Of Constructor Calls  Subclass constructor invokes superclass constructor  Implicitly or explicitly  Base of inheritance hierarchy  Last constructor called in chain is Object ’s constructor  Original subclass constructor’s body finishes executing last

21 Instantiating Subclass Object Chain Of Constructor Calls  Example, Figure 9.15 : CommissionEmployee3- BasePlusCommissionEmployee4 hierarchy Figure 9.15 Figure 9.15  CommissionEmployee3 constructor called second last (last is Object constructor)  CommissionEmployee3 constructor’s body finishes execution second (first is Object constructor’s body)  Figure 9.16, BasePlusCommissionEmploye5 Figure 9.16 Figure 9.16  Figure 9.17, Test program, demonstrates constructor calls Figure 9.17 Figure 9.17

22 Software Engineering with Inheritance  At the design stage, certain classes found to be closely related  Factor out common attributes, behaviors  Place these in a superclass  Use inheritance to develop subclasses with inherited capabilities  This avoids proliferation of classes, "reinventing the wheel"  Note  Declaring a subclass does not affect superclass source code