Inheritance Notes Chapter 6 and AJ Chapters 7 and 8 1.

Slides:



Advertisements
Similar presentations
OOP: Inheritance By: Lamiaa Said.
Advertisements

Inheritance (Part 3) 1. Preconditions and Inheritance  precondition  what the method assumes to be true about the arguments passed to it  inheritance.
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.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
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.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
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.
CPSC150 Abstract Classes and Interfaces Chapter 10.
OOP Etgar 2008 – Recitation 61 Object Oriented Programming Etgar 2008 Recitation 6.
OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance using Java
Intro to OOP with Java, C. Thomas Wu
Inheritance Version 1.0. Topics Inheritance Constructors and Inheritance Hiding Methods and Variables Designing with Inheritance.
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.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
What is inheritance? It is the ability to create a new class from an existing class.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
Inheritance (Part 4) Abstract Classes 1.  sometimes you will find that you want the API for a base class to have a method that the base class cannot.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 4 Inheritance.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.
Inheritance Notes Chapter 6 1. Inheritance  you know a lot about an object by knowing its class  for example what is a Komondor? 2
Inheritance (Part 2) Notes Chapter KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Coming up: Inheritance
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
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.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Inheritance and Polymorphism
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Important Annoucement 1  I messed up something in the last class  if a subclass overrides a method that throws an exception then it must either 1. throw.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Inheritance (Part 3) 1. Preconditions and Inheritance  precondition  what the method assumes to be true about the arguments passed to it  inheritance.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Inheritance ITI1121 Nour El Kadri.
Inheritance and Polymorphism
Interfaces.
Java Programming, Second Edition
Chapter 9 Carrano Chapter 10 Small Java
Summary.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Inheritance Notes Chapter 6 and AJ Chapters 7 and 8 1

Inheritance  you know a lot about an object by knowing its class  for example what is a Komondor? 2

3...KomondorBloodHound PureBreedMix Dog Object Dog is-a Object PureBreed is-a Dog PureBreed is-a Object Komondor is-a PureBreed Komondor is-a Dog Komondor is-a Object

4...KomondorBloodHound PureBreedMix Dog Object subclass of Object superclass of PureBreed subclass of Dog superclass of Komondor superclass of Dog (and all other classes) superclass == base class parent class subclass == derived class extended class child class

5...KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed

Some Definitions  we say that a subclass is derived from its superclass  with the exception of Object, every class in Java has one and only one superclass  Java only supports single inheritance  a class X can be derived from a class that is derived from a class, and so on, all the way back to Object  X is said to be descended from all of the classes in the inheritance chain going back to Object  all of the classes X is derived from are called ancestors of X 6

Why Inheritance?  a subclass inherits all of the non-private members (attributes and methods but not constructors) from its superclass  if there is an existing class that provides some of the functionality you need you can derive a new class from the existing class  the new class has direct access to the public and protected attributes and methods without having to re- declare or re-implement them  the new class can introduce new attributes and methods  the new class can re-define (override) its superclass methods 7

Is-A  inheritance models the is-a relationship between classes  from a Java point of view, is-a means you can use a derived class instance in place of an ancestor class instance 8 public someMethod(Dog dog) { // does something with dog } // client code of someMethod Komondor shaggy = new Komondor(); someMethod( shaggy ); Mix mutt = new Mix (); someMethod( mutt );

Is-A Pitfalls  is-a has nothing to do with the real world  is-a has everything to do with how the implementer has modelled the inheritance hierarchy  the classic example:  Circle is-a Ellipse ? 9 Circle Ellipse

Circle is-a Ellipse ?  if Ellipse can do something that Circle cannot, then Circle is-a Ellipse is false  remember: is-a means you can substitute a derived class instance for one of its ancestor instances  if Circle cannot do something that Ellipse can do then you cannot (safely) substitute a Circle instance for an Ellipse instance 10

// method in Ellipse /* * Change the width and height of the ellipse. width The desired width. height The desired height. width > 0 && height > 0 */ public void setSize(double width, double height) { this.width = width; this.height = height; } 11

 there is no good way for Circle to support setSize (assuming that the attributes width and height are always the same for a Circle ) because clients expect setSize to set both the width and height  can't Circle override setSize so that it throws an exception if width != height ?  no; this will surprise clients because Ellipse setSize does not throw an exception if width != height  can't Circle override setSize so that it sets width == height ?  no; this will surprise clients because Ellipse setSize says that the width and height can be different 12

 But I have a Ph.D. in Mathematics, and I'm sure a Circle is a kind of an Ellipse! Does this mean Marshall Cline is stupid? Or that C++ is stupid? Or that OO is stupid? [C++ FAQs ]  Actually, it doesn't mean any of these things. But I'll tell you what it does mean — you may not like what I'm about to say: it means your intuitive notion of "kind of" is leading you to make bad inheritance decisions. Your tummy is lying to you about what good inheritance really means — stop believing those lies. 13

 what if there is no setSize method?  if a Circle can do everything an Ellipse can do then Circle can extend Ellipse 14

Implementing Inheritance  suppose you want to implement an inheritance hierarchy that represents breeds of dogs for the purpose of helping people decide what kind of dog would be appropriate for them  many possible attributes:  appearance, size, energy, grooming requirements, amount of exercise needed, protectiveness, compatibility with children, etc.  we will assume two attributes measured on a 10 point scale  size from 1 (small) to 10 (giant)  energy from 1 (lazy) to 10 (high energy) 15

Dog public class Dog extends Object { private int size; private int energy; // creates an "average" dog Dog() { this(5, 5); } Dog(int size, int energy) { this.setSize(size); this.setEnergy(energy); } 16

public int getSize() { return this.size; } public int getEnergy() { return this.energy; } public final void setSize(int size) { this.size = size; } public final void setEnergy(int energy) { this.energy = energy; } } 17 why final? stay tuned…

What is a Subclass?  a subclass looks like a new class that has the same API as its superclass with perhaps some additional methods and attributes  inheritance does more than copy the API of the superclass  the derived class contains a subobject of the parent class  the superclass subobject needs to be constructed (just like a regular object)  the mechanism to perform the construction of the superclass subobject is to call the superclass constructor 18

Constructors of Subclasses 1. the first line in the body of every constructor must be a call to another constructor  if it is not then Java will insert a call to the superclass default constructor  if the superclass default constructor does not exist or is private then a compilation error occurs 2. a call to another constructor can only occur on the first line in the body of a constructor 3. the superclass constructor must be called during construction of the derived class 19

Mix (version 1) public final class Mix extends Dog { // no declaration of size or energy; inherited from Dog private ArrayList breeds; public Mix () { // call to a Dog constructor super(); this.breeds = new ArrayList (); } public Mix(int size, int energy) { // call to a Dog constructor super(size, energy); this.breeds = new ArrayList (); } 20

public Mix(int size, int energy, ArrayList breeds) { // call to a Dog constructor super(size, energy); this.breeds = new ArrayList (breeds); } 21

Mix (version 2) public final class Mix extends Dog { // no declaration of size or energy; inherited from Dog private ArrayList breeds; public Mix () { // call to a Mix constructor this(5, 5); } public Mix(int size, int energy) { // call to a Mix constructor this(size, energy, new ArrayList ()); } 22

public Mix(int size, int energy, ArrayList breeds) { // call to a Dog constructor super(size, energy); this.breeds = new ArrayList (breeds); } 23

 why is the constructor call to the superclass needed?  because Mix is-a Dog and the Dog part of Mix needs to be constructed 24