Topic 5a – Interfaces Friends share all things. CISC370/Object Oriented Programming with Java.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Lecture 5: Interfaces.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
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.
Inheritance Inheritance Reserved word protected Reserved word super
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 interfaces Professor Evan Korth. review Based on the GeometricObject -> Circle -> Cylinder hierarchy from last class, which of these are legal? GeometricObject.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
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 04 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
Object Oriented Design and UML
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
I NTERFACES Prepared by Miss Simab Shahid Lecturer computer Science and Software Engineering department, University of Hail Chapter.
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,
CISC6795: Spring Object-Oriented Programming: Polymorphism.
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 03 / 19 / 2007 Instructor: Michael Eckmann.
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()
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
Advanced Programming Rabie A. Ramadan vpro/ Lecture 4.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
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.
Chapter 6 Interfaces. Class Status CU will be close for winter break from Dec. 22. till Jan.2 We have 3 classes left after tonight (Jan 8,15, and 22)
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
Interfaces An interface is like an extreme case of an abstract class – However, an interface is not a class – It is a type that can be satisfied by any.
Interfaces and Inner Classes
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
1 Interface Design. 2 concept An interface is a way to describe what classes should do, without specifying how they should do it. It’s not a class but.
CS2 Module 26 Category: OO Concepts Topic: Interfaces Objectives –Interfaces.
Classes, Interfaces and Packages
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
Chapter Goals To be able to declare and use interface types
Inheritance and Polymorphism
Interfaces Professor Evan Korth.
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
Lecture 4: Interface Design
CMSC 202 Interfaces.
Introduction interface in Java is a blueprint of a class. It has static constants and abstract methods only. An interface is a way to describe what classes.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Lecture 16 - Interfaces Professor Adams.
Object Oriented Programming
CMSC 202 Interfaces.
Interfaces.
Chapter 13 Abstract Classes and Interfaces Part 01
Object-Oriented Design Part 2
CMSC 202 Interfaces.
Presentation transcript:

Topic 5a – Interfaces Friends share all things. CISC370/Object Oriented Programming with Java

CISC370 – Object Oriented Programming with Java / © 2003 J. Six Use and Distribution Notice Possession of any of these files implies understanding and agreement to this policy. The slides are provided for the use of students enrolled in Jeff Six's Object Oriented Programming with Java class (CISC 370) at the University of Delaware. They are the creation of Mr. Six and he reserves all rights as to the slides. These slides are not to be modified or redistributed in any way. All of these slides may only be used by students for the purpose of reviewing the material covered in lecture. Any other use, including but not limited to, the modification of any slides or the sale of any slides or material, in whole or in part, is expressly prohibited. Most of the material in these slides, including the examples, is derived from multiple textbooks. Credit is hereby given to the authors of these textbook for much of the content. This content is used here for the purpose of presenting this material in CISC 370, which uses, or has used, these textbooks.

CISC370 – Object Oriented Programming with Java / © 2003 J. Six Interfaces An interface is not a class, but a set of requirements for classes to conform to, if they want to implement the interface. The best way to explain this is with an example. We have seen before how to make an array of Employee object variables. In order to sort an array, we can call the Arrays.sort() method, a method of the Arrays class (used to perform functions on arrays).

CISC370 – Object Oriented Programming with Java / © 2003 J. Six Interfaces This method has the ability to sort arrays of any object class. In order to sort a group of things, there must be a way to decide if one object is less than, greater than, or equal to another object. So, there is one requirement on this, the class of objects we have an array of must be comparable. Comparable is an interface… public interface Comparable { int compareTo(Object other); }

CISC370 – Object Oriented Programming with Java / © 2003 J. Six Interfaces This is an interface. It simply states that any object that is Comparable (implements this interface) must have a method named compareTo(), which takes an Object as a parameter and returns an integer. public interface Comparable { int compareTo(Object other); }

CISC370 – Object Oriented Programming with Java / © 2003 J. Six Implementing an Interface To make a class implement an interface, two steps need to be done: In the class definition, you need to specify that the class will implement the specific interface. You provide a definition, in the class, for all of the methods specified in the interface. Notice that an interface is very similar to an abstract (or virtual) class in C++…it is just a set of requirements that any implementing class must have.

