Inheritance: is a - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Inheritance: is a

Description:

class body goes here. public class InterestCheckingAccount. extends CheckingAccount ... package gray.adts.shapes; public final class Cylinder extends Circle ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 23
Provided by: ChungC7
Category:

less

Transcript and Presenter's Notes

Title: Inheritance: is a


1
Inheritance is a
A Checking Account is a Bank Account
A Interest Checking Account is a Checking Account
public class CheckingAccount extends BankAccount
// class body goes here public class
InterestCheckingAccount
extends CheckingAccount // class body goes
here
2
Javas World in UML
Object
This is done implicitly
Shape
abstract
extends
Circle
Rectangle
ThreeD
interface
RectanglePrism
Cylinder
implements
3
Shape (I)
  • package gray.adts.shapes
  • public abstract class Shape
  • protected static final double DEFAULT_SIZE (
    double ) 1.0
  • protected static final String DEFAULT_NAME
    "Unknown"
  • private String shapeName
  • public Shape()
  • this.shapeName DEFAULT_NAME
  • public Shape( String name )
  • setShapeName( name )
  • protected void setShapeName( String name )
  • if ( name.trim().length() 0 ) shapeName
    DEFAULT_NAME
  • else shapeName new String( name )
  • public String getShapeName()
  • return shapeName

4
Shape (II)
  • package gray.adts.shapes
  • public abstract class Shape
  • .........
  • .........
  • /
  • Get the surface area of this Shape
  • and return the surface area of this Shape
  • /
  • public abstract double getSurfaceArea()
  • /
  • Get the perimeter of this ltttgtShapelt/ttgt.
  • and return the perimeter of this
    ltttgtShapelt/ttgt
  • /
  • public abstract double getPerimeter()

5
There are some details that are not as important
to the concepts (classes)
6
Circle (I)
  • package gray.adts.shapes
  • public class Circle extends Shape
  • private double radius
  • public Circle()
  • super( "Circle" )
  • setRadius( super.DEFAULT_SIZE )
  • public Circle( double theRadius )
  • super( "Circle" )
  • if ( theRadius lt 0.0 )
  • setRadius( Shape.DEFAULT_SIZE )
  • else
  • setRadius( theRadius )

7
Circle (II)
  • package gray.adts.shapes
  • public class Circle extends Shape
  • private double radius
  • .....
  • .....
  • public double getRadius()
  • return this.radius
  • public void setRadius( double theRadius )
  • if ( theRadius lt 0 )
  • return
  • this.radius theRadius
  • public double getSurfaceArea()
  • return this.radius this.radius Math.PI

8
equality and what is
  • double s,t
  • ....
  • ....
  • if (s t) ....
  • ....
  • ....
  • System.out.println(This value of s is s)
  • ....
  • ....

9
Objects toString and equals methods
the default method use identities to do the job.
Shape
abstract
Now, we have a better idea about how this job to
be done.
Circle
Rectangle
10
Rectangle
  • package gray.adts.shapes
  • public class Rectangle extends Shape
  • ....
  • ....
  • public String toString()
  • return this.getShapeName() " length "
    this.length
  • ", height " this.height
  • public boolean equals( Object o )
  • if ( ( o null ) ( ! ( o instanceof
    Rectangle ) ) )
  • return false

So, this method can take any object of any class.
11
Extend to three-dimension
Shape
abstract
How to unify the interface?
Circle
Rectangle
Cylinder
RectanglePrism
12
interface
Object
Shape
abstract
Circle
Rectangle
RectanglePrism
Cylinder
implements
13
ThreeD
  • package gray.adts.shapes
  • public interface ThreeD
  • double getDepth()
  • void setDepth( double theDepth )
  • double getVolume()

must be all abstract
Any class implements the interface must implement
all methods in the interface.
Some interfaces dont even have the body called
marker interface.
14
Cylinder (I)
  • package gray.adts.shapes
  • public final class Cylinder extends Circle
    implements ThreeD
  • private double depth
  • public Cylinder()
  • this( Shape.DEFAULT_SIZE, Shape.DEFAULT_SIZE
    )
  • public Cylinder( double theRadius, double
    theWidth )
  • setShapeName( "Cylinder" )
  • if ( theRadius lt 0.0 )
  • setRadius( Shape.DEFAULT_SIZE )
  • else
  • setRadius( theRadius )
  • if ( theWidth lt 0.0 )
  • setDepth( Shape.DEFAULT_SIZE )
  • else

15
Cylinder (II)
  • package gray.adts.shapes
  • public final class Cylinder extends Circle
    implements ThreeD
  • private double depth
  • ....
  • public double getSurfaceArea()
  • return 2 super.getSurfaceArea()
  • 2 Math.PI getRadius() getDepth()
  • public double getPerimeter()
  • return 2 super.getPerimeter() 4
    this.depth
  • public double getVolume()
  • return this.depth super.getSurfaceArea()
  • public double getDepth() return this.depth
  • public void setDepth( double theDepth )

16
Cylinder (III)
  • package gray.adts.shapes
  • public final class Cylinder extends Circle
    implements ThreeD
  • private double depth
  • ....
  • public String toString()
  • return super.toString()
  • ", depth " getDepth()
  • public boolean equals( Object o )
  • if ( super.equals( o )
  • ( ( Cylinder ) o ).getDepth()
    this.getDepth()
  • )
  • return true
  • else
  • return false

17
UML Extends and Implements
Extending 2D shapes to 3D shapes
extends
UML inheritance diagram The dotted line
indicates a class implements an interface. For
example, Cylinder extends Circle and implements
ThreeD.
implements
See next slide
18
Example of using shapes
  • import gray.adts.shapes.
  • import javax.swing.JOptionPane
  • public class TestShape
  • public static void main ( String args )
  • String output
  • Cylinder c1 new Cylinder( 3.5, 7.2 )
  • Cylinder c2
  • Shape s c1
  • RectangularPrism rp1 new RectangularPrism()
  • Cylinder cyl1 new Cylinder()
  • c2 (Cylinder) s
  • output "c1 " c1.toString() "\n"
  • output "c2 " c2.toString() "\n"
  • output c2 volume " c2.getVolume()
    "\n"

19
Generic Types in Java
Format for a generic (parameterized) type and
instantiation of a generic type
Type variable
public class Pair lt T gt ... ... T ... ... ...
T ...
Type declaration
Pair lt Integer gt aPair1, aPair2
Type instantiation
Actual Type
20
A generic type declaration (I)
  • public class PairltTgt
  • private T firstElement
  • private T secondElement
  • public Pair()
  • this.firstElement null
  • this.secondElement null
  • public Pair(T e1, T e2)
  • if ( (e1 null) (e2 null))
  • throw new NullPointerException()
  • this.firstElement e1
  • this.secondElement e2
  • public T getFirstElement()
  • return this.firstElement

21
A generic type declaration (II)
  • public class PairltTgt
  • private T firstElement
  • private T secondElement
  • ....
  • ....
  • public void swapElements()
  • T temp this.firstElement
  • this.firstElement this.secondElement
  • this.secondElement temp
  • public T equals(T e)
  • if (e null)
  • throw new NullPointerException()
  • if(this.firstElement.equals(e)
    this.secondElement.equals(e))
  • return e

22
A generic type instantiation
Pair lt Integer gt intPair Pair lt String gt
strPair new PairltStringgt(ISU, ITK
179) intPair new PairltIntegergt( new
Integer(1), new
Integer(2))
Write a Comment
User Comments (0)
About PowerShow.com