SlideShare a Scribd company logo
2
Most read
11
Most read
16
Most read
JAVA PROGRAMMING
INTERFACE IN JAVA
INTERFACE IN JAVA
 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.
rface in Java
a Interface also represents the IS-A relationship.
annot be instantiated just like the abstract class.
ce Java 8, we can have default and static methods in an interface.
ce Java 9, we can have private methods in an interface.
se Java interface?
are mainly three reasons to use interface.
used to achieve abstraction.
interface, we can support the functionality of multiple inheritance.
an be used to achieve loose coupling.
Interface in java
How to declare an interface?
An interface is declared by using the interface keyword.
 It provides total abstraction; means all the methods in an interface are
declared with the empty body, and all the fields are public, static and final
by default.
A class that implements an interface must implement all the methods
declared in the interface.
Syntax:
interface <interface_name>{
// declare constant fields
// declare methods that abstract
// by default.
}
 Interface can have default and static methods.
Internal addition by the compiler
The Java compiler adds public and abstract keywords before the
interface method.
Moreover, it adds public, static and final keywords before data
members.
In other words, Interface fields are public, static and final by default,
and the methods are public and abstract.
The relationship between classes and interfaces
A class extends another class, an interface extends another interface, but
a class implements an interface
Interface in java
Java Interface Example
In this example, the Printable interface has only one method, and its implementation is provided in the A6 class.
interface printable
{
void print();
}
class A6 implements printable
{
public void print()
{
System.out.println("Hello");
}
class A2
{
public static void main(String args[])
{
A6 obj = new A6();
obj.print();
}
}
OUTPUT
Hello
Java Interface Example: Drawable
 The Drawable interface has only one method.
Its implementation is provided by Rectangle and Circle classes.
 In a real scenario, an interface is defined by someone else, but its
implementation is provided by different implementation providers.
Moreover, it is used by someone else.
The implementation part is hidden by the user who uses the interface.
//Interface declaration: by first user
interface Drawable
{
void draw();
}
//Implementation: by second user
class Rectangle implements Drawable
{
public void draw()
{
System.out.println("drawing rectangle");
}
}
class Circle implements Drawable
{
public void draw(){System.out.println("drawing circle");}
}
//Using interface: by third user
class TestInterface1
{
public static void main(String args[])
{
Drawable d=new Circle();//In real scenario, object is provided by method e.g. getDrawable()
d.draw();
}
}
OUTPUT
drawing circle
Multiple inheritance in Java by interface
If a class implements multiple interfaces, or an interface extends multiple
interfaces, it is known as multiple inheritance.
Default Method in Interface
We can have method body in interface. But we need to make it default method.
interface Drawable
{
void draw();
default void msg()
{
System.out.println("default method");
}
}
class Rectangle implements Drawable
{
public void draw()
{
System.out.println("drawing rectangle");
}
}
class TestInterfaceDefault
{
public static void main(String args[])
{
Drawable d=new Rectangle();
d.draw();
d.msg();
}
}
OUTPUT
drawing rectangle
default method
Static Method in Interface:
We can have static method in interface.
interface Drawable
{
void draw();
static int cube(int x)
{
return x*x*x;
}
}
class Rectangle implements Drawable
{
public void draw()
{
System.out.println("drawing rectangle");
}
}
class TestInterfaceStatic
{
public static void main(String args[])
{
Drawable d=new Rectangle();
d.draw();
System.out.println(Drawable.cube(3));
}
}
OUTPUT:
drawing rectangle
27
Abstract class Interface
1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods. Since Java 8, it can
have default and static methods also.
2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and non-static
variables.
Interface has only static and final variables.
4) Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.
5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
6) An abstract class can extend another Java class and implement
multiple Java interfaces.
An interface can extend another Java interface only.
7) An abstract class can be extended using keyword "extends". An interface can be implemented using keyword "implements".
8) A Java abstract class can have class members like private, protected,
etc.
Members of a Java interface are public by default.
9)Example:
public abstract class Shape{
public abstract void draw();
}
Example:
public interface Drawable{
void draw();
}
Difference between abstract class and interface
Abstract class and interface both are used to achieve abstraction where we can declare the abstract methods. Abstract class and interface both can't be instantiated.

More Related Content

What's hot (20)

PPTX
virtual function
VENNILAV6
 
PPTX
Operator overloading
Ramish Suleman
 
PPTX
This keyword in java
Hitesh Kumar
 
PPTX
Interface in java
PhD Research Scholar
 
PPTX
INHERITANCE IN JAVA.pptx
NITHISG1
 
PPT
Inheritance C#
Raghuveer Guthikonda
 
