Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.

Similar presentations


Presentation on theme: "Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part."— Presentation transcript:

1 Interface & Abstract Class

2 Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part. Methods be implemented in the subclasses that use them.

3 Multiple Inheritance Using Interface The interface construct in Java is used with single inheritance to provide some form of multiple inheritance.

4 Multiple Inheritance Using Interface In order that a sales manager has the ability to manage, we add to the SalesManager class, appropriate behavior in an interface which is then inherited by the SalesManager class. We shall call that interface Manage as follows:

5 Multiple Inheritance Using Interface

6 In using the Manage interface, the subclass SalesManager must implement the abstract methods of the interface. This is reflected in the class declaration of SalesManager:

7 Multiple Inheritance Using Interface A sales manager is basically a salesperson with an additional behavior to authorize payments (via the authorize() method).

8 Attributes in an Interface Data attributes declared in an interface construct are always static and final. They are static as there can only be one copy of the data available and final since they are not modifiable. By declaring data attributes in an interface, constant declarations for use in methods is possible. Constants are names for values with a specific meaning.

9 Attributes in an Interface As all data attributes are implicitly declared as static and final in an interface definition, these keywords need not precede their declaration:

10 Methods in an Interface All methods in an interface are abstract methods and any class that uses the interface must provide an implementation for them. An interface does not have to explicitly declare its methods abstract using the keyword abstract. Interface methods are always public, and the access modifier public keyword is not required since it is implied in the interface declaration. In contrast with data attributes in an interface, methods may not be static since static methods, being class specific, are never abstract.

11 Abstract Class & Interface A class implementing an interface must implement all the abstract methods declared in an interface. The class is considered as an abstract class and must be declared using the abstract keyword.

12 Abstract Class & Interface

13 Abstract ClassInterface May have some methods declared abstract. Can only have abstract methods. May have protected properties and static methods. Can only have public methods with no implementation. May have final and nonfinal data attributes. Limited to only constants.

14 Abstract Class & Interface An abstract class can enhance inheritance as some or all parts of the class can be implemented and inherited by subclasses. An interface, on the other hand, is generally used for achieving multiple inheritance in Java. An abstract class cannot be used to instantiate objects since it may contain parts that are not implemented.

15 Extending Interface A subclass of a class that implements an interface also inherit the methods of the interface.

16 Extending Interface

17 Limitation of interface for Multiple Inheritance Although the interface feature in Java provides an alternative solution to achieving multiple inheritance in class hierarchies, it has its limitations: a. An interface does not provide a natural means of realizing multiple inheritance in situations where there is no inheritance conflict. b. While the principal reason for inheritance is code reusability, the interfacefacility does not encourage code reuse since duplication of code is inevitable.

18


Download ppt "Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part."

Similar presentations


Ads by Google