abstract class interface

20
Structuring Code by Using  Abstract Classes and In terfaces

Upload: ratnabpatel9700

Post on 06-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 1/20

Structuring Code by Using

 Abstract Classes and Interfaces

Page 2: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 2/20

Objectives

 After completing this lesson, you should be able todo the following:

Define abstract classes

Define abstract methods

Define interfaces

Implement interfaces

Page 3: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 3/20

Defining Abstract Classes

 An abstract class cannot be instantiated.

 Abstract methods must be implemented by subclasses.

Interfaces support multiple inheritance.

Abstractsuperclass

Concretesubclasses

InventoryItem 

 Movie VCR 

Page 4: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 4/20

Creating Abstract Classes

Use the abstract keyword to declare a class asabstract.

 public abstract class InventoryItem {

 private float price;

 public boolean isRentable()…

}

 public class Movie

extends InventoryItem {

 private String title;

 public int getLength()… 

 public class Vcr

extends InventoryItem {

 private int serialNbr;

 public void setTimer()… 

Page 5: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 5/20

 What Are Abstract Methods?

 An abstract method:

Is an implementation placeholder

Is part of an abstract class

Must be overridden by a concrete subclass

Each concrete subclass can implement the method differently.

Page 6: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 6/20

Defining Abstract Methods

Use the abstract keyword to declare a method as

abstract:

Provide the method signature only.

The class must also be abstract.  Why is this useful?

Declare the structure of a given class without providingcomplete implementation of every method.

 public abstract class InventoryItem {

 public abstract boolean isRentable();

Page 7: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 7/20

Defining and Using Interfaces

 An interface is like a fully abstract class:

 All its methods are abstract.

 All variables are public static final.

 An interface lists a set of method signatures withoutany code details.

 A class that implements the interface must providecode details for all the methods of the interface.

 A class can implement many interfaces but canextend only one class.

Page 8: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 8/20

Examples of Interfaces

Interfaces describe an aspect of behavior that different classesrequire.

For example, classes that can be steered support the“steerable” interface. 

Classes can be unrelated.

SteerableNonsteerable

Page 9: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 9/20

Creating Interfaces

Use the interface keyword:

 All methods are public abstract.

 All variables are public static final.

 public interface Steerable {

int MAXTURN = 45;

void turnLeft(int deg);void turnRight(int deg);

}

Page 10: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 10/20

Implementing Interfaces

Use the implements keyword:

 public class Yacht extends Boat

implements Steerable {

 public void turnLeft(int deg) {…} 

 public void turnRight(int deg) {…} 

}

Page 11: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 11/20

Sort: A Real-World Example

Is used by several unrelated classes

Contains a known set of methods

Is needed to sort any type of object

Uses comparison rules that are known only to the sortable

object

Supports good code reuse

Page 12: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 12/20

Overview of the Classes

Created by the sort expert:

Created by the movie expert:

 public class

 MyApplication

 public class Movie

implements Sortable

 public interface

Sortable

 public abstract

class Sort

Page 13: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 13/20

How the Sort Works

 MyApplication passes

an array of movies toSort.sortObjects(). 

sortObjects() 

asks a movie to

compare itself withanother movie.

The moviereturns the

result of thecomparison. 

sortObjects() 

returns thesorted list.  1

23

4

Sort

Movie

MyApplication

Page 14: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 14/20

The Sortable Interface

Specifies the compare() method:

 public interface Sortable {

// compare(): Compare this object to another object

// Returns:

// 0 if this object is equal to obj2

// a value < 0 if this object < obj2

// a value > 0 if this object > obj2

int compare(Object obj2);}

Page 15: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 15/20

The Sort Class

Holds sortObjects():

 public abstract class Sort {

 public static void sortObjects(Sortable[] items) {

// Step through the array comparing and swapping;

// do this length-1 times

for (int i = 1; i < items.length; i++) {

for (int j = 0; j < items.length - 1; j++) {

if (items[j].compare(items[j+1]) > 0) {Sortable tempitem = items[j+1];

items[j+1] = items[j];

items[j] = tempitem; } } } } }

Page 16: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 16/20

The Movie Class

Implements Sortable:

 public class Movie extends InventoryItem 

implements Sortable {

String title; public int compare(Object movie2) {

String title1 = this.title;

String title2 = ((Movie)movie2).getTitle();

return(title1.compareTo(title2));

}

}

Page 17: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 17/20

Using the Sort

Call Sort.sortObjects(Sortable []) with anarray of Movie as the argument:

class myApplication {

 Movie[] movielist;

… // build the array of Movie 

Sort.sortObjects(movielist);

}

Page 18: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 18/20

Using instanceof with Interfaces

Use the instanceof operator to determine whetheran object implements an interface.

Use downcasting to call methods that are defined inthe interface:

 public void aMethod(Object obj) {

if (obj instanceof Sortable)

((Sortable)obj).compare(obj2);

}

Page 19: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 19/20

Summary 

In this lesson, you should have learned the following:

 An abstract class cannot be instantiated.

 An abstract method has a signature but no code.

 An interface is a collection of abstract methods to beimplemented elsewhere.

 A class can implement many interfaces.

Implementing more than one interface is comparable tomultiple inheritance.

Page 20: Abstract Class Interface

8/3/2019 Abstract Class Interface

http://slidepdf.com/reader/full/abstract-class-interface 20/20

Practice 12: Overview 

This practice covers: Making an interface and abstract class

Implementing the java.lang.Comparable 

interface to sort objects Testing the abstract and interface classes