SlideShare uma empresa Scribd logo
1 de 4
Baixar para ler offline
3/30/2019 Interfaces: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/interfaces?module_item_id=21012870 1/4
Interfaces
Interface:
An interface in java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in
the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in
Java.
In other words, you can say that interfaces can have abstract methods and variables. It cannot have
a method body.
Java Interface also represents the IS-A relationship.
If a class implements an interface and does not provide method bodies for all functions specified in
the interface, then class must be declared abstract.
Syntax :
interface <interface_name>
{
// declare constant fields
// declare methods that abstract
// by default.
}
To declare an interface, use interface keyword. It is used to provide total abstraction. That means all the
methods in interface are declared with empty body and are public and all fields are public, static and final
by default. A class that implement interface must implement all the methods declared in the interface. To
implement interface use implements keyword.
Why do we use interface ?
It is used to achieve total abstraction.
Since java does not support multiple inheritance in case of class, but by using interface it can achieve
multiple inheritance .
It is also used to achieve loose coupling.
Interfaces are used to implement abstraction. So the question arises why use interfaces when we
have abstract classes?
The reason is, abstract classes may contain non-final variables, whereas variables in interface are
final, public and static.
The relationship between classes and interfaces:
As shown in the figure given below, a class extends another class, an interface extends another
interface, but a class implements an interface.
1111111111111.jpg
3/30/2019 Interfaces: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/interfaces?module_item_id=21012870 2/4
interface Animal
{
public void animalSound();
public void sleep();
}
class Pig implements Animal
{
public void animalSound()
{
System.out.println("The pig says: wee wee");
}
public void sleep()
{
System.out.println("Zzz");
}
}
class MyMainClass
{
public static void main(String[] args)
{
Pig myPig = new Pig();
myPig.animalSound();
myPig.sleep();
}
}
Multiple inheritance in Java by interface:
If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as
multiple inheritance.
2222222222222.jpg
interface FirstInterface
{
public void myMethod();
}
interface SecondInterface
3/30/2019 Interfaces: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/interfaces?module_item_id=21012870 3/4
{
public void myOtherMethod(); // interface method
}
class DemoClass implements FirstInterface, SecondInterface
{
public void myMethod()
{
System.out.println("Some text..");
}
public void myOtherMethod()
{
System.out.println("Some other text...");
}
}
class MyMainClass
{
public static void main(String[] args)
{
DemoClass myObj = new DemoClass();
myObj.myMethod();
myObj.myOtherMethod();
}
}
Important points about interface or summary of article:
We can’t create instance(interface can’t be instantiated) of interface but we can make reference of it
that refers to the Object of its implementing class.
A class can implement more than one interface.
An interface can extends another interface or interfaces (more than one interface) .
A class that implements interface must implements all the methods in interface.
All the methods are public and abstract. And all the fields are public, static, and final.
It is used to achieve multiple inheritance.
It is used to achieve loose coupling.
The Difference between interface and abstract class:
Sno interface abstract class
1 If we don’t" know anything about If we are talking about implementation but
3/30/2019 Interfaces: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/interfaces?module_item_id=21012870 4/4
implementation just we have
requirement specification then we go
for interface.
not completely (partial implementation)
then we should go for abstract class.
2
Every method present inside interface
is always public and abstract whether
we are declaring or not.
Every method present inside abstract
class need not be public and abstract.
3
We can't declare interface methods with
the modifiers private, protected, final,
static, synchronized, native, strictfp.
there are no restrictions on abstract
classes class method modifiers.
4
Every interface variable is always public
static final whether we are declaring or
not.
Every abstract class variable need not be
public static final.
5
Every interface variable is always public
static final we can't declare with the
following modifiers. Private, protected,
transient, volatile.
There are no restrictions on abstract class
variable modifiers.
6
For the interface variables compulsory
we should perform initialization at the
time of declaration otherwise we will get
compile time error.
It is not require performing initialization for
abstract class variables at the time of
declaration.
7
Inside interface we can't take static and
instance blocks.
Inside abstract class we can take both
static and instance blocks.
8
Inside interface we can't take
constructor.
Inside abstract class we can take
constructor.

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

