Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-1."— Presentation transcript:

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

2 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;

3 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

4 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!

5 Copyright 2009 by Pearson Education Inheritance reading: 9.1 - 9.2

6 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?

7 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!"); }

8 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!"); }

9 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:

10 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.

11 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)

12 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

13 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

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

15 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

16 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"; }... }

17 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()

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

19 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; }... }


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

Similar presentations


Ads by Google