CS2 Module 26 Category: OO Concepts Topic: Interfaces Objectives –Interfaces.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Polymorphism.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Interfaces.
Topic 5a – Interfaces Friends share all things. CISC370/Object Oriented Programming with Java.
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
1 interfaces Professor Evan Korth. review Based on the GeometricObject -> Circle -> Cylinder hierarchy from last class, which of these are legal? GeometricObject.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
CC1007NI: Further Programming Week Dhruba Sen Module Leader (Islington College)
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Polymorphism & Interfaces
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
1 1 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code to respond.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
CS2210: SW Development Methods Topics: Comparable and Comparator interfaces in JCF Function objects Textbook readings: More from MSD, Chapter 9 Pages
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Classes, Interfaces and Packages
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
OOP Basics Classes & Methods (c) IDMS/SQL News
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
ISBN Chapter 12 Support for Object-Oriented Programming.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Interfaces Professor Evan Korth.
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Chapter 13 Abstract Classes and Interfaces
Interfaces.
Review of Previous Lesson
Creating and Modifying Text part 3
Chapter 13 Abstract Classes and Interfaces Part 01
Inheritance Lakshmish Ramaswamy.
Presentation transcript:

CS2 Module 26 Category: OO Concepts Topic: Interfaces Objectives –Interfaces

CS 2 Introduction to Object Oriented Programming Module 26 OO Concepts Interfaces

Some languages allow multiple inheritance: Multiple inheritance leads to many potentially confusing naming problems (e.g. Pet.doYourThing() vs. Animal.doYourThing()) Growing consensus: the benefits of multiple inheritance aren’t worth the problems. Java has single inheritance only. No multiple inheritance Java has a construct that can give us much of the functionality of multiple inheritance: The Interface Animal Dog Pet Two parent classes, one child class No Multiple Inheritance in Java

What is an Interface? “An interface is a named collection of method definitions (without definitions). An interface can also declare constants.” – The Java Tutorial, Sun Microsystems Sounds similar to an abstract class, but has significant differences: 1.Interfaces cannot have any methods with implementation 2.A class can implement many interfaces, but can inherit from only one superclass 3.Interfaces are not part of a class hierarchy, so unrelated classes can implement the same interface. Purpose:Define a protocol of behavior that can be provided by any class. Any class claiming to “implement” a given interface is then contractually obligated to provide implementations of all the methods listed in the interface.

How Do Interfaces Relate to Multiple Inheritance? the class implementing an interface behaves as if it were inheriting from a parent class, only better: –it implements all of the abstract methods itself –there is no other hidden, inherited stuff to collide with (no name confusion) You can implement as many interfaces as you wish with no fear of the problems of multiple inheritance

Interface Example We want a container class that can sort the items it holds. What method will be used to compare two objects? Will we have such a method? We make a kind of specification, a protocol: –We promise that any object we want to store in this sorting container class will have a method allowing it to be compared to other objects. –Also that method will have a known signature. How do we formalize this? Use of an interface

Note to Instructors Change this so it uses something other than Comparable Some of the Comparable slides already have been copied to the Generic classes lecture

Interface java.lang.Comparable Comparable is a predefined interface that is part of Java. Straight from the API: “This interface imposes a total ordering on the objects of each class that implements it.” (Look it up yourself!) public interface Comparable { public int compareTo(Object o) } Returns: a negative integer, zero, or a positive integer as the calling object is less than, equal to, or greater than the object specified as a parameter.

Suppose... We want to store Box objects and for our purposes we decide to compare boxes based on their volume. We modify our Box class thusly:

Suppose... public class Box implements Comparable { // Same as before including getVolume method // but omitted for the sake of the PowerPoint slide public int compareTo(Object o) { int retval = -1; if(o instanceOf Box) { retVal = getVolume() - ((Box)o).getVolume(); if(retVal == 0) retVal = 0; else if(retVal > 0) retVal = 1; else retval = -1; } // if return retval; }

Running the code... Called like this: Box a = new Box(10, 20, 30); Box b = new Box(2, 4, 6); Box c = new Box(20, 10, 30); System.out.println(a.compareTo(b)); ===> 1 System.out.println(a.compareTo(c)); ===> 0 System.out.println(b.compareTo(c)); ===> -1 System.out.println(a.compareTo("Hello")); ===> - 1;

Don't be nervous! If we say that some object has to be “Comparable” it's like saying: It must be an object (that is, it must have an is-a relationship with java.lang.Object. It pretty much will satisfy that by default.), AND It must have a compareTo method implemented. (So just make sure you write a compareTo method that has the same signature of that found in the interface java.lang.Comparable Making your class implement the Comparable interface is just like making a contractual obligation to provide that method – the compareTo method.

Another use!!! Slick way to provide constants

We can create a regular class just for constants class Constants { public final static int FEETPERMILE = 5280; public final static String PROMPT = "Enter a number"; public final static double PI = ; } Sample usage: feet = miles * Constants.FEETPERMILE;

If instead we say interface Constants { public final static int FEETPERMILE = 5280; public final static String PROMPT = "Enter a number"; public final static double PI = ; } Sample usage: public class Box implements Constants { // lots of code omitted feet = miles * FEETPERMILE;

Questions?