SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
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.

More Related Content

What's hot (20)

PPT
inheritance
Mohit Patodia
 
PDF
Object Oriented Programming using JAVA Notes
Uzair Salman
 
PPT
Java inheritance
Arati Gadgil
 
PPTX
Inheritance in JAVA PPT
Pooja Jaiswal
 
PPTX
Inheritance ppt
Nivegeetha
 
PDF
itft-Inheritance in java
Atul Sehdev
 
PPTX
Inheritance in oops
Hirra Sultan
 
PDF
Inheritance In Java
Arnab Bhaumik
 
PPTX
Java inheritance
BHUVIJAYAVELU
 
PPT
Inheritance in java
Lovely Professional University
 
PPTX
Inheritance in Java
Elizabeth alexander
 
PPTX
Inheritance in java
Tech_MX
 
PPTX
Inheritance in java
yash jain
 
PPTX
java interface and packages
VINOTH R
 
PPT
Java interface
Arati Gadgil
 
PPT
Unit 3 Java
arnold 7490
 
PPTX
Inheritance and its types In Java
MD SALEEM QAISAR
 
PPTX
Inheritance and Polymorphism Java
M. Raihan
 
PPT
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
inheritance
Mohit Patodia
 
Object Oriented Programming using JAVA Notes
Uzair Salman
 
Java inheritance
Arati Gadgil
 
Inheritance in JAVA PPT
Pooja Jaiswal
 
Inheritance ppt
Nivegeetha
 
itft-Inheritance in java
Atul Sehdev
 
Inheritance in oops
Hirra Sultan
 
Inheritance In Java
Arnab Bhaumik
 
Java inheritance
BHUVIJAYAVELU
 
Inheritance in java
Lovely Professional University
 
Inheritance in Java
Elizabeth alexander
 
Inheritance in java
Tech_MX
 
Inheritance in java
yash jain
 
java interface and packages
VINOTH R
 
Java interface
Arati Gadgil
 
Unit 3 Java
arnold 7490
 
Inheritance and its types In Java
MD SALEEM QAISAR
 
Inheritance and Polymorphism Java
M. Raihan
 
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 

Similar to Interfaces in java (20)

PPTX
Interface in java
Kavitha713564
 
DOCX
Java interface
HoneyChintal
 
PDF
Advanced Programming _Abstract Classes vs Interfaces (Java)
Professor Lili Saghafi
 
PDF
Basic_Java_10.pdf
KumarUtsav24
 
PPTX
Lecture 18
talha ijaz
 
PDF
‏‏‏‏‏‏oop lecture 6_١٢٥٩٤٧taiz univercity.pdf
nabeehmohammedtaher
 
PPT
Java interfaces & abstract classes
Shreyans Pathak
 
PDF
Lecture 5 interface.pdf
AdilAijaz3
 
PPT
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
PPTX
Java 6.pptx
usmanusman720379
 
PPTX
13. interfaces
M H Buddhika Ariyaratne
 
PDF
javainterface
Arjun Shanka
 
PPTX
OOFeatures_revised-2.pptx
ssuser84e52e
 
PPTX
Object Oriented Programming - Polymorphism and Interfaces
Habtamu Wolde
 
PPTX
Interface in Java
Ducat India
 
PPT
Java Programming - Abstract Class and Interface
Oum Saokosal
 
PPT
Java interfaces
RanjithaM32
 
PPTX
what is differance between abstract class and interface ppt
manojsharma469262
 
PPTX
Java interfaces
Elizabeth alexander
 
Interface in java
Kavitha713564
 
Java interface
HoneyChintal
 
Advanced Programming _Abstract Classes vs Interfaces (Java)
Professor Lili Saghafi
 
Basic_Java_10.pdf
KumarUtsav24
 
Lecture 18
talha ijaz
 
‏‏‏‏‏‏oop lecture 6_١٢٥٩٤٧taiz univercity.pdf
nabeehmohammedtaher
 
Java interfaces & abstract classes
Shreyans Pathak
 
Lecture 5 interface.pdf
AdilAijaz3
 
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
Java 6.pptx
usmanusman720379
 
13. interfaces
M H Buddhika Ariyaratne
 
javainterface
Arjun Shanka
 
OOFeatures_revised-2.pptx
ssuser84e52e
 
Object Oriented Programming - Polymorphism and Interfaces
Habtamu Wolde
 
Interface in Java
Ducat India
 
Java Programming - Abstract Class and Interface
Oum Saokosal
 
Java interfaces
RanjithaM32
 
what is differance between abstract class and interface ppt
manojsharma469262
 
Java interfaces
Elizabeth alexander
 
Ad

More from TharuniDiddekunta (15)

PDF
String class
TharuniDiddekunta
 
PDF
Exception handling basic
TharuniDiddekunta
 
PDF
Creating your own exception
TharuniDiddekunta
 
PDF
Built in exceptions
TharuniDiddekunta
 
PDF
Packages access protection, importing packages
TharuniDiddekunta
 
PDF
Operators, control statements represented in java
TharuniDiddekunta
 
PDF
Arrays in java
TharuniDiddekunta
 
PPTX
Software Metrics (Testing)
TharuniDiddekunta
 
PPTX
unit 3 Design 1
TharuniDiddekunta
 
PPTX
Unit 4 testing
TharuniDiddekunta
 
PPTX
risk managment and quality
TharuniDiddekunta
 
PPTX
Design
TharuniDiddekunta
 
PPT
Network layer
TharuniDiddekunta
 
PPTX
Transport layer and Application layer
TharuniDiddekunta
 
PPT
Congection control and Internet working
TharuniDiddekunta
 
String class
TharuniDiddekunta
 
Exception handling basic
TharuniDiddekunta
 
Creating your own exception
TharuniDiddekunta
 
Built in exceptions
TharuniDiddekunta
 
Packages access protection, importing packages
TharuniDiddekunta
 
Operators, control statements represented in java
TharuniDiddekunta
 
Arrays in java
TharuniDiddekunta
 
Software Metrics (Testing)
TharuniDiddekunta
 
unit 3 Design 1
TharuniDiddekunta
 
Unit 4 testing
TharuniDiddekunta
 
risk managment and quality
TharuniDiddekunta
 
Network layer
TharuniDiddekunta
 
Transport layer and Application layer
TharuniDiddekunta
 
Congection control and Internet working
TharuniDiddekunta
 
Ad

Recently uploaded (20)

PPTX
Day2 B2 Best.pptx
helenjenefa1
 
PPTX
GitOps_Repo_Structure for begeinner(Scaffolindg)
DanialHabibi2
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PPTX
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
PPT
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PDF
6th International Conference on Machine Learning Techniques and Data Science ...
ijistjournal
 
PPTX
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Day2 B2 Best.pptx
helenjenefa1
 
GitOps_Repo_Structure for begeinner(Scaffolindg)
DanialHabibi2
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
MRRS Strength and Durability of Concrete
CivilMythili
 
Hashing Introduction , hash functions and techniques
sailajam21
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
6th International Conference on Machine Learning Techniques and Data Science ...
ijistjournal
 
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 

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.