PPTX
Constructor in java
Pavith Gunasekara
 
PPT
Final keyword in java
Lovely Professional University
 
PPT
Abstract class in java
Lovely Professional University
 
PPTX
Methods in java
chauhankapil
 
PPTX
classes and objects in C++
HalaiHansaika
 
PPTX
Constructor in java
Hitesh Kumar
 
PPTX
Vectors in Java
Abhilash Nair
 
PPTX
Type casting in java
Farooq Baloch
 
PPTX
Super keyword in java
Hitesh Kumar
 
PPTX
Interface in java ,multiple inheritance in java, interface implementation
HoneyChintal
 
PPT
Java access modifiers
Srinivas Reddy
 
PPTX
Control statements in java
Madishetty Prathibha
 
PDF
Java I/o streams
Hamid Ghorbani
 
PPTX
Data types in java
HarshitaAshwani
 
virtual function
VENNILAV6
 
Operator overloading
Ramish Suleman
 
This keyword in java
Hitesh Kumar
 
Interface in java
PhD Research Scholar
 
INHERITANCE IN JAVA.pptx
NITHISG1
 
Inheritance C#
Raghuveer Guthikonda
 
Constructor in java
Pavith Gunasekara
 
Final keyword in java
Lovely Professional University
 
Abstract class in java
Lovely Professional University
 
Methods in java
chauhankapil
 
classes and objects in C++
HalaiHansaika
 
Constructor in java
Hitesh Kumar
 
Vectors in Java
Abhilash Nair
 
Type casting in java
Farooq Baloch
 
Super keyword in java
Hitesh Kumar
 
Interface in java ,multiple inheritance in java, interface implementation
HoneyChintal
 
Java access modifiers
Srinivas Reddy
 
Control statements in java
Madishetty Prathibha
 
Java I/o streams
Hamid Ghorbani
 
Data types in java
HarshitaAshwani
 

Similar to Interface in java (20)

PDF
Basic_Java_10.pdf
KumarUtsav24
 
PDF
Exception handling and packages.pdf
Kp Sharma
 
PPTX
abstract,final,interface (1).pptx upload
dashpayal697
 
DOCX
Java interface
HoneyChintal
 
PDF
21UCAC31 Java Programming.pdf(MTNC)(BCA)
ssuser7f90ae
 
PPT
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
PPTX
OOP with Java - Abstract Classes and Interfaces
RatnaJava
 
PPT
oops with java modules i & ii.ppt
rani marri
 
PDF
Core Java Interface Concepts for BCA Studetns
Jainul Musani
 
PPTX
INTERFACES. with machine learning and data
dineshkesav07
 
PPTX
it is the quick gest about the interfaces in java
arunkumarg271
 
PPT
Java interface
Arati Gadgil
 
PPTX
OOFeatures_revised-2.pptx
ssuser84e52e
 
PPT
Interface in java
Lovely Professional University
 
PPTX
Lecture 8 abstract class and interface
manish kumar
 
PPTX
Java notes of Chapter 3 presentation slides
SuyashPatil72
 
PPTX
JAVA.pptx
SohamMathur1
 
PPT
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
PPTX
Lecture 18
talha ijaz
 
PPT
12.2 Abstract class and Interface.ppt
VISHNUSHANKARSINGH3
 
Basic_Java_10.pdf
KumarUtsav24
 
Exception handling and packages.pdf
Kp Sharma
 
abstract,final,interface (1).pptx upload
dashpayal697
 
Java interface
HoneyChintal
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
ssuser7f90ae
 
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
OOP with Java - Abstract Classes and Interfaces
RatnaJava
 
oops with java modules i & ii.ppt
rani marri
 
Core Java Interface Concepts for BCA Studetns
Jainul Musani
 
INTERFACES. with machine learning and data
dineshkesav07
 
it is the quick gest about the interfaces in java
arunkumarg271
 
Java interface
Arati Gadgil
 
OOFeatures_revised-2.pptx
ssuser84e52e
 
Lecture 8 abstract class and interface
manish kumar
 
Java notes of Chapter 3 presentation slides
SuyashPatil72
 
JAVA.pptx
SohamMathur1
 
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
Lecture 18
talha ijaz
 
12.2 Abstract class and Interface.ppt
VISHNUSHANKARSINGH3
 
Ad

More from Kavitha713564 (16)

PPTX
Python Virtual Machine concept- N.Kavitha.pptx
Kavitha713564
 
PPTX
Operators Concept in Python-N.Kavitha.pptx
Kavitha713564
 
PPTX
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
Kavitha713564
 
PPTX
The Java Server Page in Java Concept.pptx
Kavitha713564
 
