1 / 13

Understanding Interfaces in Java

Learn about interfaces in Java and how they can be used to achieve code reusability and type sharing. Explore interface declaration syntax, implementing interfaces, using interfaces as types, and the relationship between interfaces and inheritance.

abyers
Download Presentation

Understanding Interfaces in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 the type of 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 whatinterfacesare 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 Facultymight implement the Instructorinterface. • 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". • public interface Instructor{ • int universityCode = 31261; • String getOfficeHours(); • Course[] getTeachingCourses(); • } • All methods in an interface are automatically public and abstract. You cannot declare the methods of an interface as private or protected • All fields in an interface are automatically public, static and final (constants). Any class that implements the interface has access to all these constants. • The above declaration is the same as the following: • public interface Instructor{ • publicstatic final int universityCode = 31261; • public abstract String getOfficeHours(); • public abstract Course[] getTeachingCourses(); • } 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. • public class GraduateStudent extends Student implements Instructor{ • private String thesisTitle; • private String officeHours; • private Course[] teachingCourses; • public GraduateStudent(int id, String name, double gpa, String title){ • super(id, name, workLoad); • thesisTitle = title; • } • // ... other methods • public String getOfficeHours(){ • return officeHours(); • } • public Course[] getTeachingCourses(){ • return teachingCourses; • } • } 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 should be declared as abstract. • If a super class implements an interface, then all its subclasses are considered to have implemented it too. Unit 03

  7. Using Interfaces as Types • Interfaces can be used as types of variables and parameters. • For example, an object ofGraduateStudent 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 Containerinterface, we can extend it to have SearchableContainer interface. • public interface Container{ • void add(Object o); • int getCount(); • boolean isEmpty(); • boolean isFull(); • } • public interface SearchableContainerextendsContainer{ • boolean isMember(Object object); • void remove(Object object); • } • Unlike classes, interfaces can extend any number of other interfaces Unit 03

  9. Conflicting Methods in Interfaces Unit 03

  10. Inconsistent Interface • When a class implements two interfaces: • One type of inconsistency will occur if the interfaces have constants with the same name, but with different values • In this case, the class will be compiled correctly but when any of the constants is accessed via a reference of the class type, a compilation error will occur. If the reference is up-casted to one of the interfaces, the constant from that interface will be used without any error • Another type of inconsistency will occur if the interfaces contain methods with the same name and signatures but different return types • This is an error, the class will not compile.. Unit 03

  11. Modifying Interfaces • It is 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

  12. Abstract Classes Versus Interfaces • An abstract class may implement one or more interfaces Unit 03

  13. Exercises Write each of the following: • an interface, Instructor, which has two methods: String getOfficeHours() and Course[ ] getTeachingCourses(). • a class, Course, with instance variables: courseCode, creditHours, and title; and appropriate accessor, mutator and toString methods. • a class Faculty that extends MonthlyEmpolyee and implements the Instructor interface. It has two additional instance variables: String officeHourse and Course[] teachingCourses. • classes, Student and GraduateStudents as described in page 3 of the inheritance session. • a class, ReaseachAssistant, that extends GraduateStudent and implements the Instructor interface • 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. Unit 03

More Related