Inheritance (Part 2) Notes Chapter 6 1. 2...KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Inheritance (Part 3) 1. Preconditions and Inheritance  precondition  what the method assumes to be true about the arguments passed to it  inheritance.
Inheritance Notes Chapter 6 and AJ Chapters 7 and 8 1.
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CPSC150 Abstract Classes Chapter 10. CPSC150 Directory Example (note: your assignment does not have all of this) DirectoryEntry name phone public void.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
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.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Intro to OOP with Java, C. Thomas Wu
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
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.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
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 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
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.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
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.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
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.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
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.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
Inheritance and Polymorphism
Interfaces.
Chapter 9 Inheritance and Polymorphism
Interfaces.
Inheritance Inheritance is a fundamental Object Oriented concept
Chapter 9 Carrano Chapter 10 Small Java
Chapter 11 Inheritance and Polymorphism Part 1
Summary.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Inheritance (Part 2) Notes Chapter 6 1

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

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 fields:  appearance, size, energy, grooming requirements, amount of exercise needed, protectiveness, compatibility with children, etc.  we will assume two fields measured on a 10 point scale  size from 1 (small) to 10 (giant)  energy from 1 (lazy) to 10 (high energy) 3

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); } 4

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; } } 5 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 fields  the new class has direct access to the public and protected * fields and methods without having to re- declare or re-implement them  the new class can introduce new fields and methods  the new class can re-define (override) its superclass methods 6 * the notes does not discuss protected access

Mix UML Diagram  a mixed breed dog is a dog whose ancestry is unknown or includes more than one pure breed 7 Dog Mix 1 ArrayList breeds

8 Dog - size : int - energy : int + setSize() + setEnergy() + equals(Object) : boolean + hashCode() : int + toString() : String... Mix - breeds : ArrayList + getBreeds() : List + equals(Object) : boolean + hashCode() : int + toString() : String... subclass can add new fields subclass can add new methods subclass can change the implementation of inherited methods

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 fields  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 9

What is a Subclass?  another model of inheritance is to imagine that the subclass contains all of the fields of the parent class (including the private fields), but cannot directly use the private fields 10

Mix Memory Diagram Mix object size1 energy10 breeds1000 belongs to superclass private in superclass not accessible by name to Mix

Constructors of Subclasses  the purpose of a constructor is to set the values of the fields of this object  how can a constructor set the value of a field that belongs to the superclass?  by calling the superclass constructor and passing this as an implicit argument 12

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 13

Mix (version 1) public final class Mix extends Dog { // no declaration of size or energy; part of 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 (); } 14

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

Mix (version 2 using chaining) public final class Mix extends Dog { // no declaration of size or energy; part of 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 ()); } 16

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

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

19 Mix object Dog object Object object size1 energy10 breeds1000 Mix mutt = new Mix(1, 10); 1.Mix constructor starts running creates new Dog subobject by invoking the Dog constructor 2.Dog constructor starts running creates new Object subobject by (silently) invoking the Object constructor 3.Object constructor runs sets size and energy creates a new empty ArrayList and assigns it to breeds

Mix Memory Diagram Mix object size1 energy10 breeds1000 ArrayList object...

Invoking the Superclass Ctor  why is the constructor call to the superclass needed?  because Mix is-a Dog and the Dog part of Mix needs to be constructed  similarly, the Object part of Dog needs to be constructed 21

Invoking the Superclass Ctor  a derived class can only call its own constructors or the constructors of its immediate superclass  Mix can call Mix constructors or Dog constructors  Mix cannot call the Object constructor  Object is not the immediate superclass of Mix  Mix cannot call PureBreed constructors  cannot call constructors across the inheritance hierarchy  PureBreed cannot call Komondor constructors  cannot call subclass constructors 22

Constructors & Overridable Methods  if a class is intended to be extended then its constructor must not call an overridable method  Java does not enforce this guideline  why?  recall that a derived class object has inside of it an object of the superclass  the superclass object is always constructed first, then the subclass constructor completes construction of the subclass object  the superclass constructor will call the overridden version of the method (the subclass version) even though the subclass object has not yet been constructed 23

Superclass Ctor & Overridable Method public class SuperDuper { public SuperDuper() { // call to an over-ridable method; bad this.overrideMe(); } public void overrideMe() { System.out.println("SuperDuper overrideMe"); } 24

Subclass Overrides Method public class SubbyDubby extends SuperDuper { private final Date date; public SubbyDubby() { super(); this.date = new Date(); public void overrideMe() { System.out.println("SubbyDubby overrideMe : " + this.date); } public static void main(String[] args) { SubbyDubby sub = new SubbyDubby(); sub.overrideMe(); } 25

 the programmer's intent was probably to have the program print: SuperDuper overrideMe SubbyDubby overrideMe : or, if the call to the overridden method was intentional SubbyDubby overrideMe :  but the program prints: SubbyDubby overrideMe : null SubbyDubby overrideMe : 26 final attribute in two different states!

What's Going On? 1. new SubbyDubby() calls the SubbyDubby constructor 2. the SubbyDubby constructor calls the SuperDuper constructor 3. the SuperDuper constructor calls the method overrideMe which is overridden by SubbyDubby 4. the SubbyDubby version of overrideMe prints the SubbyDubby date field which has not yet been assigned to by the SubbyDubby constructor (so date is null) 5. the SubbyDubby constructor assigns date 6. SubbyDubby overrideMe is called by the client 27

 remember to make sure that your base class constructors only call final methods or private methods  if a base class constructor calls an overridden method, the method will run in an unconstructed derived class 28