Interfaces Describe what classes should do, without specifying how they should do it Not a class, but a set of requirements for classes that want to conform.

Slides:



Advertisements
Similar presentations
24-Aug-14 Abstract Classes and Interfaces. Java is “safer” than Python Python is very dynamic—classes and methods can be added, modified, and deleted.
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
DATA STRUCTURES Lecture: Interfaces Slides adapted from Prof. Steven Roehrig.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inner Classes in Java CMSC 432 Shon Vick. 2 Conceptual Structure package p 1 class A 1 class A 2... class A n... package n class A 1 class A 2... class.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
15-Jun-15 Abstract Classes and Interfaces. 2 Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Graphics Programming with Inheritance Template pattern (Chap 6, se SceneEditor)
Abstract Classes and Interfaces. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.
UML Class Diagram: class Rectangle
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Chapter 10 Classes Continued
GUI Event Handling Nithya Raman. What is an Event? GUI components communicate with the rest of the applications through events. The source of an event.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Dec Abstract Classes and Interfaces. Eclipse trick CTRL + D will remove lines Organization Bookmarks TODOs – marking something as //TODO allows.
Object Oriented Programming: Java Edition By: Samuel Robinson.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Abstraction ADTs, Information Hiding and Encapsulation.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
CS 151: Object-Oriented Design October 31 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Event Listeners ActionListener –button,list AdjustmentListener-scroll bar ComponentListener-when component hidden…. ContainerListener-comp added or removed.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Events and Event Handling
Programming in Java: lecture 7
Modern Programming Tools And Techniques-I
Advanced Programming in Java
Advanced Programming in Java
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Java Events. Java Events Important definitions Overridden method Class vs. abstract class vs. interface Event Handling Definition Main components Windows.
Abstract Classes and Interfaces
Interfaces.
About the Presentations
Section 11.1 Class Variables and Methods
Programming in Java Event Handling
GUI Event Handling Nithya Raman.
UML Class Diagram: class Rectangle
Object-Oriented Programming
Interface.
Java Programming Language
ITEC324 Principle of CS III
Interfaces.
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.
Advanced Java Programming
Advanced Programming in Java
Chapter 14 Abstract Classes and Interfaces
Abstract Classes and Interfaces
Abstract Classes and Interfaces
ITEC324 Principle of CS III
Abstract Data Types Abstraction is to distill a system to its most fundamental parts. Applying the abstraction paradigm to the design of data structures.
Chapter 6 Inheritance.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Interfaces Describe what classes should do, without specifying how they should do it Not a class, but a set of requirements for classes that want to conform to the interface

Views of an Interface The interface between an ADT and the rest of a system can be viewed from two sides: –From the ADT side, it specifies the features exported to the rest of the system. –For the rest of the system, it specifies the features imported from the ADT. These are subtly different (for reasons related to the theory of polymorphism).

Practical consequences A programming language should provide ways of describing both views of an interface Ideally the same mechanism should do both, but unless the language supports polymorphism properly, this is very difficult to arrange – Java does not achieve it.

What Java does provide a mechanism to define the import view, which it calls an interface restrict this (because it does not support polymorphism) handle the export views of ADTs in other ways.

Interfaces An interface is like a class, but... –it is declared with the word interface –it contains only methods, not variables –may define constants –the methods are all public, whether you say so or not –the methods are only declared, not defined

Implementations You extend a class, but you implement an interface A class can only extend one other class, but it can implement many interfaces The interfaces are comma-separated: class MyClass extends MySuperclass implements Iface1, Iface2 {...

Example interface interface Iterator { boolean hasNext( ); Object next( ); void remove( ); } Again, these methods are all automatically public

Example implementation class MyClass extends MySuperclass implements Iterator { public boolean hasNext( ) {... code... } public Object next ( ) {... code... } }

Complex Number Interface and Implementation Demo

Promises When you implement an interface, you promise to provide code for all the methods that the interface declared This means that your code guarantees certain behavior (or at least certain methods!) If you don't implement all the methods, you have an abstract class

Abstract Classes If a class is abstract, you have to say so: abstract class AlmostAnIterator implements Iterator {... You cannot create an instance (object) of an abstract class......but you can extend an abstract class (and hopefully fulfill the rest of your promises!)

Abstract Classes C an define variables and methods that the inheriting classes will have Cannot define their values, Cannot define a constructor. Abstract classes are not much used

Java Interfaces The Java interface mechanism extends this: it allows a form of multiple inheritance – ie a real class may implement several interfaces.

Limitations of Java Interfaces To make its interface mechanism work, Java imposes the following restrictions. 1.An interface cannot define any Java constants - ie public final variables, because they would have to belong to some object of the interface class; and an interface is a form of abstract class; and abstract classes cannot have objects belonging to them. (Can have a public static final constant)

Limitations of Java Interfaces 2.An interface is not allowed to define a (Java) constructor, because an interface is a form of abstract class; and an abstract class cannot define a constructor. 3.An interface cannot define any class (ie static) methods or variables, because Java maintains an object (of class Class) for every actual class static methods or variables are part of this class; but abstract classes or interfaces do not have such “class objects”.

Practical Consequences A Java interface is effectively useless for defining the “export view” of the class for some ADT Some object oriented languages provide a template mechanism for defining the export view of an ADT without these restrictions – a future version of Java may provide such a mechanism.

Java interfaces Java provides many interfaces as well as many classes All Listeners are defined as interfaces; you write their implementations An Adapter class is a convenience class that implements an interface, but all of the method bodies are empty

MouseInputListener interface MouseInputListener { void mouseClicked(MouseEvent e); void mouseDragged(MouseEvent e); void mouseEntered(MouseEvent e); void mouseExited(MouseEvent e); void mouseMoved(MouseEvent e); void mousePressed(MouseEvent e); void mouseReleased(MouseEvent e); }

MouseInputAdapter class MouseInputAdapter implements MouseInputListener { public void mouseClicked(MouseEvent e) { }; public void mouseDragged(MouseEvent e) { }; public void mouseEntered(MouseEvent e) { }; public void mouseExited(MouseEvent e) { }; public void mouseMoved(MouseEvent e) { }; public void mousePressed(MouseEvent e) { }; public void mouseReleased(MouseEvent e) { }; }

Why Adapter classes are convenient You can either: –class MyClass implements MouseInputListener {... define all seven methods... } Or: –class MyClass extends MouseInputAdapter {... define the methods you care about... }

The End