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

•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
                                                   2
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
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

More Related Content

PPTX
Lecture 18
PPTX
Abstract class and interface
DOC
DOCX
Master of Computer Application (MCA) – Semester 4 MC0078
PPTX
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
PPTX
C# Interface | Interfaces In C# | C# Interfaces Explained | C# Tutorial For B...
PPTX
Abstraction in java [abstract classes and Interfaces
PDF
Inheritance
Lecture 18
Abstract class and interface
Master of Computer Application (MCA) – Semester 4 MC0078
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
C# Interface | Interfaces In C# | C# Interfaces Explained | C# Tutorial For B...
Abstraction in java [abstract classes and Interfaces
Inheritance

What's hot (20)

PPTX
Interface in Java
DOC
Complete java&j2ee
DOCX
Java questions for viva
PDF
Design patterns
DOCX
Corejavainterviewquestions.doc
PPTX
Java interfaces
DOC
116824015 java-j2 ee
PPTX
Unusual C# - OOP
PPT
Design patterns ppt
PDF
Oops presentation java
PPT
Jedi slides 2.1 object-oriented concepts
PPTX
Understanding class, object & interface
PPTX
Abstract Class Presentation
PPTX
OOP in C#
PPT
Poo java
PPTX
.NET F# Abstract class interface
PPTX
Java Access Specifier
DOC
Mca 108
PPTX
WHAT IS ABSTRACTION IN JAVA
PPT
Top 10 Interview Questions For Java
Interface in Java
Complete java&j2ee
Java questions for viva
Design patterns
Corejavainterviewquestions.doc
Java interfaces
116824015 java-j2 ee
Unusual C# - OOP
Design patterns ppt
Oops presentation java
Jedi slides 2.1 object-oriented concepts
Understanding class, object & interface
Abstract Class Presentation
OOP in C#
Poo java
.NET F# Abstract class interface
Java Access Specifier
Mca 108
WHAT IS ABSTRACTION IN JAVA
Top 10 Interview Questions For Java
Ad

Viewers also liked (8)

PPT
Function of memory.4to5
PPT
Operating system.26
PPT
Multiple interfaces 9 cm604.31
PPT
Working with frames
PPT
9 cm604.42
PPTX
COMMONLY USED OPERATING SYSTEMS
PPTX
Need for operating systems 26(1)
PPTX
Driver
Function of memory.4to5
Operating system.26
Multiple interfaces 9 cm604.31
Working with frames
9 cm604.42
COMMONLY USED OPERATING SYSTEMS
Need for operating systems 26(1)
Driver
Ad

Similar to Interfaces (20)

PDF
javainterface
PPT
9 abstract interface
DOC
Interface Vs Abstact
PDF
‏‏‏‏‏‏oop lecture 6_١٢٥٩٤٧taiz univercity.pdf
PPTX
Object Oriented Principles
PDF
Advanced Programming _Abstract Classes vs Interfaces (Java)
PPT
Java interfaces
PPTX
13. interfaces
PDF
JAVA PROGRAMMING – Packages - Stream based I/O
PPT
Interfaces.ppt
PPT
Interfaces .ppt
PPTX
OOFeatures_revised-2.pptx
PDF
Interface
PPTX
abstract class and interface.Net
PPT
04 inheritance
PPTX
01. design pattern
PPT
Interface in java By Dheeraj Kumar Singh
PDF
Interfaces in java
PPTX
Interfaces c#
PPT
Java interface
javainterface
9 abstract interface
Interface Vs Abstact
‏‏‏‏‏‏oop lecture 6_١٢٥٩٤٧taiz univercity.pdf
Object Oriented Principles
Advanced Programming _Abstract Classes vs Interfaces (Java)
Java interfaces
13. interfaces
JAVA PROGRAMMING – Packages - Stream based I/O
Interfaces.ppt
Interfaces .ppt
OOFeatures_revised-2.pptx
Interface
abstract class and interface.Net
04 inheritance
01. design pattern
Interface in java By Dheeraj Kumar Singh
Interfaces in java
Interfaces c#
Java interface

More from myrajendra (20)

PPT
Fundamentals
PPT
Data type
PPTX
Hibernate example1
PPTX
Jdbc workflow
PPTX
2 jdbc drivers
PPTX
3 jdbc api
PPTX
4 jdbc step1
PPTX
Dao example
PPTX
Sessionex1
PPTX
Internal
PPTX
3. elements
PPTX
2. attributes
PPTX
1 introduction to html
PPTX
Headings
PPTX
Forms
PPT
PPTX
Views
PPTX
Views
PPTX
Views
PPT
Starting jdbc
Fundamentals
Data type
Hibernate example1
Jdbc workflow
2 jdbc drivers
3 jdbc api
4 jdbc step1
Dao example
Sessionex1
Internal
3. elements
2. attributes
1 introduction to html
Headings
Forms
Views
Views
Views
Starting jdbc

Interfaces

  • 1. 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
  • 2. Recap •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 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 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
  • 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 • Let's 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 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
  • 19. Quiz 1.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