Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstract Classes and Interfaces

Similar presentations


Presentation on theme: "Abstract Classes and Interfaces"— Presentation transcript:

1 Abstract Classes and Interfaces
Chapter 9

2 Print in Entertainment
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); 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); fields not printed in ver 2 {

3 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 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 Lessons Problems compile time needs method in statically checked class
At run time, method used is from dynamic type Problems prints just kid's fields. still need fields from Item

6 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 Problem 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 Solution: Accessor Methods
But should be available just to children Make them protected Why not make fields protected? fields should always be private protected void getTitle( ) { return title; }

9 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 Problem Solution Now, we don't need Item's print method
What happens if we delete it? Solution Make print method abstract

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

12 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 methods can be abstract Classes can be abstract without any abstract fields or methods Abstract classes can have non-abstract fields and methods

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

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

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

16 Interfaces Chapter

17 Inheritance Review No different than any other class. Has no access to or information about subclasses private in parent is not accessible to children; protected is class SubClass1 extends SuperClass first line of constructor is super( ); Subclasses can have only one parent can use methods from SubClass1 or SuperClass

18 Inheritance Review Two reasons for inheritance
To use the methods of the parent To use polymorphism (e.g. add Item to a library, but each Item is a CD or Video)

19 Abstract Classes Review
Abstract methods enable polymorphism, but have no body Classes with abstract methods must be abstract Abstract classes can not have objects created from them Abstract classes can have useful concrete methods (e.g., setComment) and fields (e.g., Xposition)

20 Abstract Class Review Two reasons for Abstract Classes
Enables polymorphism when methods are not appropriate for superclass (e.g., draw in Shapes or print in Item) Enforces a specification (in order to be a Shape, you must have a draw method)

21 One more detail: Interfaces
Allow multiple inheritance Be an animal AND black (other things are animals and other things are black) Can specify exactly what is needed for a concrete class actionPerformed

22 Interfaces To implement in interface (parent), put:
public interface MyInterface instead of public class MyInterface in class using interface (child), put: public class SubClass1 extends SuperClass implements MyInterface

23 Two arrows Two arrows

24 Interfaces concept Just like a class, Like abstract class on speed
but no variables (can have static final public fields) no bodies for ANY method Like abstract class on speed Purpose? polymorphism specification

25 Interface Examples Comparator ActionListener

26 Abstract Classes vs Interfaces
Use interfaces when possible because there can lots of classes following "implements" but only one "extends" Use interfaces if you do not want any fields or methods in your parent class Use abstract classes if you want subclasses to use common defined methods or fields

27 Problem 10.54 U u; G g; B b; Z z; X x;
Look at the code below. You have five types (classes or interfaces) U, G, B, Z, and X, and a variable of each of these types. What can you say about the relationships of the types? U u; G g; B b; Z z; X x; The following assignments are all illegal (they cause compiler errors): u = b; x = g; b = u; z = u; g = x; The following assignments are all legal: u = z; x = b; g = u; x = u;

28 Wrapper Classes, 8.9.3 Many times an object is wanted instead of a primitive. Use a wrapper class: wraps the primitive around a java.lang wrapper class int i = 20; Integer iwrap = new Integer(i); myCollection.add(iwrap); // added as an Integer int iwrapvalue = iwrap.intValue( 37);


Download ppt "Abstract Classes and Interfaces"

Similar presentations


Ads by Google