Presentation is loading. Please wait.

Presentation is loading. Please wait.

12 Copyright © 2005, Oracle. All rights reserved. Structuring Code Using Abstract Classes and Interfaces.

Similar presentations


Presentation on theme: "12 Copyright © 2005, Oracle. All rights reserved. Structuring Code Using Abstract Classes and Interfaces."— Presentation transcript:

1 12 Copyright © 2005, Oracle. All rights reserved. Structuring Code Using Abstract Classes and Interfaces

2 12-2 Copyright © 2005, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Define abstract classes Define abstract methods Define interfaces Implement interfaces

3 12-3 Copyright © 2005, Oracle. All rights reserved. Defining Abstract Classes An abstract class cannot be instantiated. Abstract methods must be implemented by subclasses. Interfaces support multiple inheritance. Abstract superclass Concrete subclasses InventoryItem MovieVCR

4 12-4 Copyright © 2005, Oracle. All rights reserved. Creating Abstract Classes Use the abstract keyword to declare a class as abstract. 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()…

5 12-5 Copyright © 2005, Oracle. All rights reserved. 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.

6 12-6 Copyright © 2005, Oracle. All rights reserved. Notes Page

7 12-7 Copyright © 2005, Oracle. All rights reserved. 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 providing complete implementation of every method. public abstract class InventoryItem { public abstract boolean isRentable(); …

8 12-8 Copyright © 2005, Oracle. All rights reserved. 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 without any code details. A class that implements the interface must provide code details for all the methods of the interface. A class can implement many interfaces but can extend only one class.

9 12-9 Copyright © 2005, Oracle. All rights reserved. Examples of Interfaces Interfaces describe an aspect of behavior that different classes require. For example, classes that can be steered support the steerable interface. Classes can be unrelated. SteerableNonsteerable

10 12-10 Copyright © 2005, Oracle. All rights reserved. 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); }

11 12-11 Copyright © 2005, Oracle. All rights reserved. Notes Page

12 12-12 Copyright © 2005, Oracle. All rights reserved. Implementing Interfaces Use the implements keyword: public class Yacht extends Boat implements Steerable { public void turnLeft(int deg) {…} public void turnRight(int deg) {…} }

13 12-13 Copyright © 2005, Oracle. All rights reserved. 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

14 12-14 Copyright © 2005, Oracle. All rights reserved. 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

15 12-15 Copyright © 2005, Oracle. All rights reserved. How the Sort Works MyApplication passes an array of movies to Sort.sortObjects(). sortObjects() asks a movie to compare itself with another movie. The movie returns the result of the comparison. sortObjects() returns the sorted list. 1 23 4 Sort Movie MyApplication

16 12-16 Copyright © 2005, Oracle. All rights reserved. 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); }

17 12-17 Copyright © 2005, Oracle. All rights reserved. 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; } } } } }

18 12-18 Copyright © 2005, Oracle. All rights reserved. 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)); }

19 12-19 Copyright © 2005, Oracle. All rights reserved. Using the Sort Call Sort.sortObjects(Sortable []) with an array of Movie as the argument: class myApplication { Movie[] movielist; … // build the array of Movie Sort.sortObjects(movielist); }

20 12-20 Copyright © 2005, Oracle. All rights reserved. Using instanceof with Interfaces Use the instanceof operator to determine whether an object implements an interface. Use downcasting to call methods that are defined in the interface: public void aMethod(Object obj) { … if (obj instanceof Sortable) ((Sortable)obj).compare(obj2); }

21 12-21 Copyright © 2005, Oracle. All rights reserved. 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 be implemented elsewhere. A class can implement many interfaces. Implementing more than one interface is comparable to multiple inheritance.

22 12-22 Copyright © 2005, Oracle. All rights reserved. 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

23 12-23 Copyright © 2005, Oracle. All rights reserved. Notes Page for Practice 12

24 12-24 Copyright © 2005, Oracle. All rights reserved. Practice 12: Notes

25 12-25 Copyright © 2005, Oracle. All rights reserved. Practice 12: Notes

26 12-26 Copyright © 2005, Oracle. All rights reserved. Practice 12: Notes

27 12-27 Copyright © 2005, Oracle. All rights reserved. Practice 12: Notes

28 12-28 Copyright © 2005, Oracle. All rights reserved. Practice 12: Notes


Download ppt "12 Copyright © 2005, Oracle. All rights reserved. Structuring Code Using Abstract Classes and Interfaces."

Similar presentations


Ads by Google