PPTX
Programming in python in detail concept .pptx
Kavitha713564
 
PPTX
The Input Statement in Core Python .pptx
Kavitha713564
 
PPTX
The Datatypes Concept in Core Python.pptx
Kavitha713564
 
PPTX
Packages in java
Kavitha713564
 
PPTX
Exception handling in java
Kavitha713564
 
PPTX
Multithreading in java
Kavitha713564
 
DOCX
Methods in Java
Kavitha713564
 
PPTX
Applet in java new
Kavitha713564
 
PPTX
Basic of java
Kavitha713564
 
PPTX
Multithreading in java
Kavitha713564
 
PPTX
Input output files in java
Kavitha713564
 
PPTX
Arrays,string and vector
Kavitha713564
 
Python Virtual Machine concept- N.Kavitha.pptx
Kavitha713564
 
Operators Concept in Python-N.Kavitha.pptx
Kavitha713564
 
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
Kavitha713564
 
The Java Server Page in Java Concept.pptx
Kavitha713564
 
Programming in python in detail concept .pptx
Kavitha713564
 
The Input Statement in Core Python .pptx
Kavitha713564
 
The Datatypes Concept in Core Python.pptx
Kavitha713564
 
Packages in java
Kavitha713564
 
Exception handling in java
Kavitha713564
 
Multithreading in java
Kavitha713564
 
Methods in Java
Kavitha713564
 
Applet in java new
Kavitha713564
 
Basic of java
Kavitha713564
 
Multithreading in java
Kavitha713564
 
Input output files in java
Kavitha713564
 
Arrays,string and vector
Kavitha713564
 
Ad

Recently uploaded (20)

PPTX
ENGLISH LEARNING ACTIVITY SHE W5Q1.pptxY
CHERIEANNAPRILSULIT1
 
PPTX
Folding Off Hours in Gantt View in Odoo 18.2
Celine George
 
PPTX
Constitutional Design Civics Class 9.pptx
bikesh692
 
PPTX
FAMILY HEALTH NURSING CARE - UNIT 5 - CHN 1 - GNM 1ST YEAR.pptx
Priyanshu Anand
 
PPTX
WEEK5 ENGLISH.pptx Grade 2 MATATAG POWERPOINT QUARTER 1 WEEK 5
MelanieEdradanAlcant
 
PDF
Right to Information.pdf by Sapna Maurya XI D
Directorate of Education Delhi
 
PPTX
How to Manage Resupply Subcontracting in Odoo 18
Celine George
 
PDF
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
PPTX
Auditing and Assurance Meaning - Objectives - Types - Advantages & Disadvanta...
DevaRam6
 
PPTX
LEARNING ACTIVITY SHEET PPTXX ON ENGLISH
CHERIEANNAPRILSULIT1
 
PPTX
Mrs Mhondiwa Introduction to Algebra class
sabinaschimanga
 
PDF
FULL DOCUMENT: Read the full Deloitte and Touche audit report on the National...
Kweku Zurek
 
PPTX
ANORECTAL MALFORMATIONS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPT
DRUGS USED IN THERAPY OF SHOCK, Shock Therapy, Treatment or management of shock
Rajshri Ghogare
 
PDF
Stepwise procedure (Manually Submitted & Un Attended) Medical Devices Cases
MUHAMMAD SOHAIL
 
PDF
water conservation .pdf by Nandni Kumari XI C
Directorate of Education Delhi
 
PPTX
Room booking management - Meeting Room In Odoo 17
Celine George
 
PDF
Exploring-the-Investigative-World-of-Science.pdf/8th class curiosity/1st chap...
Sandeep Swamy
 
PPTX
10CLA Term 3 Week 4 Study Techniques.pptx
mansk2
 
PPTX
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 
ENGLISH LEARNING ACTIVITY SHE W5Q1.pptxY
CHERIEANNAPRILSULIT1
 
Folding Off Hours in Gantt View in Odoo 18.2
Celine George
 
Constitutional Design Civics Class 9.pptx
bikesh692
 
FAMILY HEALTH NURSING CARE - UNIT 5 - CHN 1 - GNM 1ST YEAR.pptx
Priyanshu Anand
 
WEEK5 ENGLISH.pptx Grade 2 MATATAG POWERPOINT QUARTER 1 WEEK 5
MelanieEdradanAlcant
 
Right to Information.pdf by Sapna Maurya XI D
Directorate of Education Delhi
 
How to Manage Resupply Subcontracting in Odoo 18
Celine George
 
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
Auditing and Assurance Meaning - Objectives - Types - Advantages & Disadvanta...
DevaRam6
 
LEARNING ACTIVITY SHEET PPTXX ON ENGLISH
CHERIEANNAPRILSULIT1
 
