inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 inheritance.pdf · inheritance a java class...

28
Inheritance COMP1400 - Week 12

Upload: others

Post on 28-Sep-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

InheritanceCOMP1400 - Week 12

Page 2: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

Uno GameConsider the card game Uno:

http://en.wikipedia.org/wiki/Uno_(card_game)

There are 6 kinds of cards:

• number cards

• draw two

• skip

• reverse

• wild

• wild draw four

Page 3: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

Game classWe have a Game class which defines the rules of the game:

• Players take turns playing a card

• A valid play must be the same number or symbol or a wild card

• If you cannot play, draw a card.

• First player with no cards wins.

Page 4: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

Card interfaceEach kind of card has different properties and effects so belongs in a different class.

However they share a common interface.

They all support the methods of:

• can I play this card now?

• play this card.

Page 5: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

public interface Card {

// get the colour public int getColour();

// get the symbol public char getSymbol();

// test if it can be played public boolean canPlayOn( Card card);

// implement any effects public void play(Game g);

}

Page 6: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

Card classesThere are six classes that implement the Card interface:

NumberCard DrawCard

SkipCard ReverseCard

WildCard WildDrawCard

Page 7: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

Card classes

Page 8: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

Common codeLooking at the card classes we notice a lot of common data and code, e.g.:

private int myColour;

public boolean canPlayOn(Card c) {

return c.getSymbol() == 'S' || myColour == c.getColour();

}

Page 9: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

AbstractionThe design principles of abstraction and encapsulation prompt us to ask:

Is there a way to factor out this common data and code into a single, reusable chunk?

The techniques we've seen so far do not apply very well.

We need a new idea: inheritance.

Page 10: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

InheritanceA Java class can extend another class:

// based classpublic class Parent {

}

// derived classpublic class Child extends Parent {

}

Page 11: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

InheritanceA derived class (child) inherits:

• All the fields of its base class (parent)

• All the methods of its base class

However it cannot access the private fields or methods on its base.

Page 12: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

ExtendingAn extended child class may:

• Add new fields

• Add new methods

• Override old methods on its parent.

Page 13: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

public class Turtle {

private Point myPos;

public Point getPos() {

return myPos;

}

public void move(int dist) {

// move forward

}

}

Page 14: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

public class ColourTurtle extends Turtle {

// add a field

private Color myColour;

// add a method

public Color getColour() {

return myColour;

} }

Page 15: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

public class TurningTurtle extends Turtle {

private double myTurnSpeed;

// override method

public void move(int dist) {

// move forward // while turning

}

}

Page 16: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

Example

Turtle myPos getPos() move()

ColourTurtle

myColour getColour()

Turtle myPos getPos() move()

TurningTurtle

myTurnSpeed move()

Turtle myPos getPos() move()

Page 17: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

Calling methodsWhen you call a method on a derived class Java searches up the inheritance hierarchy until it finds a class that implements it.

Page 18: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

ExampleColouredTurtle ct = new ColouredTurtle();

ct.getColour(); // on ColouredTurtle

ct.move(100); // on Turtle

Page 19: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

ExampleTurningTurtle tt = new TurningTurtle();

tt.getPos(); // on Turtle

tt.move(100); // on TurningTurtle

Page 20: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

superA method on a subclass can use the keyword super to refer to its parent class.

public class TurningTurtle extends Turtle {

public void move(int dist) {

// move forward

super.move(); // while turning

Page 21: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

public class TurningTurtle extends Turtle {

public void move(int dist) {

// call parent // to move forward

super.move();

// now turn...

myAngle += turnSpeed; } }

Page 22: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

ConstructorsWhen we constructor a derived class we must first construct its parent.

We use the notation:

super()

Or if the super-constructor has parameters:

super(value1, value2, ...)

The super-constructor must always come first.

Page 23: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

Constructorpublic class Turtle {

private Point myPos;

public Turtle() {

myPos = new Point(0,0);

}

}

Page 24: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

Constructorspublic class ColourTurtle {

private Color myColour;

public ColourTurtle( Color colour) {

super(); // call parent's // constructor first

myColour = colour; }}

Page 25: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

AbstractSometimes several classes are based on the same parent, but the parent is incomplete or does not make sense as a usable object on its own.

In these cases it is appropriate to make the parent class abstract.

Page 26: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

public abstract class AbstractCard {

public boolean canPlayOn(Card c) {

return mySymbol == c.getSymbol() || myColour == c.getColour();

}

// method not implemented:

abstract public void play(Game g);

}

Page 27: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

Abstract classesAn abstract class cannot be instantiated. It only exists to provide a base for other classes:

AbstractCard card = new AbstractCard( 'X', Card.COLOUR_BLUE);

// ERROR!

Page 28: Inheritance - cse.unsw.edu.auis1609/13s2/lectures/12 Inheritance.pdf · Inheritance A Java class can extend another class: // based class public class Parent {} // derived class public

AdvantagesThe advantages of inheritance:

abstraction: common code is chunked

encapsulation: parent code is hidden from children

extendability: extra features can be added to classes

polymorphism: child classes all inherit the same interface