Interfaces An interface is like an extreme case of an abstract class – However, an interface is...

Post on 19-Jan-2016

216 views 0 download

Transcript of Interfaces An interface is like an extreme case of an abstract class – However, an interface is...

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

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

• 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

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

• 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

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)

Implementations of an Interface

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

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

Abstract Classes Implementing Interfaces

<<interface>>Payable

Person

Invoice Employee

Salaried Employee

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

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

• Inheritance and interfaces are both is-a relationships

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

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

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

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

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++; } // ...}

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; // ...}

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

Composition Example

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

birthdate (class Date)

Java Programming: Program Design Including Data Structures

20

Composition Example (continued)

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); }}

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 + ")"; } }

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; } }

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