Inheritance is a
Sponsored Links
This presentation is the property of its rightful owner.
1 / 22

Inheritance: is a PowerPoint PPT Presentation


  • 48 Views
  • Uploaded on
  • Presentation posted in: General

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

Download Presentation

Inheritance: is a

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


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

}

ITK 179


Object

This is done

implicitly

Shape

{abstract}

extends

Java’s World in UML

Rectangle

Circle

ThreeD

{interface}

RectanglePrism

Cylinder

implements

ITK 179


Shape (I)

package gray.adts.shapes;

publicabstractclass Shape {

protectedstaticfinaldoubleDEFAULT_SIZE = ( double ) 1.0;

protectedstaticfinal String DEFAULT_NAME = "Unknown";

private String shapeName;

public Shape() {

this.shapeName = DEFAULT_NAME;

}

public Shape( String name ) {

setShapeName( name );

}

protectedvoid setShapeName( String name ) {

if ( name.trim().length() == 0 ) shapeName = DEFAULT_NAME;

elseshapeName = new String( name );

}

public String getShapeName() {

returnshapeName;

}

.....

.....

}

ITK 179


Shape (II)

package gray.adts.shapes;

publicabstractclass Shape {

.........

.........

/*

* Get the surface area of this Shape

* and return the surface area of this Shape

*/

publicabstractdouble getSurfaceArea();

/**

* Get the perimeter of this <tt>Shape</tt>.

* and return the perimeter of this <tt>Shape</tt>

*/

publicabstractdouble getPerimeter();

}

ITK 179


There are some details

that are not as important to the concepts (classes)

ITK 179


Circle (I)

package gray.adts.shapes;

publicclass Circle extends Shape {

privatedoubleradius;

public Circle() {

super( "Circle" );

setRadius( super.DEFAULT_SIZE );

}

public Circle( double theRadius ) {

super( "Circle" );

if ( theRadius <= 0.0 ) {

setRadius( Shape.DEFAULT_SIZE );

}

else {

setRadius( theRadius );

}

}

......

......

}

ITK 179


Circle (II)

package gray.adts.shapes;

publicclass Circle extends Shape {

privatedoubleradius;

.....

.....

publicdouble getRadius() {

returnthis.radius;

}

publicvoid setRadius( double theRadius ) {

if ( theRadius <= 0 ) {

return;

}

this.radius = theRadius;

}

publicdouble getSurfaceArea() {

returnthis.radius * this.radius * Math.PI;

}

publicdouble getPerimeter() {

return 2 * this.radius + Math.PI;

}

}

ITK 179


equality and “what is”

double s,t;

....

....

if (s == t) ....

....

....

System.out.println(“This value of s is “ + s);

....

....

ITK 179


the default method

use identities to do the job.

Object’s toString andequals methods

Shape

{abstract}

Now, we have a better idea about how this job to be done.

Rectangle

Circle

ITK 179


Rectangle

package gray.adts.shapes;

publicclass Rectangle extends Shape {

....

....

public String toString() {

returnthis.getShapeName() + ": length = " + this.length

+ ", height = " + this.height;

}

publicboolean equals( Object o ) {

if ( ( o == null ) || ( ! ( o instanceof Rectangle ) ) ) {

returnfalse;

}

return ( ( ( ( Rectangle ) o ).getLength() == getLength() ) &&

( ( ( Rectangle ) o ).getHeight() == getHeight() ) );

}

.....

}

So, this method can take any object of any class.

ITK 179


Shape

{abstract}

Extend to three-dimension

How to unify

the interface?

Rectangle

Circle

RectanglePrism

Cylinder

ITK 179


Object

Shape

{abstract}

interface

Rectangle

Circle

RectanglePrism

Cylinder

implements

ITK 179


ThreeD

package gray.adts.shapes;

publicinterface 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 don’t even have the body

called marker interface.

ITK 179


Cylinder (I)

package gray.adts.shapes;

publicfinalclass Cylinder extends Circle implements ThreeD {

privatedoubledepth;

public Cylinder() {

this( Shape.DEFAULT_SIZE, Shape.DEFAULT_SIZE );

}

public Cylinder( double theRadius, double theWidth ) {

setShapeName( "Cylinder" );

if ( theRadius <= 0.0 ) {

setRadius( Shape.DEFAULT_SIZE );

}

else {

setRadius( theRadius );

}

if ( theWidth <= 0.0 ) {

setDepth( Shape.DEFAULT_SIZE );

}

else {

setDepth( theWidth );

}

}

....

}

ITK 179


Cylinder (II)

package gray.adts.shapes;

publicfinalclass Cylinder extends Circle implements ThreeD {

privatedoubledepth;

....

publicdouble getSurfaceArea() {

return 2 * super.getSurfaceArea()

+ 2 * Math.PI * getRadius() * getDepth();

}

publicdouble getPerimeter() {

return 2 * super.getPerimeter() + 4 * this.depth;

}

publicdouble getVolume() {

returnthis.depth * super.getSurfaceArea();

}

publicdouble getDepth() {returnthis.depth;}

publicvoid setDepth( double theDepth ) {

if ( theDepth <= 0 ) return;

this.depth = theDepth;

....

}

ITK 179


Cylinder (III)

package gray.adts.shapes;

publicfinalclass Cylinder extends Circle implements ThreeD {

privatedoubledepth;

....

public String toString() {

returnsuper.toString()

+ ", depth = " + getDepth();

}

publicboolean equals( Object o ) {

if ( super.equals( o )

&& ( ( Cylinder ) o ).getDepth() == this.getDepth()

) {

returntrue;

}

else {

returnfalse;

}

}

}

ITK 179


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

ITK 179


Example of using shapes

import gray.adts.shapes.*;

import javax.swing.JOptionPane;

publicclass TestShape {

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

JOptionPane.showMessageDialog( null, output,

"shape-cylinder cast exercise",

JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 );

}

}

ITK 179


Generic Types in Java

Format for a generic (parameterized) type and instantiation of a generic type

Type variable

publicclass Pair < T > {

...

... T ...

... ... T ...

}

Type declaration

Pair < Integer > aPair1, aPair2;

Type instantiation

Actual Type

ITK 179


A generic type declaration (I)

publicclass Pair<T> {

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

thrownew NullPointerException();

}

this.firstElement = e1;

this.secondElement = e2;

}

public T getFirstElement() {

returnthis.firstElement;

}

.....

}

ITK 179


A generic type declaration (II)

publicclass Pair<T> {

private T firstElement;

private T secondElement;

....

....

publicvoid swapElements() {

T temp = this.firstElement;

this.firstElement = this.secondElement;

this.secondElement = temp;

}

public T equals(T e) {

if (e == null) {

thrownew NullPointerException();

}

if(this.firstElement.equals(e) || this.secondElement.equals(e)){

return e;

}

elsereturnnull;

}

.....

}

ITK 179


A generic type instantiation

Pair < Integer > intPair;

Pair < String > strPair = new Pair<String>(“ISU”, “ITK 179”);

intPair = new Pair<Integer>( new Integer(1),

new Integer(2));

ITK 179


  • Login