inheritance. what is inheritance? familiar examples: –a family tree (individuals inherit...

24
Inheritance

Upload: kerrie-lawson

Post on 04-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

Inheritance

Page 2: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

What Is Inheritance?

Familiar examples:

– A family tree (individuals inherit characteristics from other individuals)

– A taxonomy (classes inherit characteristics from other classes)

Page 3: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

Example Taxonomy: The Animal Kingdom

animal

mammal insect

bear cat dog ant cockroach tic

Page 4: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

Example Taxonomy: Vehicles

vehicle

car truck

sedan van wagon pickup semi tow

bussuv

Page 5: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

What Is Inheritance?

• Classes are organized in a hierarchy (subclasses and superclasses)

• A subclass inherits the attributes and behavior of its ancestor classes

Page 6: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

Example: java.awt GUI Component Classes

Component

TextComponent MenuComponent ContainerButtonCanvasCheckboxCheckboxGroupChoiceListScrollbar

TextArea TextField MenuItem MenuBar

Menu

PopupMenu

Window Panel

AppletDialog Frame

FileDialog

CheckboxMenuItem

Page 7: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

Example: Circles and Wheels

• Develop classes to represent circles and wheels

• Users should be able to create a circle with a given color, corner point, width and height

• They should be able to move and draw a circle

Page 8: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

Example: Circles and Wheels

• A wheel has all the attributes of a circle and a set of spokes in addition

• Users should be able to specify the number of spokes when instantiating a wheel

• Users should be able to change the number of spokes at any time

Page 9: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

Class Diagram

Wheel

CircleRelationship is one of inheritance rather than aggregation, “is-a” rather than “has-a”

Page 10: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

Class Diagram

Wheel

CircleAll classes implicitly are descendants of Object, which lies at the root of the hierarchy

Object

Page 11: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

The Circle Interfacepublic Circle()

public Circle(int x, int y, int width, int height, Color c)

public void draw(Graphics g)

public void move(int xDistance, int yDistance)

public boolean equals(Object other)

public String toString()

Page 12: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

The Wheel Interfacepublic Wheel()

public Wheel(int x, int y, int width, int height, Color c, int numSpokes)

public void draw(Graphics g)

public void move(int xDistance, int yDistance)

public boolean equals(Object other)

public String toString()

public void setSpokes(int numSpokes)

Page 13: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

Some Test Code

import java.awt.*;import BreezySwing.*;

public class ShapePanel extends GBPanel{

public ShapePanel(){ setBackground(Color.white); }

public void paintComponent(Graphics g){ super.paintComponent(g); Circle c = new Circle(10, 10, 50, 50, Color.blue); Wheel w = new Wheel(100, 10, 50, 50, Color.red, 5); c.draw(g); w.draw(g); }}

Page 14: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

Some Test Code

import BreezySwing.*;

public class TestShapes extends GBFrame{

public TestShapes(){ addPanel(new ShapePanel(), 1,1,1,1); }

public static void main(String[] args){ TestShapes theGUI = new TestShapes(); theGUI.setSize(300, 300); theGUI.setVisible(true); }}

Page 15: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

Extending a Classimport java.awt.*;

public class Circle { // Variables and methods

}

import java.awt.*;

public class Wheel extends Circle { // Variables and methods

}

Page 16: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

Protected vs Privateimport java.awt.*;

public class Circle { protected Color color; protected int x, y, width, height;

}

import java.awt.*;

public class Wheel extends Circle { private int numSpokes;

}

Protected variables are visible in subclasses but not other clients

Page 17: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

Circle Constructors import java.awt.*;

public class Circle { protected Color color; protected int x, y, width, height;

public Circle(int x, int y, int width, int height, Color c){ this.x = x; this.y = y; this.width = width; this.height = height; color = c; }

public Circle(){ this(0, 0, 50, 50, Color.black); }

this refers to the instance within its own class

Page 18: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

Wheel Constructors import java.awt.*;

public class Wheel extends Circle { private int numSpokes;

public Wheel(int x, int y, int width, int height, Color c, int numSpokes){ super(x, y, width, height, c); this.numSpokes = numSpokes; }

public Wheel(){ this(0, 0, 50, 50, Color.black, 5); }

super refers to the instance as viewed by its parent class

Page 19: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

Move the Shape import java.awt.*;

public class Circle { protected Color color; protected int x, y, width, height;

public void move(int xDistance, int yDistance){ x = x + xDistance; y = y + yDistance; }

move is inherited by Wheel

Each class manages its own data

Page 20: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

toString import java.awt.*;

public class Circle {

public String toString(){ return "(" + x + "," + y + ")" + "\n" + "Width : " + width + "\n" + "Height: " + height + "\n " + "Color: " + color + "\n"; }

import java.awt.*;

public class Wheel extends Circle {

public String toString(){ return super.toString() + "Spokes: " + numSpokes + "\n"; }

Page 21: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

draw import java.awt.*;

public class Circle {

public void draw(Graphics g){ Color oldColor = g.getColor(); g.setColor(color); g.drawOval(x, y, width, height); g.setColor(oldColor); }

import java.awt.*;

public class Wheel extends Circle {

public void draw(Graphics g){ super.draw(g); // The code for drawing the spokes would go here }

Page 22: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

equals import java.awt.*;

public class Circle {

public boolean equals(Object other){ if (! (other instanceof Circle)) return false; else{ Circle c = (Circle) other; return x == c.x && y == c.y && width == c.width && height == c.height; } }

Page 23: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

equals import java.awt.*;

public class Wheel extends Circle {

public boolean equals(Object other){ return other instanceof Wheel && super.equals(other) && numSpokes == ((Wheel) other).numSpokes; }

Page 24: Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes

Inheritance and Reuse

• Inheritance allows programs to reuse data and methods

• Eliminates redundancy and errors

• Try to place data and methods as high in the hierarchy as possible