Interfaces.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

CS18000: Problem Solving and Object-Oriented Programming.
ABSTRACT CLASSES AND INTERFACES. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
Interfaces.
Abstract Classes and Interfaces The objectives of this chapter are: To explore the concept of abstract classes To understand interfaces To understand the.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
Interfaces. Lecture Objectives To learn about interfaces To be able to convert between class and interface references To appreciate how interfaces can.
Interfaces. Lecture Objectives To learn about interfaces To be able to convert between class and interface references To appreciate how interfaces can.
Abstract Classes.
Multiple Choice Solutions True/False a c b e d   T F.
Polymorphism & Interfaces
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Inheritence Put classes into a hierarchy derive a new class based on an existing class with modifications or extensions. Avoiding duplication and redundancy.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.
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.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Interfaces and Polymorphism CS 162 (Summer 2009).
1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
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.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
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.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Modern Programming Tools And Techniques-I
Abstract classes and interfaces
Web Design & Development Lecture 9
A Concrete Presentation on Abstract Classes and Methods, Interfaces, and Polymorphism CSC 202.
Chapter 15 Abstract Classes and Interfaces
Sixth Lecture ArrayList Abstract Class and Interface
Lecture 12 Inheritance.
Interfaces.
EKT 472: Object Oriented Programming
Interface.
UML Class Diagram: class Rectangle
Interfaces.
Abstract classes and interfaces
Interface.
Introduction to Java Programming
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
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 ,
Advanced Java Programming
CSE 501N Fall ‘09 13: Interfaces and Multiple Representation
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Abstract classes and interfaces
Abstract Classes and Interfaces
Final and Abstract Classes
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
Abstract Classes and Interfaces
CSE 143 Lecture 21 Advanced List Implementation
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Interfaces

Interfaces An interface is a Java structure similar to a class an interface defines methods with signatures only (no body) public interface Plumber { public void clearDrain(); public double computeBill(); } An interface defines the essence of something

Interfaces (continued) Other classes can implement an interface public class Handyman extends Carpenter implements Plumber { implementation is a contract, indicating that the class will include the methods specified by the interface (real methods, with bodies) this creates the same kind of “is a” relationship as with superclasses: a Handyman is a Handyman a Handyman is a Carpenter a Handyman is a Plumber

Interfaces (continued) Interfaces are Java’s approach to multiple inheritance when extending a superclass, a class inherits the functionality (methods) of the superclass when implementing an interface, a class agrees to provide the functionality (methods) of the interface a class may only extend one superclass, but may implement many interfaces public class Handyman extends Carpenter implements Plumber, Electrician, Painter {…}

Iterator Interface Class ArrayList has the method: public Iterator iterator() Returns an iterator over the elements in this list in proper sequence. Iterator is an interface: public interface Iterator { public boolean hasNext(); // true if another object exists public Object next(); // returns next object public void remove(); // removes last object returned } What does it mean when the return type is an interface?

Iterator Interface Class ArrayList has the method: public Iterator iterator() Returns an iterator over the elements in this list in proper sequence. Iterator is an interface: public interface Iterator { public boolean hasNext(); // true if another object exists public Object next(); // returns next object public void remove(); // removes last object returned } What does it mean when the return type is an interface? It means that the method returns an Object of a class that implements that interface. (We don’t know, or care, what the class name is.). We can interact with the object using the interface methods.

Iterator Interface ArrayList list = new ArrayList(); list.add (new Student (“Wemba”, “CSS”, 3.7, 75)); … Iterator it = list.iterator(); while (it.hasNext()) { Student s = (Student)it.next(); System.out.println (s.getName() + “ “ + s.getGPA()); }

Interfaces as Data Types an interface can be used as a data type when declaring variables, just as a class type can: Iterator it; however, objects of an interface cannot be instantiated: Iterator it = new Iterator(); // incorrect! objects of a class which implements the interface can be assigned to such a variable: it = list.iterator(); // returned object implements Iterator

Polymorphism with Interfaces interface Speaker { void speak(); } class Dog implements Speaker { public void speak() { System.out.println (“Woof”); class Cat implements Speaker { System.out.println (“Meow”); class Cow implements Speaker { System.out.println (“Moo”); Speaker[] critters = new Speaker[3]; int k = 0; critters[k++] = new Cat(); critters[k++] = new Dog(); critters[k++] = new Cow(); for (int j=0; j<critters.length; j++) { critters[j].speak(); }

More About Interfaces interfaces may contain: methods public & abstract by default have no body (end with semi-colon) constants public, static, final by default

public interface ConversionFactors { double MM_PER_INCH = 25.4; double GRAMS_PER_OUNCE = 28.349523125; double GRAMS_PER_POUND = 453.5924; double WATTS_PER_HP = 745.7; double HP_PER_WATT = 1 / WATTS_PER_HP; } // don't have to say public, static, or final public interface Conversions double inchToMM(double inches); double ounceToGram(double ounces); double poundToGram(double pounds); double HPToWatt(double hp); double wattToHP(double watts); } // don't have to say public or abstract

public interface ConversionFactors { double MM_PER_INCH = 25.4; double GRAMS_PER_OUNCE = 28.349523125; double GRAMS_PER_POUND = 453.5924; double WATTS_PER_HP = 745.7; double HP_PER_WATT = 1 / WATTS_PER_HP; } public class MyClass implements ConversionFactors private double poundsWeight; public double getMetricWeight() return poundsWeight * GRAMS_PER_POUND; ...

public interface Conversions { double inchToMM(double inches); double ounceToGram(double ounces); double poundToGram(double pounds); double HPToWatt(double hp); double wattToHP(double watts); } public class MyClass implements Conversions, ConversionFactors public double inchToMM(double inches) return inches * MM_PER_INCH; ... // need to implement all methods, // or make class abstract

interface Alpha { void b(); void c(); } class Beta implements Alpha { void b() { System.out.println(“b() called”); void c() { System.out.println(“c() called”); public class InterfaceExample { public static void main(String[] args) { Alpha x = new Alpha(); Alpha y = new Beta(); y.b(); y.c();

interface Alpha { void b(); void c(); } class Beta implements Alpha { void b() { System.out.println(“b() called”); void c() { System.out.println(“c() called”); public class InterfaceExample { public static void main(String[] args) { Alpha x = new Alpha(); // no way Alpha y = new Beta(); // OK y.b(); y.c();