unit 031 interfaces what is an interface? interface declaration syntax implementing interfaces using...

12
03 Unit 1 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces Cannot Grow Abstract Classes Versus Interfaces

Post on 21-Dec-2015

246 views

Category:

Documents


2 download

TRANSCRIPT

Unit 03 1

Interfaces

• What is an Interface?

• Interface Declaration Syntax

• Implementing Interfaces

• Using Interfaces as Types

• Interfaces and Inheritance

• Interfaces Cannot Grow

• Abstract Classes Versus Interfaces

Unit 03 2

What is an Interface?

• We have seen that inheritance allows code reusability - a subclass inherits

the properties of its super class.

• Inheritance also allows type sharing - an instance of a subclass is of type its

class, its superclass, and any class up its class-hierarchy tree.

• But Java allows only single inheritance. So are there other ways of

achieving more of the above advantages?

• Regarding having more code reusability, the answer is NO.

• However, regarding having more type sharing, the answer is YES. It is

possible for an object to be of a type outside its inheritance tree - This is

what interfaces are about.

Unit 03 3

What is an Interface? –Cont’d

• Interfaces are used to realize some of the advantages of multiple inheritance, while avoiding its complexity.

• For example, both GraduateStudent and Faculty might implement the Instructor interface.

• Interfaces only declare methods that objects must have but with no implementation - all methods are abstract.

• Interfaces differ from abstract classes in that:

– They cannot have implemented methods

– No instance variables except constants.

– No constructors

• Interface names are often adjectives, ending in -able.

examples: Colorable, Rotatable, Comparable, Runnable

• Interfaces that specify roles can be named with nouns.

examples: Container, Mover, Instructor

Unit 03 4

Interface Declaration Syntax• Interfaces are declared similar to classes, except "interface" is used instead of "class".

1. public interface Instructor{2. int universityCode = 31261;3. String getOfficeHours();4. Course[] getTeachingCourses();5. }

• All methods in an interface are automatically public and abstract.

• All fields in an interface are automatically static and final.

• Thus, the above declaration is the same as the following:

1. public interface Instructor{

2. static final int universityCode = 31261;3. public abstract String getOfficeHours();4. public abstract Course[] getTeachingCourses();5. }

Unit 03 5

Implementing Interfaces• A class implements an interface similar to the way it extends a superclass but using " implements" instead of "extends".

• A class can extend another class and at the same time implement one or more interfaces.

1. public class GraduateStudent extends Student implements Instructor{2. private String thesisTitle;3. private String officeHours;4. private Course[] teachingCourses;5. public GraduateStudent(int id, String name, double gpa, String title){6. super(id, name, workLoad);7. thesisTitle = title;8. }9. // ... other methods10. public String getOfficeHours(){11. return officeHours();12. }13. public Course[] getTeachingCourses(){14. return teachingCourses;15. }16. }

Unit 03 6

Implementing Interfaces – Cont’d

• Note: although methods of an interface are automatically public, they must be specified as

such in the implementing class.

• If a class implements more than one interface, the interface names are separated by commas.

• What happens if a class does not define all the methods of an interface it claims to

implement?

• Same thing that happens if an abstract method of a superclass is not defined - the

implementing class is abstract.

• If a super class implements an interface, then all its subclasses are considered to

implement it too.

Unit 03 7

Using Interfaces as Types• Interfaces can be used as types of variables and parameters.

• For example, an object of GraduateStudent can be of type GraduateStudent, Student, Object or Instructor.

• Similarly, if Faculty is defined as shown below, then its instance can be of type Faculty, MonthlyEmployee, Employee, Instructor, Comparable, or Object.

• Thus, a method with the following header can accept both an object of Faculty or that of GraduateStudent.

Unit 03 8

Interfaces and Inheritance• Like classes, interfaces can extend other interfaces

• For example, if we have a Container interface, we can extend it to have SearchableContainer interface.

1. public interface Container{2. void add(Object o);3. int getCount();4. boolean isEmpty();5. boolean isFull();6. void purge();7. }

1. public interface SearchableContainer extends Container{2. boolean isMember(Object object);3. void remove(Object object);4. }

• Unlike classes, interfaces can extend any number of other interfaces

• This is because interfaces merely declare methods, but do not implement, so no collision can occur as in multiple inheritance.

Unit 03 9

Conflicting Methods in Interfaces

Unit 03 10

Interfaces Cannot Grow

• It important to think through very well about the methods (and their signatures) when designing an interface.

• This is because Interfaces are like foundation to a building. To make changes, the whole building must be destroyed.

• If you add a single method to an interface or change the signature of a method, then all classes that implement the old Interface will break because they don't implement the interface anymore!

• The only solution in this case is to extend the interface.

Unit 03 11

Abstract Classes Versus Interfaces

Interfaces Abstract Classes

Defines a set of methods Models an object with properties and methods

Factors out common methods of potentially dissimilar objects

Factors out common properties and methods of similar objects

Does not implement its methods May implement some or all of its methods

A class can implement multiple interfaces

A class can extend only one abstract class

Unit 03 12

Exercises

Write each of the following:1. an interface, Instructor, which has two methods: String getOfficeHours() and

Course[ ] getTeachingCourses().2. a class, Course, with instance variables: courseCode, creditHours, and title; and

appropriate accessor, mutator and toString methods.3. a class Faculty that extends MonthlyEmpolyee and implements the Instructor

interface. It has two additional instance variables: String officeHourse and Course[] teachingCourses.

4. classes, Student and GraduateStudents as described in page 3 of the inheritance session.

5. a class, ReaseachAssistant, that extends GraduateStudent and implements the Instructor interface

6. A test class, TestInstructors, that has the following methods: void PrintTeachingDetails(Instructor i): that prints the office hours and teaching courses for i; and a main method, that creates instances of Faculty and ReasearchAssistant and prints their teaching details.