inheritance
inheritanceinheritance
inheritance
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Java interface
Java interfaceJava interface
Java interface
 
Unit 3 Java
Unit 3 JavaUnit 3 Java
Unit 3 Java
 
Inheritance and its types In Java
Inheritance and its types In JavaInheritance and its types In Java
Inheritance and its types In Java
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
 
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
 

Semelhante a Interfaces in java

OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxOOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxssuser84e52e
 
Interface in Java
Interface in JavaInterface in Java
Interface in JavaDucat India
 
03_A-OOPs_Interfaces.ppt
03_A-OOPs_Interfaces.ppt03_A-OOPs_Interfaces.ppt
03_A-OOPs_Interfaces.pptJyothiAmpally
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examplesbindur87
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdfKp Sharma
 
What are abstract classes Dont interfaces also allow for polymo.pdf
What are abstract classes Dont interfaces also allow for polymo.pdfWhat are abstract classes Dont interfaces also allow for polymo.pdf
What are abstract classes Dont interfaces also allow for polymo.pdfarihantkitchenmart
 
What is an interface How is extending a class different from implem.pdf
What is an interface How is extending a class different from implem.pdfWhat is an interface How is extending a class different from implem.pdf
What is an interface How is extending a class different from implem.pdfarchitcreation
 
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdfch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdfbca23189c
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.pptrani marri
 
06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programmingdeffa5
 
Master of Computer Application (MCA) – Semester 4 MC0078
Master of Computer Application (MCA) – Semester 4  MC0078Master of Computer Application (MCA) – Semester 4  MC0078
Master of Computer Application (MCA) – Semester 4 MC0078Aravind NC
 
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
 
Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)Professor Lili Saghafi
 

Semelhante a Interfaces in java (20)

Interface
InterfaceInterface
Interface
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxOOFeatures_revised-2.pptx
OOFeatures_revised-2.pptx
 
Interface in Java
Interface in JavaInterface in Java
Interface in Java
 
Java interface
Java interface Java interface
Java interface
 
03_A-OOPs_Interfaces.ppt
03_A-OOPs_Interfaces.ppt03_A-OOPs_Interfaces.ppt
03_A-OOPs_Interfaces.ppt
 
Core java questions
Core java questionsCore java questions
Core java questions
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examples
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
 
What are abstract classes Dont interfaces also allow for polymo.pdf
What are abstract classes Dont interfaces also allow for polymo.pdfWhat are abstract classes Dont interfaces also allow for polymo.pdf
What are abstract classes Dont interfaces also allow for polymo.pdf
 
What is an interface How is extending a class different from implem.pdf
What is an interface How is extending a class different from implem.pdfWhat is an interface How is extending a class different from implem.pdf
What is an interface How is extending a class different from implem.pdf
 
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdfch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
 
Java 06
Java 06Java 06
Java 06
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.ppt
 
Interface &amp;packages
Interface &amp;packagesInterface &amp;packages
Interface &amp;packages
 
06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Master of Computer Application (MCA) – Semester 4 MC0078
Master of Computer Application (MCA) – Semester 4  MC0078Master of Computer Application (MCA) – Semester 4  MC0078
Master of Computer Application (MCA) – Semester 4 MC0078
 
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
 
Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)
 

Mais de TharuniDiddekunta

Mais de TharuniDiddekunta (15)

String class
String classString class
String class
 
Exception handling basic
Exception handling basicException handling basic
Exception handling basic
 
Creating your own exception
Creating your own exceptionCreating your own exception
Creating your own exception
 
Built in exceptions
Built in exceptions Built in exceptions
Built in exceptions
 
Packages access protection, importing packages
Packages   access protection, importing packagesPackages   access protection, importing packages
Packages access protection, importing packages
 
Operators, control statements represented in java
Operators, control statements  represented in javaOperators, control statements  represented in java
Operators, control statements represented in java
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Software Metrics (Testing)
Software Metrics (Testing)Software Metrics (Testing)
Software Metrics (Testing)
 
unit 3 Design 1
unit 3 Design 1unit 3 Design 1
unit 3 Design 1
 
Unit 4 testing
Unit 4 testingUnit 4 testing
Unit 4 testing
 
risk managment and quality
risk managment and qualityrisk managment and quality
risk managment and quality
 
