Inheritance - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Inheritance

Description:

Child class inherits data and methods from the parent, but not constructors ... If you override a method and want to get at the parent's version: super. ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 16
Provided by: qya
Category:

less

Transcript and Presenter's Notes

Title: Inheritance


1
Inheritance
2
Inheritance
  • Child class inherits data and methods from the
    parent, but not constructors
  • Child class can override parent methods it wants
    to change
  • Child class can have additional data and methods
  • In Java, classes that dont explicitly inherit
    from another class inherit from class Object
  • You can use a child object wherever a parent
    object is expected (Not the other way around!)

3
Example class Box
  • Some members
  • position (x, y)
  • (upper-left hand corner of the Box)
  • height
  • width
  • isVisible
  • Could be
  • private
  • public
  • protected

4
  • public class Box
  • protected int x, y
  • protected int width, height
  • public boolean isVisible
  •  
  • public Box( int startX, int startY, int w, int
    h )
  • x startX
  • y startY
  • width w
  • height h
  •  
  • ...

5
  • public class Box
  • ...
  • public void draw()
  • // Some code that draws the box
  • public void hide()
  • // Fill in rectangle with the background
    color
  • public void resize ( int newW, int newH )
  • hide()
  • width newW
  • height newH
  • draw()
  • ... 

6
  • public class Box
  • ...
  • public void move ( int deltaX, int deltaY )
  • hide()
  • x deltaX
  • y deltaY
  • draw()
  •  
  • public int area()
  • return width height
  • // more methods perimeter, getLocation,
    getSize, ....

7
Subclass ColoredBox
  • Box with a color
  • Will inherit everything from Box but the
    constructor
  • Will want to write its own version of draw
  • Leave other methods alone
  • Can access x, y, height and width, since
    protected
  • Cant access x, y, height and width if private
  • Would still have x, y, height and width even
    private

8
Some Rules on Child Class
  • The first line of the constructor must invoke a
    parent constructor via super (superclass)
  • Don't call any methods in the parameters of the
    constructor
  • If you override a method and want to get at the
    parents version super.
  • For example super.draw()
  • Protected
  • Allow the children access to its data
  • Allow classes in the same package to
    access the data

9
  • public class ColoredBox extends Box
  • private Color color
  •  
  • public ColoredBox( int startX, int startY, int
    w, int h,
  • Color c )
  • super(startX, startY, w, h)
  • color c
  •  
  • public void draw()
  • super.draw() // method draw is
    overridden
  • ...
  • public Color getColor()
  • return color

10
Example
  • Box b new Box( 50, 40, 20, 30)
  • ColoredBox cb new ColoredBox( 80, 60, 10, 20,
    Color.red )
  • b.draw()
  • cb.draw()
  • cb.move( 3, 4 )
  • b cb
  • b.resize( 10, 15 )
  • b.draw()

11
Prog3
12
Testing
You must test all classes THOROUGHLY!
But I dont know how to test it!
13
Testing
  • Method equals
  • 12.3 12 12.3 12
  • 12.3 14 12.6 14
  • 12.6 14 12.3 14
  • 12.3 12 12.3 14
  • 12.3 14 12.3 12

13
14
Prog4
  • Lab on Tuesday
  • Work plan and Test document due
  • 11 PM Thursday, Oct 23
  • Program due
  • 11 PM Tuesday, Oct 28
  • Grace Date
  • 11 PM Friday, Oct 31

14
15
CS/SE 2430
You must come to the class every day, including
labs!
Yes, sir!
15
Write a Comment
User Comments (0)
About PowerShow.com