SlideShare une entreprise Scribd logo
1  sur  22
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

Contenu connexe

Tendances

Interface in Java
Interface in JavaInterface in Java
Interface in JavaDucat India
 
Complete java&j2ee
Complete java&j2eeComplete java&j2ee
Complete java&j2eeShiva Cse
 
Java questions for viva
Java questions for vivaJava questions for viva
Java questions for vivaVipul Naik
 
Corejavainterviewquestions.doc
Corejavainterviewquestions.docCorejavainterviewquestions.doc
Corejavainterviewquestions.docJoyce Thomas
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 eehomeworkping9
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns pptAman Jain
 
Oops presentation java
Oops presentation javaOops presentation java
Oops presentation javaJayasankarPR2
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsMaryo Manjaruni
 
Understanding class, object & interface
Understanding class, object & interfaceUnderstanding class, object & interface
Understanding class, object & interfaceMD. Shohag Mia
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentationtigerwarn
 
OOP in C#
OOP in C#OOP in C#
OOP in C#DevMix
 
.NET F# Abstract class interface
.NET F# Abstract class interface.NET F# Abstract class interface
.NET F# Abstract class interfaceDrRajeshreeKhande
 
Java Access Specifier
Java Access SpecifierJava Access Specifier
Java Access SpecifierDeeptiJava
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAsivasundari6
 
Top 10 Interview Questions For Java
Top 10 Interview Questions For JavaTop 10 Interview Questions For Java
Top 10 Interview Questions For JavaEME Technologies
 

Tendances (20)

Interface in Java
Interface in JavaInterface in Java
Interface in Java
 
Complete java&j2ee
Complete java&j2eeComplete java&j2ee
Complete java&j2ee
 
Java questions for viva
Java questions for vivaJava questions for viva
Java questions for viva
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Corejavainterviewquestions.doc
Corejavainterviewquestions.docCorejavainterviewquestions.doc
Corejavainterviewquestions.doc
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 ee
 
Unusual C# - OOP
Unusual C# - OOPUnusual C# - OOP
Unusual C# - OOP
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns ppt
 
Oops presentation java
Oops presentation javaOops presentation java
Oops presentation java
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
 
Understanding class, object & interface
Understanding class, object & interfaceUnderstanding class, object & interface
Understanding class, object & interface
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
OOP in C#
OOP in C#OOP in C#
OOP in C#
 
Poo java
Poo javaPoo java
Poo java
 
.NET F# Abstract class interface
.NET F# Abstract class interface.NET F# Abstract class interface
.NET F# Abstract class interface
 
Java Access Specifier
Java Access SpecifierJava Access Specifier
Java Access Specifier
 
Mca 108
Mca 108Mca 108
Mca 108
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
 
Top 10 Interview Questions For Java
Top 10 Interview Questions For JavaTop 10 Interview Questions For Java
Top 10 Interview Questions For Java
 

En vedette

Function of memory.4to5
Function of memory.4to5Function of memory.4to5
Function of memory.4to5myrajendra
 
Operating system.26
Operating system.26Operating system.26
Operating system.26myrajendra
 
Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31myrajendra
 
Working with frames
Working with framesWorking with frames
Working with framesmyrajendra
 
COMMONLY USED OPERATING SYSTEMS
COMMONLY USED OPERATING SYSTEMSCOMMONLY USED OPERATING SYSTEMS
COMMONLY USED OPERATING SYSTEMSPriya Patra
 
Need for operating systems 26(1)
Need for operating systems 26(1)Need for operating systems 26(1)
Need for operating systems 26(1)myrajendra
 

En vedette (8)

Function of memory.4to5
Function of memory.4to5Function of memory.4to5
Function of memory.4to5
 
Operating system.26
Operating system.26Operating system.26
Operating system.26
 
Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31
 
Working with frames
Working with framesWorking with frames
Working with frames
 
9 cm604.42
9 cm604.429 cm604.42
9 cm604.42
 
COMMONLY USED OPERATING SYSTEMS
COMMONLY USED OPERATING SYSTEMSCOMMONLY USED OPERATING SYSTEMS
COMMONLY USED OPERATING SYSTEMS
 
Need for operating systems 26(1)
Need for operating systems 26(1)Need for operating systems 26(1)
Need for operating systems 26(1)
 
Driver
DriverDriver
Driver
 

Similaire à Interfaces

Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphismmcollison
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in javaShiv Mehmi
 
CPP14 - Encapsulation
CPP14 - EncapsulationCPP14 - Encapsulation
CPP14 - EncapsulationMichael Heron
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oopcolleges
 
OOPS – General Understanding in .NET
OOPS – General Understanding in .NETOOPS – General Understanding in .NET
OOPS – General Understanding in .NETSabith Byari
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and InterfacesJamsher bhanbhro
 
EEE oops Vth semester viva questions with answer
EEE oops Vth semester viva questions with answerEEE oops Vth semester viva questions with answer
EEE oops Vth semester viva questions with answerJeba Moses
 
what is differance between abstract class and interface ppt
what is differance between abstract class and interface pptwhat is differance between abstract class and interface ppt
what is differance between abstract class and interface pptmanojsharma469262
 
Web Design & Development - Session 9
Web Design & Development - Session 9Web Design & Development - Session 9
Web Design & Development - Session 9Shahrzad Peyman
 
More oop in java
More oop in javaMore oop in java
More oop in javaSAGARDAVE29
 
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdfmarkbrianBautista
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singhdheeraj_cse
 

Similaire à Interfaces (20)

javainterface
javainterfacejavainterface
javainterface
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Java 6.pptx
Java 6.pptxJava 6.pptx
Java 6.pptx
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Java Interface
Java InterfaceJava Interface
Java Interface
 
CPP14 - Encapsulation
CPP14 - EncapsulationCPP14 - Encapsulation
CPP14 - Encapsulation
 
Interface
InterfaceInterface
Interface
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oop
 
OOPS – General Understanding in .NET
OOPS – General Understanding in .NETOOPS – General Understanding in .NET
OOPS – General Understanding in .NET
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and Interfaces
 
EEE oops Vth semester viva questions with answer
EEE oops Vth semester viva questions with answerEEE oops Vth semester viva questions with answer
EEE oops Vth semester viva questions with answer
 
Core java questions
Core java questionsCore java questions
Core java questions
 
what is differance between abstract class and interface ppt
what is differance between abstract class and interface pptwhat is differance between abstract class and interface ppt
what is differance between abstract class and interface ppt
 
Web Design & Development - Session 9
Web Design & Development - Session 9Web Design & Development - Session 9
Web Design & Development - Session 9
 
More oop in java
More oop in javaMore oop in java
More oop in java
 
2CPP19 - Summation
2CPP19 - Summation2CPP19 - Summation
2CPP19 - Summation
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
inheritance
inheritanceinheritance
inheritance
 
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
 

Plus de myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
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