Object Oriented Programming: Inheritance Chapter 9.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

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.
Abteilung für Telekooperation Softwareentwicklung 2 UE SE2UE_ Vererbung (Inheritance)
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
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.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
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.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
Object-Oriented PHP (1)
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 27 - Java Object-Oriented Programming Outline.
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.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
ISE 582: Web Technology for Industrial Engineers University of Southern California Department of Industrial and Systems Engineering Lecture 4 JAVA Cup.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object Oriented Programming: Inheritance Chapter 9.
 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.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
1 Inheritance Chapter 9. 2 What You Will Learn Software reusability (Recycling) Inheriting data members and functions from previously defined classes.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Peyman Dodangeh Sharif University of Technology Fall 2014.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Object Oriented Programming
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
Coming up: Inheritance
Topics Inheritance introduction
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:
© 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.
Inheritance ndex.html ndex.htmland “Java.
Object-Oriented Programming: Inheritance and Polymorphism.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
Object Oriented Programming: Inheritance Chapter 9.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Object Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Chapter 9 Object-Oriented Programming: Inheritance
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Object-Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Java Programming, Second Edition
Object-Oriented Programming: Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Final and Abstract Classes
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  Class can be derived from one or multiple classes  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 - Checking & Savings are derived from Bank Account Class - Super-Now class derived from Checking class - Checking & Savings are derived from Bank Account Class - Super-Now class derived from Checking class

7 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

8 Declaring Classes Using Inheritance  View superclass Point.java, Figure 9.9 Figure 9.9Figure 9.9  View subclass Circle.java, Figure 9.10 Figure 9.10Figure 9.10  Note  The proteted specification for the superclass data  The extends specification  The implicit call to the superclass constructor  The explicit call with super( … )  View the test program, Figure 9.11 Figure 9.11Figure 9.11

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

10 Three Level Inheritance Hierarchy  So far we have superclass Point and subclass Circle  Consider creating a subclass Cylinder, derived from Circle  View class Cylinder, Figure 9.15 Figure 9.15Figure 9.15  Note the program for testing class Cylinder, Figure 9.16 Figure 9.16 Figure 9.16

11 Constructors in Subclasses  When you instantiate a subclass object  Subclass constructor invokes superclass constructor  Implicitly  Explicitly with super ( ) reference  Subclass constructor performs its own tasks  Superclass constructor may also invoke a constructor in the next class up the hierarchy  Last constructor in the chain is always the constructor for Object

12 Finalizers in Subclasses When classes in your hierarchy declare their own finalize methods …  A subclass finalize should invoke the superclass finalize as its last action  Ensures all parts of an object are finalized properly  Marked correctly for garbage collection  Note example of finalize functions in Figures 17, 18, and the test Figure Figure Figure 9.19

13 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