Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.

Slides:



Advertisements
Similar presentations
Chapter 1 Inheritance University Of Ha’il.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Class Hierarchy (Inheritance)
Chapter 8 Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Chapter 8 Inheritance 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
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
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.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Chapter 7 Inheritance, Polymorphism, and Scope.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Chapter 8 Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
© 2004 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 : Inheritance Intermediate Java Programming Summer 2007.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
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.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/23 Outline Creating Subclasses Overriding Methods Class Hierarchies.
COS 312 DAY 13 Tony Gauvin. Ch 1 -2 Agenda Questions? First Progress Over due – Next progress report is March 26 Assignment 4 Posted – Chap 6 & 7 – Due.
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
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.
1 Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Chapter 8 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
Chapter 8 Inheritance.
Inheritance and Polymorphism
03/10/14 Inheritance-2.
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
An Introduction to Inheritance
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
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.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Lecture 22 Inheritance Richard Gesick.
Inheritance, Polymorphism, and Interfaces. Oh My
Overriding Methods & Class Hierarchies
Chapter 8 Inheritance Part 2.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010

2 CSE 1302C Topics Inheritance Concepts Inheritance Design –Inherited Members of a Class –Subclass Constructors –Adding Specialization to the Subclass –Overriding Inherited Methods The protected Access Modifier

3 CSE 1302C Inheritance Concepts A common form of reuse of classes is inheritance. We can organize classes into hierarchies of functionality. The class at the top of the hierarchy (superclass) defines instance variables and methods common to all classes in the hierarchy. We derive a subclass, which inherits behavior and fields from the superclass.

4 CSE 1302C Inheritance Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class, or superclass, or base class The derived class is called the child class, subclass, or derived class As the name implies, the child inherits characteristics of the parent That is, the child class inherits the methods and data defined by the parent class

5 CSE 1302C A Sample Vehicle Hierarchy This hierarchy is depicted using a Unified Modeling Language (UML) diagram. In UML diagrams, arrows point from the subclass to the superclass.

6 CSE 1302C Superclasses and Subclasses A superclass can have multiple subclasses. Subclasses can be superclasses of other subclasses. A subclass can inherit directly from only one superclass. All classes inherit from the Object class.

7 CSE 1302C Superclasses and Subclasses A big advantage of inheritance is that we can write common code once and reuse it in subclasses. A subclass can define new methods and instance variables, some of which may override (hide) those of a superclass.

8 CSE 1302C Specifying Inheritance The syntax for defining a subclass is to use the : symbol in the class header, as in accessModifier class SubclassName: SuperclassName { // class definition } The superclass name specified after the : is called the direct superclass. As mentioned, a subclass can have many superclasses, but only one direct superclass.

9 CSE 1302C Class Hierarchies A child class of one parent can be the parent of another child, forming a class hierarchy Business KMartMacys ServiceBusiness Kinkos RetailBusiness

10 CSE 1302C Class Hierarchies Two children of the same parent are called siblings Common features should be put as high in the hierarchy as is reasonable An inherited member is passed continually down the line Therefore, a child class inherits from all its ancestor classes There is no single class hierarchy that is appropriate for all situations

11 CSE 1302C The Object Class A class called Object exists All classes are derived from the Object class If a class is not explicitly defined to be the child of an existing class, it is assumed to be the child of the Object class Therefore, the Object class is the ultimate root of all class hierarchies

12 CSE 1302C The Object Class The Object class contains a few useful methods, which are inherited by all classes: Constructor Equals GetHashCode GetType ReferenceEquals ToString (virtual) Finalize (overridden, destructor) MemberwiseClone (shallow copy of object)

13 CSE 1302C Example namespace ConsoleApplication1 { public class ObjectExample { static void Main(string[] args) { Object o1 = new Object(); Console.WriteLine("This is an object " + o1); Temp t = new Temp(); Console.WriteLine("This is a temp object " + t); } public class Temp { private int a; private char ch; } Output This is an object System.Object This is a temp object ConsoleApplication1.Temp

14 CSE 1302C The Bank Account Hierarchy The BankAccount class is the superclass. –Instance variables: balance (double) –Methods: Default and overloaded constructors deposit and withdraw methods balance accessor toString

15 CSE 1302C ScreenManager Hierarchy //**pull from A2**//

16 CSE 1302C private Members Superclass members declared as private are part of the subclass, but not accessible by the subclass /** rework based on a2 **/

17 CSE 1302C Subclass Constructors Constructors are not inherited. The first task of a constructor is to call a base class constructor. Use this syntax: base( argument list ); Or: public constructor(arg1 list):base( argument list );

18 CSE 1302C The base Reference A child’s constructor is responsible for calling the parent’s constructor Normally the first line of a child’s constructor should use the base reference to call the parent’s constructor The base reference can also be used to reference other variables and methods defined in the parent’s class

19 CSE 1302C Adding Specialization A subclass can define new fields and methods.

20 CSE 1302C Software Engineering Tip The superclasses in a class hierarchy should contain fields and methods common to all subclasses. The subclasses should add specialized fields and methods.

21 CSE 1302C Overriding Inherited Methods A subclass can override (or replace) an inherited method by providing a new version of the method. Base class must include virtual Descendant must include override The API of the new version must match the inherited method. When the client calls the method, it will call the overridden version. The overridden (original) method is invisible to the client of the subclass, but the subclass methods can still call the overridden (original) method using this syntax: base.methodName( argument list )

22 CSE 1302C Overloading vs. Overriding Overloading deals with multiple methods with the same name in the same class, but with different signatures Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature Overloading lets you define a similar operation in different ways for different parameters Overriding lets you define a similar operation in different ways for different object types

23 CSE 1302C Common Error Trap Do not confuse overriding a method with overloading a method. Overriding a method: –A subclass provides a new version of that method (same signature), which hides the superclass version from the client. Overloading a method: –A class provides a version of the method, which varies in the number and/or type of parameters (different signature). A client of the class can call any of the public versions of overloaded methods.

24 CSE 1302C protected Members protected members are accessible by subclasses (like public members), while still being hidden from client classes (like private members). Also, any class in the same package as the superclass can directly access a protected field, even if that class is not a subclass. Disadvantage: –Because more than one class can directly access a protected field, protected access compromises encapsulation and complicates maintenance of a program. –For that reason, try to use private, rather than protected, for instance variables.

25 CSE 1302C The protected Access Modifier Declaring fields as private preserves encapsulation. –Subclass methods call superclass methods to set the values of the fields, and the superclass methods enforce the validation rules for the data. –But calling methods incurs processing overhead. Declaring fields as protected allows them to be accessed directly by subclass methods. –Classes outside the hierarchy and package must use accessors and mutators for protected fields.

26 CSE 1302C protected fields: Tradeoffs Advantage: –protected fields can be accessed directly by subclasses, so there is no method-invocation overhead. Disadvantage: –Maintenance is complicated because the subclass also needs to enforce validation rules. Recommendation: –Define protected fields only when high performance is necessary. –Avoid directly setting the values of protected fields in the subclass.

27 CSE 1302C Inheritance Rules Superclass Members Inherited by subclass? Directly Accessible by Subclass? Directly Accessible by Client of Subclass? public fieldsyesyes, by using field name yes public methodsyesyes, by calling method from subclass methods yes protected fieldsyesyes, by using field name no, must call accessors and mutators protected methods yesyes, by calling method from subclass methods no private fieldsyes* - no access no, must call accessors and mutators private methodsyes* - no access no

28 CSE 1302C Questions?