Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance Beaulieu College, 2008.

Similar presentations


Presentation on theme: "Inheritance Beaulieu College, 2008."— Presentation transcript:

1 Inheritance Beaulieu College, 2008

2 What is Inheritance? Getting things from your parents!
The process of using existing code and its properties and methods and extending its functionality. Remember the Gogga?

3 Child Class AND Parent Class
Some terminology Child Class Child Class AND Parent Class Parent Class Vehicle Car Sedan Convertible Truck Panel Van

4 Another example with food
Sub Class Sub Class AND Super Class Sub Class AND Super Class Super Class Food Starters Main Course Curry & Rice Thai Indian Roast Chicken Pudding Ice-cream Dairy Sorbet Cakes

5 Another example, with people
Child Class Child Class AND Parent Class Parent Class Person Student GET Phase FET Phase Staff Member Parent

6 What can Gogga do? A recap…
Gogga can move () One space at a time… Yawn…

7 How to move 5 spaces? Gogga g = new Gogga (); g.move ();

8 But this is boring! Can’t we take the Gogga and teach it some new tricks? Yes! We can create our own class that will extend the Gogga’s functions. We will also be able to use the existing functions, without having to reprogram the Gogga! That’s cool! Let’s call our first attempt CleverGogga

9 Extending our Gogga public class CleverGogga extends Gogga { public CleverGogga () // new constructor super (); // call the parent constructor to do // the hard work! } public void move (int spaces) for (int i = 0 ; i < spaces ; i ++) move (); // the parent move method

10 Using our CleverGogga public class UseCleverGogga { public static void main (String [] args) CleverGogga cg = new CleverGogga (); cg.move (10); // using the new method cg.turnRight (); // using the parent method cg.move (); // using the parent method }

11 Let’s draw some squares
public class CleverGogga extends Gogga { // the other code is still here… public void square (int size) for (int i = 0 ; i < 4; i ++) move (size); turnRight (); }

12 Which methods are we calling?
Java looks at the children classes first. If we call the move() method, it is not in CleverGogga, so it is called from Gogga. If we call move(int), it is in CleverGogga. CHILD CleverGogga move(int) square(int) PARENT Gogga move() turnRight()

13 Forcing the parent method to be used
public class CleverGogga extends Gogga { // the other code is still here… public void move () // moving with a wiggle // NB “wiggling” is not useful, just fun! super.move (); // so we don’t use this method turnLeft (); // turn left! turnRight (); turnRight (); // turn right turnLeft (); // facing the way we were! }

14 Super? In the new method we use the move() method.
If we don’t say “super” before the move() method, it will call itself and not the parent method. This will cause a never-ending cycle of a method that will call itself which will call itself which will call itself which will call itself… eventually the JVM will crash.

15  What’s with the Wiggle? We have now replaced the move() method.
Java will use this method instead of the parent method when it sees move(). The CleverGogga will now wiggle when it draws squares, for example. CHILD CleverGogga move() move(int) square(int) PARENT Gogga turnRight()

16 Overriding and Overloading
By coding a move(int) method, we overloaded the move() method – that is, we provided an alternative that could be used. By coding a move() method in the CleverGogga class, we override the move() method in the parent class.

17 super and this super forces the method to look in the parent class for a method with that name. this forces the method to look in the (same) child class for a method with that name. this is usually not needed. We can still keep our wiggle-move, but make Java act a little more sensibly when using our “long-distance” move(int) method.

18 Reviewing our clever move
public class CleverGogga extends Gogga { // the other code is still here… public void move (int size) for (int i = 0 ; i < size; i ++) super.move (); // without wiggle! }

19 this in other places We can use the keyword this in other places to refer to the current object. public class myExample { private int num = 2; public void setNum (int num) this.num = num; // object variable = local variable // saying “num = num” is just silly. }

20 Changing the constructor method
We can change the constructor method to get different behaviour from our new CleverGogga. public class CleverGogga extends Gogga { public CleverGogga () super (); // must always be called first! setTrailWidth (8); setPosition (0, 0); }

21 Resources Exploring IT: Java Programming (Kench et al.), pages 1 to 9.


Download ppt "Inheritance Beaulieu College, 2008."

Similar presentations


Ads by Google