View
217
Download
2
Category
Tags:
Preview:
DESCRIPTION
Citation preview
Objectives
On completion of this period, you would be able to learn• Concepts of interfaces• Why interfaces are needed ?• Benefits of interfaces• Drawback of interfaces
1
Recap
2
•Inner classes
• Classes can defined as members a block of java code
• Member class
• Local class
• Anonymous class
• import statement
• Importing classes from other packages
Concepts of Interfaces
• What is an Interface ?• English meaning
• Something that connects two things
• Interface feature in Java realises the Object Oriented Axiom• Implementation
should be separated from Interface
3
•It is typically the programmer of ”Other code” who has designed the interface to be used in ”Your code”•“Other code” defines Interface•“Your code” implements the Interface
Fig. 29.1. Interface between two programs
Concepts of Interfaces Contd..
• What is an Interface ?
• A collection of method declarations and constant fields
• It defines a standard and public way of specifying the behavior of classes
• Defines a contract – to implement all the methods of interface
4
Concepts of Interfaces Contd..
• All methods of an interface are abstract methods
• Defines the signatures of a set of methods, without the body (implementation of the methods)
• A concrete class must implement the interface (all the abstract methods of the Interface)
5
Why Interface ? Reason #1
• To reveal an object's programming interface (functionality of the object) without revealing its implementation• This is the concept of encapsulation• The implementation can change without
affecting the caller of the interface
6
Why Interface ? Reason #2 • To have unrelated classes implement similar methods
(behaviors)
• One class is not a sub-class of another
• Example:
• Class Line and class MyInteger
• They are not related through inheritance
• But, both may want to implement comparison methods
• Check Is Greater (Object x, Object y)
• Check Is Less (Object x, Object y)
• Check Is Equal (Object x, Object y)
• Define Relation interface which has the three abstract methods above
7
Why Interface ? Reason #3
• To model multiple inheritance • A class can implement multiple interfaces while
it can extend only one class
8
Interfaces vs. Abstract Classes• All methods of an Interface are abstract methods
while some methods of an abstract class are abstract methods• Abstract methods of abstract class have
abstract modifier
• An interface can only define constants while abstract class can have fields
9
Interfaces vs. Abstract Classes Contd..• Interfaces have no direct inherited relationship
with any particular class, they are defined independently• Interfaces themselves have inheritance
relationship among themselves
• To model multiple inheritance • A class can implement multiple interfaces while
it can extend only one class
10
Interface As a Type
• When you define a new interface • you are defining a new reference type
• You can use interface names anywhere you can use any other type name
• If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface
11
Example : Interface As a Type• Let's say Person class implements
PersonInterface interface• You can do
• Person p1 = new Person();• PersonInterface pi1 = p1;• PersonInterface pi2 = new Person();
12
Interface vs. Class: Commonality
• Interfaces and classes are both types• This means that an interface can be used in
places where a class can be used• For example:
• // Recommended practice • PersonInterface pi = new Person(); • // Not recommended practice• Person pc = new Person();
• Interface and class can both have methods
13
Interface vs. Class: Differences
• The methods of an interface are all abstract methods• They cannot have bodies
• You cannot create an instance from an interface• For example:
• PersonInterface pi = new PersonInterface(); //ERROR!
• An interface can only be implemented by classes or extended by other interfaces
14
Benefits of Interfaces
• Provide multiple inheritance• Allow standard sets of methods to be used
across the class hierarchy• As interfaces are declared independently of
classes, they are unaffected by changes to specific classes
15
Drawback of Interfaces
• Interfaces cannot grow• Cannot add more methods to the existing
interface• One has to re-implement all the classes which
implements the interface when it grows
16
Summary
• In this class we have discussed– Basic concepts of interfaces– What is the need of interfaces– Comparison of interfaces with classes and
abstract classes– The benefits and drawbacks of interfaces
17
Frequently Asked Questions
1. What is an interface ?
2. List the benefits of interface
3. Compare abstract classes and interfaces
4. Compare a class and interface
18
Quiz
1.Which is NOT a part of an interface?1. Methods
2. Constructors
3. Constants
4. None
19
Quiz Contd..
2 .What is the commonality between interfaces
and abstract classes?
1. Both have methods
2. Both have abstract methods
3. Both have variables
4. No commonality
Quiz Contd..
3.Which of the statement is true about interface?1. It contains only methods
2. It contains both methods and constants
3. It contains both abstract methods and constants
4. None
21
Quiz Contd..
4. What is the commonality between interfaces and classes?
1. Both have methods
2. Both define new types
3. Both have variables
4. No commonality
Recommended