Presentation is loading. Please wait.

Presentation is loading. Please wait.

CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009.

Similar presentations


Presentation on theme: "CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009."— Presentation transcript:

1 CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009

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

3 CreatingSubclasses3 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

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

5 CreatingSubclasses5 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?

6 CreatingSubclasses6 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);

7 CreatingSubclasses7 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);

8 CreatingSubclasses8 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);

9 CreatingSubclasses9 Add a Constructor that takes a World public 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); } }

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

11 CreatingSubclasses11 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); }

12 CreatingSubclasses12 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);

13 CreatingSubclasses13 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?

14 CreatingSubclasses14 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

15 CreatingSubclasses15 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)

16 CreatingSubclasses16 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

17 CreatingSubclasses17 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

18 CreatingSubclasses18 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

19 CreatingSubclasses19 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

20 CreatingSubclasses20 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?

21 CreatingSubclasses21 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


Download ppt "CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009."

Similar presentations


Ads by Google