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

Post on 04-Jan-2016

219 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Inheritance

What Is Inheritance?

Familiar examples:

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

– A taxonomy (classes inherit characteristics from other classes)

Example Taxonomy: The Animal Kingdom

animal

mammal insect

bear cat dog ant cockroach tic

Example Taxonomy: Vehicles

vehicle

car truck

sedan van wagon pickup semi tow

bussuv

What Is Inheritance?

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

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

Example: java.awt GUI Component Classes

Component

TextComponent MenuComponent ContainerButtonCanvasCheckboxCheckboxGroupChoiceListScrollbar

TextArea TextField MenuItem MenuBar

Menu

PopupMenu

Window Panel

AppletDialog Frame

FileDialog

CheckboxMenuItem

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

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

Class Diagram

Wheel

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

Class Diagram

Wheel

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

Object

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()

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)

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

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

Extending a Classimport java.awt.*;

public class Circle { // Variables and methods

}

import java.awt.*;

public class Wheel extends Circle { // Variables and methods

}

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

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

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

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

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"; }

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 }

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; } }

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; }

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

top related