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

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Advanced Piloting Cruise Plot.
Final and Abstract Classes
1 Inheritance Classes and Subclasses Or Extending a Class.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Chapter 1 The Study of Body Function Image PowerPoint
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 1 Embedded Computing.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 5 Author: Julia Richards and R. Scott Hawley.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
UNITED NATIONS Shipment Details Report – January 2006.
19 Copyright © 2005, Oracle. All rights reserved. Distributing Modular Applications: Developing Web Services.
12 Copyright © 2005, Oracle. All rights reserved. Implementing Business Tasks with Session EJBs.
9 Copyright © 2005, Oracle. All rights reserved. Using Strings, String Buffer, Wrapper, and Text-Formatting Classes.
11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.
3 Copyright © 2005, Oracle. All rights reserved. Basic Java Syntax and Coding Conventions.
8 Copyright © 2005, Oracle. All rights reserved. Object Life Cycle and Inner Classes.
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
6 Copyright © 2005, Oracle. All rights reserved. Building Applications with Oracle JDeveloper 10g.
17 Copyright © 2005, Oracle. All rights reserved. Deploying Applications by Using Java Web Start.
Summary of Convergence Tests for Series and Solved Problems
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
Determine Eligibility Chapter 4. Determine Eligibility 4-2 Objectives Search for Customer on database Enter application signed date and eligibility determination.
My Alphabet Book abcdefghijklm nopqrstuvwxyz.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Addition Facts
Year 6 mental test 5 second questions
Around the World AdditionSubtraction MultiplicationDivision AdditionSubtraction MultiplicationDivision.
ABC Technology Project
Object Oriented Programming with Java
VOORBLAD.
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain.
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Squares and Square Root WALK. Solve each problem REVIEW:
© 2012 National Heart Foundation of Australia. Slide 2.
Lets play bingo!!. Calculate: MEAN Calculate: MEDIAN
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
Chapter 5 Test Review Sections 5-1 through 5-4.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
Addition 1’s to 20.
25 seconds left…...
Januar MDMDFSSMDMDFSSS
Week 1.
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
PSSA Preparation.
Immunobiology: The Immune System in Health & Disease Sixth Edition
Immunobiology: The Immune System in Health & Disease Sixth Edition
Abstract Class, Packages and interface from Chapter 9
1 Abstract Class and Packages from Chapter 9 Lecture.
Data Structures Using C++ 2E
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
1 Role of Interfaces in Large-Scale Software l API:Application Programmer’s Interface: The methods that allow a programmer to manipulate the data elements.
1 Lecture 08(API)Lecture 11 Java API and Interfaces l API:Application Programmer’s Interface: The methods that allow a programmer to manipulate the data.
Presentation transcript:

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

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

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

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()…

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.

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

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(); …

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.

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

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); }

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

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) {…} }

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

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

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 Sort Movie MyApplication

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); }

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; } } } } }

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)); }

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); }

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); }

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.

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

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

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

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

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

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

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