Design
DesignDesign
Design
 
Network layer
Network layerNetwork layer
Network layer
 
Transport layer and Application layer
Transport layer and Application layerTransport layer and Application layer
Transport layer and Application layer
 
Congection control and Internet working
Congection control and Internet workingCongection control and Internet working
Congection control and Internet working
 

Último

Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniquesugginaramesh
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxPurva Nikam
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 

Último (20)

Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniques
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptx
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 

Interfaces in java

  • 1. 3/30/2019 Interfaces: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/interfaces?module_item_id=21012870 1/4 Interfaces Interface: An interface in java is a blueprint of a class. It has static constants and abstract methods. The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java. In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body. Java Interface also represents the IS-A relationship. If a class implements an interface and does not provide method bodies for all functions specified in the interface, then class must be declared abstract. Syntax : interface <interface_name> { // declare constant fields // declare methods that abstract // by default. } To declare an interface, use interface keyword. It is used to provide total abstraction. That means all the methods in interface are declared with empty body and are public and all fields are public, static and final by default. A class that implement interface must implement all the methods declared in the interface. To implement interface use implements keyword. Why do we use interface ? It is used to achieve total abstraction. Since java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance . It is also used to achieve loose coupling. Interfaces are used to implement abstraction. So the question arises why use interfaces when we have abstract classes? The reason is, abstract classes may contain non-final variables, whereas variables in interface are final, public and static. The relationship between classes and interfaces: As shown in the figure given below, a class extends another class, an interface extends another interface, but a class implements an interface. 1111111111111.jpg
  • 2. 3/30/2019 Interfaces: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/interfaces?module_item_id=21012870 2/4 interface Animal { public void animalSound(); public void sleep(); } class Pig implements Animal { public void animalSound() { System.out.println("The pig says: wee wee"); } public void sleep() { System.out.println("Zzz"); } } class MyMainClass { public static void main(String[] args) { Pig myPig = new Pig(); myPig.animalSound(); myPig.sleep(); } } Multiple inheritance in Java by interface: If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance. 2222222222222.jpg interface FirstInterface { public void myMethod(); } interface SecondInterface
  • 3. 3/30/2019 Interfaces: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/interfaces?module_item_id=21012870 3/4 { public void myOtherMethod(); // interface method } class DemoClass implements FirstInterface, SecondInterface { public void myMethod() { System.out.println("Some text.."); } public void myOtherMethod() { System.out.println("Some other text..."); } } class MyMainClass { public static void main(String[] args) { DemoClass myObj = new DemoClass(); myObj.myMethod(); myObj.myOtherMethod(); } } Important points about interface or summary of article: We can’t create instance(interface can’t be instantiated) of interface but we can make reference of it that refers to the Object of its implementing class. A class can implement more than one interface. An interface can extends another interface or interfaces (more than one interface) . A class that implements interface must implements all the methods in interface. All the methods are public and abstract. And all the fields are public, static, and final. It is used to achieve multiple inheritance. It is used to achieve loose coupling. The Difference between interface and abstract class: Sno interface abstract class 1 If we don’t" know anything about If we are talking about implementation but
  • 4. 3/30/2019 Interfaces: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/interfaces?module_item_id=21012870 4/4 implementation just we have requirement specification then we go for interface. not completely (partial implementation) then we should go for abstract class. 2 Every method present inside interface is always public and abstract whether we are declaring or not. Every method present inside abstract class need not be public and abstract. 3 We can't declare interface methods with the modifiers private, protected, final, static, synchronized, native, strictfp. there are no restrictions on abstract classes class method modifiers. 4 Every interface variable is always public static final whether we are declaring or not. Every abstract class variable need not be public static final. 5 Every interface variable is always public static final we can't declare with the following modifiers. Private, protected, transient, volatile. There are no restrictions on abstract class variable modifiers. 6 For the interface variables compulsory we should perform initialization at the time of declaration otherwise we will get compile time error. It is not require performing initialization for abstract class variables at the time of declaration. 7 Inside interface we can't take static and instance blocks. Inside abstract class we can take both static and instance blocks. 8 Inside interface we can't take constructor. Inside abstract class we can take constructor.