CISC370 – Object Oriented Programming with Java / © 2003 J. Six Implementing an Interface To make our Worker class Comparable… class Worker implements Comparable {... public int compareTo(Object otherObject) { Worker other = (Worker)otherObject; if (salary < other.salary)return –1; if (salary > other.salary)return 1; elsereturn 0; }

CISC370 – Object Oriented Programming with Java / © 2003 J. Six The Comparable Interface Looking up the Comparable interface in the API documentation reveals what the compareTo() method should do: Return a –1 if the first object is less than the second object (passed as a parameter) Return a 1 if the second object (passed as a parameter) is less than the first object) Return a 0 if the two objects are equal We can look this interface up in the docs because it’s a system interface – just like we can only look us Sun’s classes in the docs.

CISC370 – Object Oriented Programming with Java / © 2003 J. Six The Comparable Interface So, as written, what is the basis for deciding the <> relationship between Worker objects? class Worker implements Comparable {... public int compareTo(Object otherObject) { Worker other = (Worker)otherObject; if (salary < other.salary)return –1; if (salary > other.salary)return 1; elsereturn 0; }

CISC370 – Object Oriented Programming with Java / © 2003 J. Six Testing for Interfaces We have seen the instanceof operator to see if an object is an instance of a particular class. We can also use this operator to see if an object implements a particular interface… if (obj1 instanceof Comparable) { // runs if whatever class obj1 is an instance of // implements the Comparable interface } else {// runs if it does not implement the interface }

CISC370 – Object Oriented Programming with Java / © 2003 J. Six Testing for Interfaces So, in this manner we can find out if a particular object can be compared to another object in the manner described by the Comparable interface. if (obj1 instanceof Comparable) { // runs if whatever class obj1 is an instance of // implements the Comparable interface } else {// runs if it does not implement the interface }

CISC370 – Object Oriented Programming with Java / © 2003 J. Six Interface Object Variables We can use an object variable to refer to an object of any class that implements an interface. Using this object variable, we can only access methods that are present in the interface. For example… Object obj1; … if (obj1 instanceof Comparable) { Comparable comp1 = (Comparable)obj1; int res = comp1.compareTo(obj2); }

CISC370 – Object Oriented Programming with Java / © 2003 J. Six Interface Definitions Let’s look at the interface definition. Notice we do not specify the methods as public. For an interface, all methods are public by default. We do not need to explicitly specify them as public. public interface Comparable { int compareTo(Object other); }

CISC370 – Object Oriented Programming with Java / © 2003 J. Six Interface Definitions and Inheritance We can also extend interfaces, just like we can extend classes. This allows a chain of interfaces that go from general to more specific with each step. For example, let’s define an interface for a object which is capable of moving: public interface Movable { void move(double x, double y); }

CISC370 – Object Oriented Programming with Java / © 2003 J. Six Interface Definitions and Inheritance Here, we can see that a powered vehicle is also movable. In addition to all the requirements of being Movable, it must also have a MPG() method, which will return the miles per gallon this object gets. public interface Powered extends Movable { double miles_per_gallon(); }

CISC370 – Object Oriented Programming with Java / © 2003 J. Six Constants in an Interface If a variable is specified in an interface, it is automatically a constant, specifically a public static final variable. Thus, an object that implements this new Powered interface has a constant, SPEED_LIMIT defined. public interface Powered extends Movable { double miles_per_gallon(); double SPEED_LIMIT = 95; }

CISC370 – Object Oriented Programming with Java / © 2003 J. Six Interface Definitions and Inheritance Thus, the Powered interface extends the Movable interface. Bear in mind, that any object that implements the Powered interface must satisfy all the requirements of that interface as well as its superinterface. So, a Powered object must have an MPG() method and a move() method.

CISC370 – Object Oriented Programming with Java / © 2003 J. Six Multiple Interfaces It is possible to implement multiple interfaces. As an interface is simply a promise to implement certain methods, we can easily have more than one interface but fulfilling the requirements of each one. A class can implement as many interfaces as it likes. Note this is NOT possible with inheritance…a class can only extend (or inherit from) one class.