SlideShare a Scribd company logo
2
Most read
5
Most read
8
Most read
P R E S E N T E D B Y :
P R A J J A V A L R A O C H I N T A L
Interface in Java
What is an 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.
 It is used to achieve abstraction and multiple
inheritance in Java.
Why use Java interface?
 It is used to achieve abstraction.
 By interface, we can support the functionality of
multiple inheritance.
 It can be used to achieve loose coupling.
Multiple inheritance is not supported
through class in java, but it is possible by
an interface, why?
 multiple inheritance is not supported in the case of
class because of ambiguity.
 However, it is supported in case of an interface
because there is no ambiguity. It is because its
implementation is provided by the implementation
class.
How to declare an interface?
 An interface is declared by using the interface
keyword.
 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.
}
Working of compiler
 The Java compiler adds public and abstract
keywords before the interface method. Moreover, it
adds public, static and final keywords before data
members.
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.
Java Interface Example
//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
public class TestInterface1
{
public static void main(String args[])
{
Drawable d=new Circle();//In real
scenario, object is provided by
method e.g. getDrawable()
Drawable r=new Rectangle();
d.draw();
r.draw();
}
}
Multiple inheritance in Java by interface
 If a class implements multiple interfaces, or an
interface extends multiple interfaces, it is known as
multiple inheritance.
Multiple inheritance using interface
interface Printable
{
void print();
}
interface Showable
{
void show();
}
public class A7 implements
Printable,Showable
{
public void print()
{
System.out.println("Hello");
}
public void show()
{
System.out.println("Welcome");
}
public static void main(String
args[])
{
A7 obj = new A7();
obj.print();
obj.show();
}
}

More Related Content

PPT
ABSTRACT CLASSES AND INTERFACES.ppt
PPTX
Dynamic method dispatch
PPT
Java interfaces
PPTX
Polymorphism presentation in java
PPTX
Java abstract class & abstract methods
PPT
Java interfaces & abstract classes
PPS
Introduction to class in java
PPTX
Multi-threaded Programming in JAVA
ABSTRACT CLASSES AND INTERFACES.ppt
Dynamic method dispatch
Java interfaces
Polymorphism presentation in java
Java abstract class & abstract methods
Java interfaces & abstract classes
Introduction to class in java
Multi-threaded Programming in JAVA

What's hot (20)

PPTX
Interface in java
PPTX
Control statements in java
PPTX
Java program structure
PPTX
Inheritance in java
PPTX
PDF
C++ OOPS Concept
PDF
Methods in Java
PPT
Abstract class in java
PPTX
Access specifiers(modifiers) in java
PDF
Enumeration in Java Explained | Java Tutorial | Edureka
PPT
Thread model in java
PPTX
Packages in java
PDF
Java threads
PPTX
Type casting in java
PPTX
Control structures in java
PPT
Method overriding
PPTX
MULTI THREADING IN JAVA
PPTX
Operators in java
PPTX
Classes, objects in JAVA
PDF
Abstraction
Interface in java
Control statements in java
Java program structure
Inheritance in java
C++ OOPS Concept
Methods in Java
Abstract class in java
Access specifiers(modifiers) in java
Enumeration in Java Explained | Java Tutorial | Edureka
Thread model in java
Packages in java
Java threads
Type casting in java
Control structures in java
Method overriding
MULTI THREADING IN JAVA
Operators in java
Classes, objects in JAVA
Abstraction
Ad

Similar to Interface in java ,multiple inheritance in java, interface implementation (20)

PPTX
JAVA.pptx
DOCX
Java interface
PDF
Basic_Java_10.pdf
PPTX
Interface in java
PPT
PPT
Java interface
PPTX
INTERFACES. with machine learning and data
PPTX
it is the quick gest about the interfaces in java
PPTX
Object Oriented Programming - Polymorphism and Interfaces
PPTX
abstract,final,interface (1).pptx upload
PDF
Interfaces in java
PPTX
OOFeatures_revised-2.pptx
PPT
Interface in java By Dheeraj Kumar Singh
DOC
How would you implement multiple inheritance in java
PDF
Exception handling and packages.pdf
PPT
oops with java modules i & ii.ppt
PPTX
Interface
PDF
21UCAC31 Java Programming.pdf(MTNC)(BCA)
PPTX
Java Interface
PPTX
Java notes of Chapter 3 presentation slides
JAVA.pptx
Java interface
Basic_Java_10.pdf
Interface in java
Java interface
INTERFACES. with machine learning and data
it is the quick gest about the interfaces in java
Object Oriented Programming - Polymorphism and Interfaces
abstract,final,interface (1).pptx upload
Interfaces in java
OOFeatures_revised-2.pptx
Interface in java By Dheeraj Kumar Singh
How would you implement multiple inheritance in java
Exception handling and packages.pdf
oops with java modules i & ii.ppt
Interface
21UCAC31 Java Programming.pdf(MTNC)(BCA)
Java Interface
Java notes of Chapter 3 presentation slides
Ad

Recently uploaded (20)

PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Institutional Correction lecture only . . .
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
master seminar digital applications in india
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Classroom Observation Tools for Teachers
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
O5-L3 Freight Transport Ops (International) V1.pdf
Pre independence Education in Inndia.pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Institutional Correction lecture only . . .
Anesthesia in Laparoscopic Surgery in India
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
master seminar digital applications in india
2.FourierTransform-ShortQuestionswithAnswers.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
FourierSeries-QuestionsWithAnswers(Part-A).pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Complications of Minimal Access Surgery at WLH
01-Introduction-to-Information-Management.pdf
Final Presentation General Medicine 03-08-2024.pptx
Microbial diseases, their pathogenesis and prophylaxis
Classroom Observation Tools for Teachers
Supply Chain Operations Speaking Notes -ICLT Program
Abdominal Access Techniques with Prof. Dr. R K Mishra

Interface in java ,multiple inheritance in java, interface implementation

  • 1. P R E S E N T E D B Y : P R A J J A V A L R A O C H I N T A L Interface in Java
  • 2. What is an 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.  It is used to achieve abstraction and multiple inheritance in Java.
  • 3. Why use Java interface?  It is used to achieve abstraction.  By interface, we can support the functionality of multiple inheritance.  It can be used to achieve loose coupling.
  • 4. Multiple inheritance is not supported through class in java, but it is possible by an interface, why?  multiple inheritance is not supported in the case of class because of ambiguity.  However, it is supported in case of an interface because there is no ambiguity. It is because its implementation is provided by the implementation class.
  • 5. How to declare an interface?  An interface is declared by using the interface keyword.  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.
  • 6. Syntax: interface <interface_name> { // declare constant fields // declare methods that abstract // by default. }
  • 7. Working of compiler  The Java compiler adds public and abstract keywords before the interface method. Moreover, it adds public, static and final keywords before data members.
  • 8. 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.
  • 9. Java Interface Example //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 public class TestInterface1 { public static void main(String args[]) { Drawable d=new Circle();//In real scenario, object is provided by method e.g. getDrawable() Drawable r=new Rectangle(); d.draw(); r.draw(); } }
  • 10. Multiple inheritance in Java by interface  If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance.
  • 11. Multiple inheritance using interface interface Printable { void print(); } interface Showable { void show(); } public class A7 implements Printable,Showable { public void print() { System.out.println("Hello"); } public void show() { System.out.println("Welcome"); } public static void main(String args[]) { A7 obj = new A7(); obj.print(); obj.show(); } }