Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes."— Presentation transcript:

1 Inheritance

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

3 Example Taxonomy: The Animal Kingdom animal mammalinsect bearcatdogantcockroachtic

4 Example Taxonomy: Vehicles vehicle cartruck sedanvanwagonpickupsemitow bussuv

5 What Is Inheritance? Classes are organized in a hierarchy (subclasses and superclasses) A subclass inherits the attributes and behavior of its ancestor classes

6 Example: java.awt GUI Component Classes Component TextComponentMenuComponentContainer Button Canvas Checkbox CheckboxGroup Choice List Scrollbar TextAreaTextFieldMenuItemMenuBar Menu PopupMenu WindowPanel AppletDialogFrame FileDialog CheckboxMenuItem

7 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

8 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

9 Class Diagram Wheel Circle Relationship is one of inheritance rather than aggregation, “is-a” rather than “has-a”

10 Class Diagram Wheel Circle All classes implicitly are descendants of Object, which lies at the root of the hierarchy Object

11 The Circle Interface public 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()

12 The Wheel Interface public 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)

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

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

15 Extending a Class import java.awt.*; public class Circle { // Variables and methods } import java.awt.*; public class Wheel extends Circle { // Variables and methods }

16 Protected vs Private import 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

17 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

18 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

19 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

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

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

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

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

24 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


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

Similar presentations


Ads by Google