Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Inheritance - Introduction to Programming in Java - Lecture Slides, Slides of Network security

The key points are:Inheritance, Introduce Inheritance, Superclasses, Polymorphic Data Structures, Subclasses, Wrapper Classes, Inheritance Hierarchies, Classes and Types, Object Class, Collections and Primitive Types

Typology: Slides

2012/2013

Uploaded on 04/22/2013

sathaye
sathaye 🇮🇳

4.8

(8)

106 documents

Partial preview of the text

Download Inheritance - Introduction to Programming in Java - Lecture Slides and more Slides Network security in PDF only on Docsity!

Objectives

  • to introduce inheritance, superclasses,

subclasses, polymorphic data structures, and

wrapper classes

  1. Inheritance

Topics

• 1. The DoME Example

• 2. Inheritance Hierarchies

• 3. DoME using Inheritance

• 4. Polymorphism

• 5. The Revised Database Class

• 6. Classes and Types

• 7. A Vehicle Example

• 8. The Object Class

• 9. Collections and Primitive Types

DoME Classes

essModel

cannot display

the ArrayLists

properly

"uses"

DoME Objects

public void setComment(String com) { comment = com; }

public String getComment() { return comment; }

public void setOwn(boolean ownIt) // set the flag indicating whether we own this CD. { gotIt = ownIt; }

public boolean getOwn() // return true if we own a copy of this CD. { return gotIt; }

continued

public void print() // print details about this CD { System.out.print("CD: " + title + " (" + playingTime + " mins)"); if (gotIt) System.out.println("*"); else System.out.println(); System.out.println(" " + artist); System.out.println(" tracks: " + numberOfTracks); if (comment != null) System.out.println(" " + comment); } // end of print()

} // end of CD class

public void setComment(String com)

{ comment = com; }

public String getComment()

{ return comment; }

public void setOwn(boolean ownIt)

// set the flag indicating whether we own this DVD.

{ gotIt = ownIt; }

public boolean getOwn()

// return true if we own a copy of this DVD.

{ return gotIt; }

continued

public void print() // print details about this DVD { System.out.print("DVD: " + title + " (" + playingTime + " mins)"); if (gotIt) System.out.println("*"); else System.out.println(); System.out.println(" " + director); if (comment != null) System.out.println(" " + comment); } // end of print()

} // end of DVD class

public void list() // print a list of all currently stored CDs and DVDs { for (CD cd : cds) cd.print();

for (DVD dvd : dvds) dvd.print(); } // end of list()

} // end of Database class

Using the DoME Database

public class UseDome

{

public static void main(String[] args) { Database db = new Database();

CD beatles = new CD("the white album", "the beatles",13, 122); db.addCD( beatles); beatles.setComment("the best of the later period");

db.addCD( new CD("morrison hotel", "the doors", 11, 109)); db.addCD( new CD("dark side of the moon","pink floyd",9,100)); :

continued

Execution

Problems with DoME's Design

• Code duplication: the CD and DVD classes are

very similar

– it makes maintenance harder

– it introduces the danger of bugs

• The Database class also suffers from code

duplication.

3. DoME using Inheritance

"is a"

Compare the

fields and

methods with

those for CD

and DVD in

slide 4.

Inheritance Terminlogy

• The Item class is a superclass.

• The new versons of the CD and DVD classes

are subclasses

– the superclass defines fields (attributes) and

methods which are inherited by the subclasses

– the subclasses add extra fields and methods