Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.

Similar presentations


Presentation on theme: "Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer."— Presentation transcript:

1 Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

2 Single Inheritance Single Vs Multiple Inheritance Base_Class_1 Derived_Class_1Derived_Class_2Derived_Class_3

3 Multiple Inheritance : when a class inherits from two or more different base classes : Multiple inheritance can create significant complications because the implementations that are inherited could be conflicting Single Vs Multiple Inheritance Base_Class_1 Derived_Class_1Derived_Class_2Derived_Class_3 Base_Class_2

4 Student StudentEmployee Employee Person Difficulty with Multiple Inheritance String name getHours() How many name will be in StudentEmployee? Which version of getHours() will be used in StudentEmployee?

5 Multiple Inheritance in Java Multiple inheritance is not allowed for classes. Multiple inheritance is allowed only for interfaces. An interface can be seen as a class without any implementation. So Interfaces are Java ’ s way of approximating multiple inheritance. What is an interface? next slide …

6 Interfaces An interface is something 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 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 An interface and all of its method headings should be declared public They cannot be given private, protected, or package access

7 The Ordered Interface

8 Interfaces To implement an interface, a concrete class must do two things: It must include the phrase implements Interface_Name at the start of the class definition ( If more than one interface is implemented, each is listed, separated by commas) The class must implement all the method headings listed in the definition(s) of the interface(s)

9 Implementation of an Interface

10

11 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

12 An Abstract Class Implementing an Interface

13 Derived Interfaces 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

14 Extending an Interface

15 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

16 Pitfall: Inconsistent Interfaces 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 but different return types This is an error, the class will not compile

17 The end


Download ppt "Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer."

Similar presentations


Ads by Google