Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-1.

Slides:



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

CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Static Data; More Inheritance reading:
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 9: Inheritance and Interfaces.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-1: Inheritance reading:
Copyright 2010 by Pearson Education Topic 31 - inheritance.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-x: Critters reading: HW9 Spec.
Intro to OOP with Java, C. Thomas Wu
CS305j Introduction to Computing Inheritance and Polymorphism 1 Topic 26 Introduction to Inheritance and Polymorphism "One purpose of CRC cards [a design.
CSC 142 Computer Science II Zhen Jiang West Chester University
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 19: encapsulation, inheritance reading: (Slides adapted from Stuart.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-1: Inheritance reading:
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
1 Building Java Programs Chapter 9: Inheritance and Interfaces These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Static Data; More Inheritance reading:
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Copyright 2010 by Pearson Education Building Java Programs Chapter 9 Lecture 9-1: Inheritance reading: 9.1.
Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.
© 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 and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ); Discussion of Homework 9:
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Copyright 2010 by Pearson Education Homework 8: Critters reading: HW8 spec.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ); Discussion of Homework 9:
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Copyright 2009 by Pearson Education Building Java Programs Chapter 8: Classes Lecture 8-3: More Critters, static.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-1.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Building Java Programs Chapter 9
Inheritance and Polymorphism
Building Java Programs
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
Lecture 9-2: Interacting with the Superclass (super);
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
Building Java Programs
Java Programming, Second Edition
Building Java Programs
Law firm employee analogy
Building Java Programs
Lecture 14: Inheritance Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Building Java Programs
Topic 31 - inheritance.
Building Java Programs
CSE 142 Lecture Notes Inheritance, Interfaces, and Polymorphism
Building Java Programs
Homework 8: Critters (cont.)
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-1

Copyright 2009 by Pearson Education 2 Static in a Nutshell static: Part of a class, rather than part of an object. A single static field is shared by all objects of that class static field, general syntax: private static type name ; or, private static type name = value ; Example: private static int count = 0; private static Random rand;

Copyright 2009 by Pearson Education 3 Static methods Methods can also be made static cannot access fields directly shared by all objects of that class if public, can be called from inside or outside the class Declaration syntax: (same as we have seen before) public static return type name ( params ) { statements ; } Methods that do not access/modify the object’s state should be made static

Copyright 2009 by Pearson Education 4 Calling static methods, outside Static method call syntax (outside the class): class name. method name ( values ); This is the syntax client code uses to call a static method. Examples: int absVal = Math.max(5, 7); double[] a1 = {2.4, 2.5, 60.3, -4.1}; double[] a2 = Arrays.copyOf(a1, a1.length); See last Friday’s slides for much more information and examples!

Copyright 2009 by Pearson Education Inheritance reading:

Copyright 2009 by Pearson Education 6 Organization Techniques in Java We’ve learned several strategies for organizing code and reducing redundancy To reduce redundancy at the statement-level, make methods To reduce redundancy at the program-level, make classes What to do about redundancy at the class-level?

Copyright 2009 by Pearson Education 7 Redundant Classes A Sorcerer is a Character who can also cast spells Code is redundant between the two class definitions public class Character { private int hitPoints; public Character() { hitPoints = 100; } public int getAttack() { return 10; } public boolean isAlive() { return hitPoints > 0; } public void attack(Character other) { other.hitPoints -= getAttack(); } public class Sorcerer { private int hitPoints; public Sorcerer() { hitPoints = 100; } public int getAttack() { return 10; } public boolean isAlive() { return hitPoints > 0; } public void attack(Sorcerer other) { other.hitPoints -= getAttack(); } public void castSpell() { System.out.println("Abracadabra!"); }

Copyright 2009 by Pearson Education 8 Desire for code-sharing castSpell is the only unique behavior in Sorcerer. We'd like to be able to say: // A class to represent sorcerers. public class Sorcerer { copy all the contents from the Character class; public void castSpell() { System.out.println("Abracadabra!"); }

Copyright 2009 by Pearson Education 9 Inheritance Java allows you to share code between classes in an inheritance paradigm Parent/child relationships created between classes The child classes have all the methods and fields from the parent and can extend and override parent’s behavior inheritance hierarchy: A set of classes connected by is-a relationships that can share common code. Drawn as downward tree of connected boxes or ovals:

Copyright 2009 by Pearson Education 10 Is-a relationships is-a relationship: A hierarchical connection where one category can be treated as a specialized version of another. Every Polygon is a Shape Every Rectangle is a Polygon We say that one class can extend another by absorbing its state and behavior. superclass: The parent class that is being extended. subclass: The child class that extends the superclass and inherits its behavior. Subclass gets copy of every field and method from superclass.

Copyright 2009 by Pearson Education 11 Inheritance syntax public class name extends superclass { Example: public class Sorcerer extends Character {... } By extending Character, each Sorcerer object now: receives a hitPoints field and a isAlive, getAttack, and attack method automatically can be treated as a Character by client code (seen later)

Copyright 2009 by Pearson Education 12 Improved Sorcerer Code // A class to represent sorcerer. public class Sorcerer extends Character { public void castSpell() { System.out.println("Abracadabra!"); } Now we only write the parts unique to each type. Sorcerer inherits a hitPoints field and a isAlive, getAttack, and attack method from Character Sorcerer adds the castSpell method

Copyright 2009 by Pearson Education 13 Levels of inheritance Multiple levels of inheritance in a hierarchy are allowed. Example: An Oracle is a Sorcerer who can also predict the future // A class to represent Oracles. public class Oracle extends Sorcerer { public void predictFuture() { System.out.println("Good fortune awaits you!"); } Oracle inherits the castSpell method from Sorcerer Oracle inherits the hitPoints field and a isAlive, getAttack, and attack method from Character Oracle adds the predictFuture method

Copyright 2009 by Pearson Education Inheritance and Critters Reading: HW8 spec

Copyright 2009 by Pearson Education 15 Analyzing Critters Homework 8 uses inheritance with the Critter class public class Snake extends Critter { … } The classes you write inherit the methods from the Critter class But you can override the inherited methods to replace the default behavior

Copyright 2009 by Pearson Education 16 Overriding methods override: To write a new version of a method in a subclass that replaces the superclass's version. There is no special syntax for overriding. To override a superclass method, just write a new version of it in the subclass. This will replace the inherited version. Note: the method header in the subclass must be identical to the method in the superclass intended to be overridden Example: public class Snake extends Critter { // overrides the default toString method public String toString() { return "S"; }... }

Copyright 2009 by Pearson Education 17 Critter: Anaconda The classes we wrote inherit default behavior from Critter Write Anaconda: Slithers in a wider and wider pattern ROAR 50% of the time; POUNCE 50% of the time Never hungry Displayed as an "A" Gray Can we use Snake? Override toString() and getColor()

Copyright 2009 by Pearson Education Interacting with the superclass: the super keyword reading:

Copyright 2009 by Pearson Education 19 Calling overridden methods Subclasses can call overridden methods with super keyword Calling an overridden method, syntax: super. method name ( parameter(s) ) Example: public class Sorcerer extends Character { public int getAttack() { int initPower = super.getAttack(); return initPower / 2; }... }