Mrs Mhondiwa Introduction to Algebra class
sabinaschimanga
 
FULL DOCUMENT: Read the full Deloitte and Touche audit report on the National...
Kweku Zurek
 
ANORECTAL MALFORMATIONS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
DRUGS USED IN THERAPY OF SHOCK, Shock Therapy, Treatment or management of shock
Rajshri Ghogare
 
Stepwise procedure (Manually Submitted & Un Attended) Medical Devices Cases
MUHAMMAD SOHAIL
 
water conservation .pdf by Nandni Kumari XI C
Directorate of Education Delhi
 
Room booking management - Meeting Room In Odoo 17
Celine George
 
Exploring-the-Investigative-World-of-Science.pdf/8th class curiosity/1st chap...
Sandeep Swamy
 
10CLA Term 3 Week 4 Study Techniques.pptx
mansk2
 
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 

Interface in java

  • 2. INTERFACE IN JAVA  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.
  • 3. rface in Java a Interface also represents the IS-A relationship. annot be instantiated just like the abstract class. ce Java 8, we can have default and static methods in an interface. ce Java 9, we can have private methods in an interface. se Java interface? are mainly three reasons to use interface. used to achieve abstraction. interface, we can support the functionality of multiple inheritance. an be used to achieve loose coupling.
  • 5. How to declare an interface? An interface is declared by using the interface keyword.  It provides total abstraction; means all the methods in an interface are declared with the empty body, and all the fields are public, static and final by default. A class that implements an interface must implement all the methods declared in the interface. Syntax: interface <interface_name>{ // declare constant fields // declare methods that abstract // by default. }
  • 6.  Interface can have default and static methods. Internal addition by the compiler The Java compiler adds public and abstract keywords before the interface method. Moreover, it adds public, static and final keywords before data members. In other words, Interface fields are public, static and final by default, and the methods are public and abstract.
  • 7. The relationship between classes and interfaces A class extends another class, an interface extends another interface, but a class implements an interface
  • 9. Java Interface Example In this example, the Printable interface has only one method, and its implementation is provided in the A6 class. interface printable { void print(); } class A6 implements printable { public void print() { System.out.println("Hello"); } class A2 { public static void main(String args[]) { A6 obj = new A6(); obj.print(); } } OUTPUT Hello
  • 10. Java Interface Example: Drawable  The Drawable interface has only one method. Its implementation is provided by Rectangle and Circle classes.  In a real scenario, an interface is defined by someone else, but its implementation is provided by different implementation providers. Moreover, it is used by someone else. The implementation part is hidden by the user who uses the interface.
  • 11. //Interface declaration: by first user interface Drawable { void draw(); } //Implementation: by second user class Rectangle implements Drawable { public void draw() { System.out.println("drawing rectangle"); } } class Circle implements Drawable { public void draw(){System.out.println("drawing circle");} } //Using interface: by third user class TestInterface1 { public static void main(String args[]) { Drawable d=new Circle();//In real scenario, object is provided by method e.g. getDrawable() d.draw(); } } OUTPUT drawing circle
  • 12. Multiple inheritance in Java by interface If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance.
  • 13. Default Method in Interface We can have method body in interface. But we need to make it default method. interface Drawable { void draw(); default void msg() { System.out.println("default method"); } } class Rectangle implements Drawable { public void draw() { System.out.println("drawing rectangle"); } } class TestInterfaceDefault { public static void main(String args[]) { Drawable d=new Rectangle(); d.draw(); d.msg(); } } OUTPUT drawing rectangle default method
  • 14. Static Method in Interface: We can have static method in interface. interface Drawable { void draw(); static int cube(int x) { return x*x*x; } } class Rectangle implements Drawable { public void draw() { System.out.println("drawing rectangle"); } } class TestInterfaceStatic { public static void main(String args[]) { Drawable d=new Rectangle(); d.draw(); System.out.println(Drawable.cube(3)); } }
  • 16. Abstract class Interface 1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods. Since Java 8, it can have default and static methods also. 2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance. 3) Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables. 4) Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class. 5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface. 6) An abstract class can extend another Java class and implement multiple Java interfaces. An interface can extend another Java interface only. 7) An abstract class can be extended using keyword "extends". An interface can be implemented using keyword "implements". 8) A Java abstract class can have class members like private, protected, etc. Members of a Java interface are public by default. 9)Example: public abstract class Shape{ public abstract void draw(); } Example: public interface Drawable{ void draw(); } Difference between abstract class and interface Abstract class and interface both are used to achieve abstraction where we can declare the abstract methods. Abstract class and interface both can't be instantiated.