Interfaces Unit 08.

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

ITEC200 – Week03 Inheritance and Class Hierarchies.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
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.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Object-oriented Programming Concepts
Communication in Distributed Systems –Part 2
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
ISE 582: Web Technology for Industrial Engineers University of Southern California Department of Industrial and Systems Engineering Lecture 4 JAVA Cup.
Polymorphism & Interfaces
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Introduction to Generics
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Lecture 5:Interfaces and Abstract Classes
Chapter 13 Abstract Classes and Interfaces
Java Generics.
More About Java and Java How to Program By Deitel & Deitel.
Web Design & Development Lecture 9
Sections Inheritance and Abstract Classes
Interfaces.
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Chapter 11 Object-Oriented Design
Remote Method Invocation
A tree set Our SearchTree class is essentially a set.
Week 4 Object-Oriented Programming (1): Inheritance
UML Class Diagram: class Rectangle
Comp 249 Programming Methodology
Lecture 4: Interface Design
Java Programming Language
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Packages and Interfaces
Week 6 Object-Oriented Programming (2): Polymorphism
A tree set Our SearchTree class is essentially a set.
Interfaces.
Lecture 16 - Interfaces Professor Adams.
Chapter 12 Abstract Classes and Interfaces
Advanced Java Programming
Abstract Classes Page
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Object-Oriented Programming: Inheritance and Polymorphism
Chapter 14 Abstract Classes and Interfaces
OBJECT ORIENTED PROGRAMMING
Interfaces.
Defining Classes and Methods
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
OBJECT ORIENTED PROGRAMMING
Java Remote Method Invocation
Software Design Lecture : 38.
Presentation transcript:

Interfaces Unit 08

Unit Objectives To learn how And when to use interfaces To create your own interfaces

Interfaces An interface is simply a named list of method declarations Does not implement methods, only declares them An interface is not a class, although it looks remarkably like an abstract class Declared types in Java are either classes or interfaces Interfaces represent a promise of support services to the objects which implement the interface – a “contract” public interface File { public void open(String name); public void close(); } For example, a class that implements an interface MUST implement all its methods, which is not the case when extending abstract classes.

Protocols An interface defines a protocol (a set of methods) If a class implements a given interface, then that class implements that protocol An interface can impose a common protocol on a group of classes that are not related by inheritance In the chain of classes related by inheritance, the common protocol is imposed through subclassing Comparable compareTo() Date compareTo() Integer compareTo() String compareTo() Interfaces contain only method stubs - no functionality. Interfaces contain no variables. Superclasses can contain methods, with functionality, as well as variables.

Declaring an Interface Interface definition has two components Interface declaration Interface body The interface declaration declares various attributes about the interface Name Whether it extends other interfaces

Implementing Interface Methods Methods declared in an interface are implemented in the classes which support that interface public class TextFile implements File { public void open(String name) { // implementation of open method } public void close() { // implementation of close method

Syntax public class Directory extends Secure implements File { ... } In a class declaration, the naming of the superclass precedes any interfaces supported by the class: public class Directory extends Secure implements File { ... }

Multiple Interfaces public class Directory implements File, Secure { If a class implements multiple interfaces, the interfaces are all listed, separated by commas: public class Directory implements File, Secure { ... }

Implementing an Interface A class which implements an interface must implement every method defined by that interface If one or more methods is not implemented, Java will generate a compiler error Subclasses automatically implement all interfaces that their superclass implements Note: the clone() method performs the actual bitwise duplication of an object An "empty" interface is uncommon, but useful in this case. The clone() method is part of the Object class, which ordinarily would mean that all classes would inherit it. However, a built-in safeguard, CloneNotSupportedException, prevents any object from being cloned if it is not an instance of a "cloneable" class. Thus, Cloneable is a way of preventing just any and all objects from being cloned.

Cloneable Interface Some interfaces, such as Cloneable, do not contain any methods In the case of Cloneable, it alerts the JVM that objects of the implementing class can be copied via the clone() method These act as a signal to Java that a class observes a certain protocol that may not be expressible as a specific set of methods Only the objects that implement the Cloneable interface can be copied, or cloned.

Typing and Interfaces File r = new File(); // Error A variable's type can be an interface Stipulations Only objects whose class implements that interface can be bound to that variable Only messages defined by the interface can be used Interfaces cannot appear in a new expression File r = new File(); // Error File f = new TextFile(); // OK! In this example, f does not “know” it is a TextFile, unless it is explicitly cast as one.

Subinterfaces Interfaces can be extended Interface hierarchy is independent of the class hierarchy The interface which extends another interface inherits all of its method declarations interface File { public void open(String name); public void close(); } interface ReadableFile extends File { public byte readByte(); interface WritableFile extends File { public void writeByte(byte b); interface ReadWriteFile extends ReadableFile, WritableFile { public void seek(int position); When subclassing, use the word extends with only one superclass. With interfaces, extends can refer to multiple base interfaces when building a new interface. In other words: with classes, there is NO multiple inheritance in Java. with interfaces, there CAN be multiple inheritance.

Using Interfaces Using interfaces allows Cross-hierarchy polymorphism Access to methods in separate class trees Substitution of an object for another object which is not related via the class hierarchy Classes that implement the same interface understand the same messages Regardless of their location in the class hierarchy One of the differences between interfaces and abstract classes is that the use of abstract classes cannot provide access to methods in separate class trees.

More Advantages of Using Interfaces Allows more control over how objects are used The programmer can define a method's parameters as interfaces This restricts the use of those parameters The programmer knows which message the objects will respond to Improves reusability of code

Naming Conventions for Interfaces Make "able" interfaces Cloneable, Serializable, ... Name interfaces using proper nouns and provide "Impl" implementation of your interfaces Bank, BankImpl, BankAccount, BankAccountImpl With this convention, the interface typically contains a definition for all (or most) of the implementation class' public methods Prefix interface names with "I" and use proper nouns for your classes IBank, Bank, IBankAccount These naming conventions are suggested, and not intended to be enforced simultaneously.

Distributed Programming Java's Remote Method Invocation (RMI) makes extensive use of Interfaces The idea is to make the distinction between local and remote objects transparent If you declare all variables to be of the interface type, then you don't have to care where the object is located! Web Services takes advantage of a similar paradigm LOCAL REMOTE client service stub skeleton The stub, service and skeleton inherit from the same interface, but only the service implements that interface with business logic. The client can invoke the same methods against the stub that it would against the service if it were locally available, but the stub methods only exist to take the parameters from the client and invoke the methods remotely. Note that the arrows in the diagrams do not refer to class instantiation, extension or implementation, but rather to apparent and actual access. That is, the client behaves as if it were accessing the service directly, but it is in fact accessing the stub.

What You Have Learned How to define an interface How classes implement interfaces Interfaces allow developers to take advantage of cross-hierarchy polymorphism Interfaces give developers more control over how their objects can be used