creatingsubclasses1 barb ericson georgia institute of technology dec 2009

21
CreatingSubclasses 1 Creating Subclasses Barb Ericson Georgia Institute of Technology Dec 2009

Upload: austin-hunter

Post on 13-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 1

Creating Subclasses

Barb EricsonGeorgia Institute of Technology

Dec 2009

Page 2: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 2

Learning Goals

• Computing concepts– Inheriting from a class– Calling parent constructors– Overriding a parent method– Adding new fields to a subclass– Adding new methods to a subclass

Page 3: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 3

Creating an Inherited Class

• Create a class SlowTurtle that inherits from the Turtle class– But when a SlowTurtle object is asked to go

forward (without any parameters) it will only go forward 50 instead of 100

– And if you ask it to go forward with a passed amount it will go forward only half that amount

Page 4: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 4

Inheriting from a Class

• To inherit from another class– Add extends ClassName to the class

declaration

public class SlowTurtle extends Turtle

{

}

• Save in SlowTurtle.java

• Try to compile it

Page 5: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 5

Compile Error?

• If you try to compile SlowTurtle you will get a compiler error– Error: cannot resolve symbol – symbol: constructor Turtle()– location: class Turtle

• Why do you get this error?

Page 6: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 6

Inherited Constructors

• When one class inherits from another all constructors in the child class will have an implicit call to the no-argument parent constructor as the first line of code in the child constructor– Unless an explicit call to a parent constructor

is the first line of code in the constructor

super(argumentList);

Page 7: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 7

Why is an Implicit Call to Super Added?

• Object fields are inherited from a parent class– But object fields should be declared private

• Not public, protected, or package visibility– Lose control over field at the class level then

– But then subclasses can’t directly access inherited object fields

– How do you initialize inherited fields?• By calling the parent constructor that initializes

them– Using super(paramList);

Page 8: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 8

Explanation of the Compile Error

• There are no constructors in SlowTurtle– So a no-argument one is added for you

• With a call to super();

– But, the Turtle class doesn’t have a no-argument constructor

• All constructors take a world to put the turtle in

• So we need to add a constructor to SlowTurtle– That takes a world to add the turtle to

• And call super(theWorld);

Page 9: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 9

Add a Constructor that takes a Worldpublic class SlowTurtle extends Turtle{ /** * Constructor that takes a world and * calls the parent constructor * @param theWorld the world to put the * slow turtle in */ public SlowTurtle(World theWorld) { super (theWorld); } }

Page 10: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 10

Try this Out

• Compile SlowTurtle– It should compile

• Try it out– It should act just like a Turtle object

• How do we get it to go forward the correct amount (1/2)?– Since we are overriding the forward(int

amount) command?– Use super.forward(amount); which calls the

parent's forward(int amount) method

Page 11: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 11

SlowTurtle forward methods /** * Method to go forward by a passed amount * it will actually go forward by half that * amount * @param amount the amount to go forward by */ public void forward(int amount) { super.forward(amount / 2); } /** * Method to go forward without a passed * amount. It will go forward by 50 */ public void forward() { super.forward(50); }

Page 12: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 12

Try out a SlowTurtle

> World earth = new World();> Turtle tasha = new Turtle(earth);> tasha.forward();> Turtle sue = new SlowTurtle(earth);> sue.forward();> tasha.turnRight();> sue.turnRight();> tasha.forward(50);> sue.forward(50);

Page 13: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 13

What do you think will happen?

• Look at the following code> World earth = new World();

> Turtle t1 = new SlowTurtle(earth);

> t1.forward(100);

• How far will the turtle move?• 100 since the variable is declared to be a

Turtle?• 50 since it is actually a SlowTurtle object?

Page 14: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 14

Method Resolution

• You can set the value of an object reference to be of the declared type

Turtle tasha = new Turtle(earth);– Or any subclass of the declared type

Turtle t1 = new SlowTurtle(earth);

• Methods are executed at run-time– Based on the actual type of the object

• The class that created it– So the turtle t1 will only move forward 50

Page 15: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 15

Override Methods

• Children classes inherit parent object methods– The slow turtle knows how to turn right

• Because it inherits this from Turtle

• Children can override parent object methods– Have a method with the same name and

parameter list as a parent method• This method will be called instead of the parent

method– Like forward() and forward(int amount)

Page 16: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 16

What is Happening?

• Each time an object is asked to execute a method– Check the class that created the object to see

if the method is defined in that class• If it is, it will execute that method• If it isn’t, next check the parent class of the class

that created it – that method will execute if one is found– If no method with that name and parameter list is found,

check that classes parent» Keep going until you find the method

Page 17: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 17

Method Overloading

SlowTurtle: Class

-----------------------------------

forward()

forward(int amount)sasha

Turtle: Class

-----------------------------------drawSquare()

tasha

SimpleTurtle: Class

-----------------------------------forward()

forward(int amount)

turnLeft()

turnRight()Obj: Turtle-------------class

Obj: SlowTurtle-------------class

Page 18: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 18

Exercise

• Create a StubbornTurtle class– That has a 50% chance of doing what you ask– You can use Math.random() to get back a

number from 0 to not quite 1 (not inclusive) – You can check if the random number is

greater than .5 and if so call the parent method to do the action

Page 19: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 19

Adding Fields and Methods to a Subclass

• What if we want to play music while the slide show is playing? – Create a MusicalSlideShow that inherits from

SlideShow

• Add a field for a sound clip (using the Sound class).

• We can override the show method to first start playing the sound and then call the parent's show method

• We can add methods to get and set the sound

Page 20: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 20

Challenge

• What if the music is too long for the slide show?

• What if the music is too short for the slide show?

• Can you make the music match the length of the slide show?

Page 21: CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

CreatingSubclasses 21

Summary• A class inherits methods and fields from a parent class

– But doesn’t have direct access to private fields and methods

• A implicit call to the no-argument parent constructor will be added– If there isn’t a call to super(paramList) as the first line of code in

a child constructor

• A subclass can override a parent method– To be called instead of the parent method

• Using the same method name and parameter list as a parent method

• A subclass can invoke a parent method– Using super.methodName(paramList);

• A subclass can add new fields and methods