1 Inheritance and Polymorphism Chapter 11 Spring 2007 CS 101 Aaron Bloomfield.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

More on Classes Inheritance and Polymorphism
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
1 Inheritance and Polymorphism. 2 This section is not required material!!!!  A note about inheritance… It’s not normally covered in 101 It will be gone.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
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.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Computer Science I Inheritance Professor Evan Korth New York University.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Chapter 8 More Object Concepts
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Inheritance Version 1.1. Topics Inheritance Constructors and Inheritance Function Over-riding (Redefinition) The Stringstream class Shadowing Variables.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Lists and the Collection Interface Review inheritance & collections.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
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.
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.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
1 Inheritance and Polymorphism. 2 This section is not required material!!!!  Since we are ahead in lecture, I’m delving into it  If you feel confused,
AP Computer Science A – Healdsburg High School 1 Inheritance - What is inheritance? - “Is-a” vs. “Has-a” relationship - Programming example “CircleBug”
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
A Introduction to Computing II Lecture 3: Interfaces and Inheritance Fall Session 2000.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Lecture 12 Inheritance.
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance and Polymorphism
Final and Abstract Classes
Presentation transcript:

1 Inheritance and Polymorphism Chapter 11 Spring 2007 CS 101 Aaron Bloomfield

2 This section is not required material!!!!  A note about inheritance… It’s not normally covered in 101 It will be gone over in more detail in CS 201  Ask questions if you are confused about inheritance You aren’t the only one!

3 Motivation  Consider a transportation computer game Different types of vehicles:  Planes Jets, helicopters, space shuttle  Automobiles Cars, trucks, motorcycles  Trains Diesel, electric, monorail  Ships …  Let’s assume a class is written for each type of vehicle

4 More on classes vs. objects

5 Motivation  Sample code for the types of planes: fly() takeOff() land() setAltitude() setPitch()  Note that a lot of this code is common to all types of planes They have a lot in common! It would be a waste to have to write separate fly() methods for each plane type  What if you then have to change one – you would then have to change dozens of methods

6 Motivation  Indeed, all vehicles will have similar methods: move() getLocation() setSpeed() isBroken()  Again, a lot of this code is common to all types of vehicles It would be a waste to have to write separate move() methods for each vehicle type  What if you then have to change one – you would then have to change dozens of methods  What we want is a means to specify one move() method, and have each vehicle type inherit that code Then, if we have to change it, we only have to change one copy

7 Motivation Provides:move()getLocation()setSpeed()isBroken() Provides: fly() takeOff() land() setAltitude() setPitch() Provides: derail() getStation() Provides: oilChange() isInTraffic()

8 Motivation  What we will do is create a “parent” class and a “child” class  The “child” class (or subclass) will inherit the methods (etc.) from the “parent” class (or superclass)  Note that some classes (such as Train) are both subclasses and superclasses

9 Inheritance code class Vehicle {... } class Train extends Vehicles {... } class Monorail extends Train {... }

10 About extends  If class A extends class B Then class A is the subclass of B Class B is the superclass of class A A “is a” B A has (almost) all the methods and variables that B has  If class Train extends class Vehicle Then class Train is the subclass of Vehicle Class Vehicle is the superclass of class Train Train “is a” Vehicle Train has (almost) all the methods and variables that Vehicle has

11 Object-oriented terminology  In object-oriented programming languages, a class created by extending another class is called a subclass  The class used for the basis is called the superclass  Alternative terminology The superclass is also referred to as the base class The subclass is also referred to as the derived class MonorailTrainVehicle

12 10 dimensions Disclaimer: it doesn’t have much scientific validity Disclaimer: it doesn’t have much scientific validity

13 End of lecture on 27 April 2007

14 Another example  Consider shapes in a graphics program Shape class  Circle class  Cube class  Dodecahedron class

15 Inheritance  Organizes objects in a top-down fashion from most general to least general  Inheritance defines a “is-a” relationship A mountain bike “is a” kind of bicycle A SUV “is a” kind of automobile A border collie “is a” kind of dog A laptop “is a” kind of computer

16 Packages  Allow definitions to be collected together into a single entity— a package  The classes in our game could be added to a package  Classes and names in the same package are stored in the same folder  Classes in a package go into their own “namespace” and therefore the names in a particular package do not conflict with other names in other packages For example, a package called OtherGame might have a different definition of Map

17 Controlling access  Class access rights Member Restriction thisSubclassPackageGeneral public protected  default  private 

18 Java’s Mother-of-all-objects—Class Object

19 Thus, everything extends Object  Either directly or indirectly  So what does that give us?  Object contains the following methods: clone() equals() toString() and others…  Thus, every class has those methods

20 More about clone()  It’s protected in class Object Which means that a Circle class also has a protected version Recall that protected means that code outside the Circle class can NOT call it  If we wanted a clone() method, we would declare it as such: public Object clone() { … }  Otherwise, we would get the following error message: Method clone() has protected visibility in class Cirlce

21 A note about equals()  Why does the equals() method always have to have the following prototype: boolean equals(Object obj)  Many other class in the Java SDK require the use of equals() Such as the Vector class  Those classes need to know how the equals() method will work in order for them to work properly Thus, it must have the same prototype  Same for toString() This method allows Java to “print” out an object

22 Overriding  Consider the following code: class Foo { // automatically extends Object public String toString () { return “Foo”; } }... Foo f = new Foo(); System.out.println (f);  Now there are two toString() method defined One inherited from class Object One defined in class Foo  And they both have the same prototype!  Which one does Java call?

23 Overriding  Java will call the most specific overriden method it can toString() in Foo is more specific than toString() in Object  Consider our transportation hierarchy: Assume each class has its own toString() method Car extends Automobile extends Vehicle (extends Object) Assume each defines a toString() methods  The toString() method in Vehicle is more specific (to vehicles) than the one in Object  The toString() method in Automobiles is more specific than the ones in Vehicle or Object  The toString() method in Car is more specific than the ones in Automobile, Vehicle, or Object  Thus, for a Car object, the Car toString() will be called There are ways to call the other toString() methods  This has to be specifically requested

24 Overriding  This is called overriding, because the toString() in Foo “overrides” the toString() in Object  Note that the prototype must be EXACTLY the same With overloading, the parameter list must be DIFFERENT  Overriding only works with inheritance In particular, you can only override a method already defined in a parent (or grandparent, etc.) class