Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using...

download Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces

of 12

  • date post

  • Category


  • view

  • download


Embed Size (px)

Transcript of Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using...

  • Slide 1
  • Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces Cannot Grow Abstract Classes Versus Interfaces
  • Slide 2
  • Unit 032 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.
  • Slide 3
  • Unit 033 What is an Interface? Contd 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
  • Slide 4
  • Unit 034 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.}
  • Slide 5
  • Unit 035 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 methods 10.public String getOfficeHours(){ 11.return officeHours(); 12.} 13.public Course[] getTeachingCourses(){ 14.return teachingCourses; 15.} 16.}
  • Slide 6
  • Unit 036 Implementing Interfaces Contd 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.
  • Slide 7
  • Unit 037 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.
  • Slide 8
  • Unit 038 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.
  • Slide 9
  • Unit 039 Conflicting Methods in Interfaces
  • Slide 10
  • Unit 0310 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.
  • Slide 11
  • Unit 0311 Abstract Classes Versus Interfaces InterfacesAbstract Classes Defines a set of methodsModels 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 methodsMay implement some or all of its methods A class can implement multiple interfaces A class can extend only one abstract class
  • Slide 12
  • Unit 0312 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.