24
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 class that implements the interface The syntax for defining an interface is similar to that of defining a class Except the word interface is used in place of class public interface Person An interface specifies a set of methods that any class that implements the interface must have It contains method headings and constant definitions only

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

Embed Size (px)

Citation preview

Page 1: 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

• 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 class that implements the

interface

• The syntax for defining an interface is similar to that of defining a class– Except the word interface is used in place of class– public interface Person

• An interface specifies a set of methods that any class that implements the interface must have– It contains method headings and constant definitions only– It contains no instance variables nor any complete method definitions

Page 2: 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

2

Interfaces

• An interface serves a function similar to a base class, though it is not a base class

• A class that contains only abstract methods and/or named constants

• Some languages allow one class to be derived from two or more different base classes

• This multiple inheritance is not allowed in Java• Instead, Java's way of approximating multiple inheritance is

through interfaces• To be able to handle a variety of events, Java allows a class

to implement more than one interface

Page 3: 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

• Implementing an interface is like signing a contract• Interface is typically used when disparate or

unrelated classes share common methods and constants

• Allows objects to be processed polymorphically• Interface is used in place of an abstract class when

there is no default implementation to inherit, no fields or default method implementation

Page 4: 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

public interface Payable{ public double getPaymentAmount(); } // end interface Person

Page 5: 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

• An interface and all of its method headings should be declared public

• They cannot be given private, protected• Interfaces are public and abstract by default• When a class implements an interface, it must make all the

methods in the interface public• Because an interface is a type, a method may be written

with a parameter of an interface type• That parameter will accept as an argument any class that

implements the interface

Page 6: 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

To implement an interface:

• a concrete class must do two things:1.It must include the phrase

implements Interface_Name at the start of the class definitionpublic class Invoice implements Payable

– If more than one interface is implemented, each is listed, separated by commas

2. It must implement all the method headings listed in the definition(s) of the interface(s)

Page 7: 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

Implementations of an Interface

public class Invoice implements Payable{ private String partNumber; private String partDescription; …… …… public double getPaymentAmount() { return (getQuantity() * getPricePerItem(); } }

Page 8: 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

Abstract Classes Implementing Interfaces

• Abstract classes may implement one or more interfaces– Any method headings given in the interface that

are not given definitions are made into abstract methods

• A concrete class must give definitions for all the method headings given in the abstract class and the interface

Page 9: 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

Abstract Classes Implementing Interfaces

<<interface>>Payable

Person

Invoice Employee

Salaried Employee

Page 10: 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

Payable & Person Class implementation

// Payable interface declarationpublic interface Payable { double getPaymentAmount();}// Person classpublic class Person { protected String address; public Person (String ad) { address = new String (ad); }} // end Person class

Page 11: 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

Derived Interfaces (Extending an Interface)

• Like classes, an interface may be derived from a base interface– This is called extending the interface– The derived interface must include the phrase

extends BaseInterfaceName• •A concrete class that implements a derived interface

must have definitions for any methods in the derived interface as well as any methods in the base interface

public interface X extends Y

Page 12: 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

• Inheritance and interfaces are both is-a relationships

• Allows polymorphic use of arrays• Allows polymorphic use of methods

Page 13: 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

Defined Constants in Interfaces

• An interface can contain defined constants in addition to or instead of method headings– Any variables defined in an interface must be

public, static, and final– Because this is understood, Java allows these

modifiers to be omitted• Any class that implements the interface has

access to these defined constants

Page 14: 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

public Interface Constants{ int ONE = 1; int TWO = 2; int THREE = 3;}

Page 15: 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

Java Programming: Program Design Including Data Structures

15

Composition

• Another way to relate two classes• One or more members of a class are objects of

another class type• “has-a” relation between classes

– For example, “every person has a date of birth” • Containment• Mechanism by which the internal data of one

class includes an object of another class

Page 16: 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

public class Student{ private String name; private String id;

public void copyStudent(Student st) { name= st.name; id= st.id ; } // ...}

/** Section.java -*/public class Section{ private String sectionName; private int capacity; private int currentNbStudents; private Student[ ] stud; …. public void addStudent(Student s) { stud[currentNbStudents]=s; currentNbStudents++; } // ...}

Page 17: 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

public class Course{ private String courseName; private int nbSection; private Section[ ] sect; // ...}

public class Teacher{ private String teacherName; private String Id; private Section[3] sect; // ...}

Page 18: 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

public class Department{ private String departName; private Student[ ] stud; private Course[ ] csc; private Teacher[ ] teach; // ...}

Page 19: 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

Composition Example

• Class PersonalInfo• Includes id (string), name (class Person),

birthdate (class Date)

Page 20: 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

Java Programming: Program Design Including Data Structures

20

Composition Example (continued)

Page 21: 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

public class PersonalInfo{ private Person name; private Date bDay; private int personID; public PersonalInfo() { name = new Person(); bDay = new Date(); personID = 0; } public PersonalInfo(String first, String last, int month,int day, int year, int ID) { name = new Person(first,last); bDay = new Date(month,day,year); personID = ID; } public void setpersonalInfo(String first, String last, int month,int day, int year, int ID) { name.setName(first,last); bDay.setDate(month,day,year); personID = ID; } //Method to return the string containing personal information public String toString() { return ("Name: " + name.toString() + "\n" + "Date of birth: " + bDay.toString() + "\n" + "Personal ID: " + personID); }}

Page 22: 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

public class Point { private int x; private int y; public Point(int x, int y) { // constructor this.x = x; this.y = y; } public Point() { // no-arg constructor x = 0; y = 0; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public String toString() { return "(" + x + "," + y + ")"; } }

Page 23: 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

public class Line { Point begin; Point end; // begin and end Points public Line(int x1, int y1, int x2, int y2) { // constructor 1 begin = new Point(x1, y1); end = new Point(x2, y2); } public Line(Point begin, Point end) { // constructor 2 this.begin = begin; this.end = end; } public String toString() { return "Line from " + begin + " to " + end; } }

Page 24: 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

OOD and OOP

• Encapsulation – combine data and operations on the data in a single unit

• Inheritance – ability to create new objects from existing classes

• Polymorphism – ability to use the same expression to denote different operations

• Rule of Thumb: Use composition if possible, before considering inheritance