Interfaces
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
476
On Slideshare
471
From Embeds
5
Number of Embeds
1

Actions

Shares
Downloads
10
Comments
0
Likes
0

Embeds 5

http://improvejava.blogspot.in 5

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. ObjectivesOn completion of this period, you would beable to learn• Concepts of interfaces• Why interfaces are needed ?• Benefits of interfaces• Drawback of interfaces 1
  • 2. Recap•Inner classes• Classes can defined as members a block of javacode• Member class• Local class• Anonymous class• import statement• Importing classes from other packages 2
  • 3. Concepts of Interfaces• What is an Interface ? • English meaning • Something that connects two things• Interface feature in Fig. 29.1. Interface between two programs Java realises the •It is typically the programmer of Object Oriented ”Other code” who has designed the interface to be used in ”Your Axiom code” • Implementation •“Other code” defines Interface should be separated •“Your code” implements the Interface from Interface 3
  • 4. 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
  • 5. 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
  • 6. Why Interface ? Reason #1• To reveal an objects 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
  • 7. 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
  • 8. Why Interface ? Reason #3• To model multiple inheritance • A class can implement multiple interfaces while it can extend only one class 8
  • 9. 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
  • 10. 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
  • 11. 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
  • 12. Example : Interface As a Type• Lets say Person class implements PersonInterface interface• You can do • Person p1 = new Person(); • PersonInterface pi1 = p1; • PersonInterface pi2 = new Person(); 12
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. Frequently Asked Questions1. What is an interface ?2. List the benefits of interface3. Compare abstract classes and interfaces4. Compare a class and interface 18
  • 19. Quiz1.Which is NOT a part of an interface? 1. Methods 2. Constructors 3. Constants 4. None 19
  • 20. 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
  • 21. 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
  • 22. 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