Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC150 Inheritance Details Chapter 9. CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title.

Similar presentations


Presentation on theme: "CPSC150 Inheritance Details Chapter 9. CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title."— Presentation transcript:

1 CPSC150 Inheritance Details Chapter 9

2 CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title + "(" + playingTime + " mins)"); if(gotIt) { System.out.println("*"); } else { System.out.println(); } System.out.println(" " + comment); } ver 1 (from CD class, no inheritance) public void print() { 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); System.out.println(" " + comment); } { fields not printed in ver 2

3 CPSC150 Print problems Inheritance is one way: CD and Video inherit what is up (if fields/methods are public), but Item cannot inherit DOWN Possible solution: –move print method down to CD and Video –What do you think will happen? just like draw in Shapes

4 CPSC150 Print problem Solution #1 CD can't access private fields –could make accessor methods. for now, print accessible fields. Database can't access print –make print method for Item Compiles. What does it print? –Item print method not used

5 CPSC150 Lessons compile time needs static method there At run time, method used is from dynamic type Problems prints just kid's fields. still need fields from Item

6 CPSC150 Solution #2 super.print( ) calls parent's method. Unlike super ( ) constructor: –method name is explicitly stated with any parameters –can be called anywhere from any method –not automatically called

7 CPSC150 Problem What if we don't want title first Solution: –put super.print( ) second Problem What if we want to intermingle Item fields and CD fields Must print some of Item fields, then some of CD fields

8 CPSC150 Solution: Accessor Methods But should be available just to children Make them protected Why not make fields protected? –fields should always be private // in Circle.java protected String getColor( ) { return color; }

9 CPSC150 Your turn Write protected accessor for fields in Item: public class Item { private String title; private int playingTime; private boolean gotIt; private String comment; // methods omitted }

10 CPSC150 Problem Now, we don't need Item's print method What happens if we delete it? Solution Make print method abstract

11 CPSC150 Abstract Classes Classes can be abstract if they should not have objects instantiating them Classes MUST be abstract if they have any abstract methods or fields fields or methods can be abstract Classes can be abstract without any abstract fields or methods Abstract classes can have non-abstract fields and methods

12 CPSC150 Why Abstract Methods Abstract Methods require any children to implement that method Compile time error if print method not in subclass Allow clients that use the code to compile with the guarantee that that method will be implemented

13 CPSC150 Syntax of abstract methods abstract public class MyClass { // regular constructors and methods that are called in a regular way abstract method signature ; //no body }

14 CPSC150 Syntax of abstract methods abstract public class Item { // no abstract fields private String title; private int playingTime; // rest of fields abstract public void print() ; public Item(String theTitle, int time) { // same constructor. unchanged. // invoked by children with super( ) // cannot be used except by children } // other regular methods }

15 CPSC150 Your turn Write the abstract class Item. Make the print method abstract


Download ppt "CPSC150 Inheritance Details Chapter 9. CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title."

Similar presentations


Ads by Google