Creating Subclasses Barb Ericson Georgia Institute of Technology

  • Slides: 21
Download presentation
Creating Subclasses Barb Ericson Georgia Institute of Technology Dec 2009 Creating. Subclasses 1

Creating Subclasses Barb Ericson Georgia Institute of Technology Dec 2009 Creating. Subclasses 1

Learning Goals • Computing concepts – Inheriting from a class – Calling parent constructors

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 Creating. Subclasses 2

Creating an Inherited Class • Create a class Slow. Turtle that inherits from the

Creating an Inherited Class • Create a class Slow. Turtle that inherits from the Turtle class – But when a Slow. Turtle 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 Creating. Subclasses 3

Inheriting from a Class • To inherit from another class – Add extends Class.

Inheriting from a Class • To inherit from another class – Add extends Class. Name to the class declaration public class Slow. Turtle extends Turtle { } • Save in Slow. Turtle. java • Try to compile it Creating. Subclasses 4

Compile Error? • If you try to compile Slow. Turtle you will get a

Compile Error? • If you try to compile Slow. Turtle you will get a compiler error – Error: cannot resolve symbol – symbol: constructor Turtle() – location: class Turtle • Why do you get this error? Creating. Subclasses 5

Inherited Constructors • When one class inherits from another all constructors in the child

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(argument. List); Creating. Subclasses 6

Why is an Implicit Call to Super Added? • Object fields are inherited from

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(param. List); Creating. Subclasses 7

Explanation of the Compile Error • There are no constructors in Slow. Turtle –

Explanation of the Compile Error • There are no constructors in Slow. Turtle – So a no-argument one is added for you • With a call to super(); – But, the Turtle class doesn’t have a noargument constructor • All constructors take a world to put the turtle in • So we need to add a constructor to Slow. Turtle – That takes a world to add the turtle to • And call super(the. World); Creating. Subclasses 8

Add a Constructor that takes a World public class Slow. Turtle extends Turtle {

Add a Constructor that takes a World public class Slow. Turtle extends Turtle { /** * Constructor that takes a world and * calls the parent constructor * @param the. World the world to put the * slow turtle in */ public Slow. Turtle(World the. World) { super (the. World); } } Creating. Subclasses 9

Try this Out • Compile Slow. Turtle – It should compile • Try it

Try this Out • Compile Slow. Turtle – 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 Creating. Subclasses 10

Slow. Turtle forward methods /** * Method to go forward by a passed amount

Slow. Turtle 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); } Creating. Subclasses 11

Try out a Slow. Turtle > World earth = new World(); > Turtle tasha

Try out a Slow. Turtle > World earth = new World(); > Turtle tasha = new Turtle(earth); > tasha. forward(); > Turtle sue = new Slow. Turtle(earth); > sue. forward(); > tasha. turn. Right(); > sue. turn. Right(); > tasha. forward(50); > sue. forward(50); Creating. Subclasses 12

What do you think will happen? • Look at the following code > World

What do you think will happen? • Look at the following code > World earth = new World(); > Turtle t 1 = new Slow. Turtle(earth); > t 1. forward(100); • How far will the turtle move? • 100 since the variable is declared to be a Turtle? • 50 since it is actually a Slow. Turtle object? Creating. Subclasses 13

Method Resolution • You can set the value of an object reference to be

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 t 1 = new Slow. Turtle(earth); • Methods are executed at run-time – Based on the actual type of the object • The class that created it – So the turtle t 1 will only move forward 50 Creating. Subclasses 14

Override Methods • Children classes inherit parent object methods – The slow turtle knows

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) Creating. Subclasses 15

What is Happening? • Each time an object is asked to execute a method

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 Creating. Subclasses 16

Method Overloading Simple. Turtle: Class -----------------forward() forward(int amount) turn. Left() Obj: Turtle ------class tasha

Method Overloading Simple. Turtle: Class -----------------forward() forward(int amount) turn. Left() Obj: Turtle ------class tasha Obj: Slow. Turtle ------class sasha turn. Right() Turtle: Class -----------------draw. Square() Slow. Turtle: Class -----------------forward() forward(int amount) Creating. Subclasses 17

Exercise • Create a Stubborn. Turtle class – That has a 50% chance of

Exercise • Create a Stubborn. Turtle 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 Creating. Subclasses 18

Adding Fields and Methods to a Subclass • What if we want to play

Adding Fields and Methods to a Subclass • What if we want to play music while the slide show is playing? – Create a Musical. Slide. Show that inherits from Slide. Show • 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 Creating. Subclasses 19

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

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? Creating. Subclasses 20

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

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(param. List) 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. method. Name(param. List); • A subclass can add new fields and methods Creating. Subclasses 21