SlideShare a Scribd company logo
Interface in Java
 An interface is like a class. It has static constants and abstract methods.
 Interfaces are declared using the interface keyword,
 method signature
 constant declarations (variable declarations that are declared to be
both static and final).
 An interface never contains method implementations (ie function
"bodies").
access interface name
{
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
// ...
return-type method-nameN(parameter-list);
type final-varnameN = value;
}
 Access – either public or not used.
 Not used – default is an answer.
 i/f is only available to other member of the package which it’s
declared.
 Methods declared have no bodies and end with a semicolon after the
parameter list(abstract methods). Each class that includes an interface
must implement all of the methods.
 Variables – declared implicitly final and static, meaning they cannot be
changed by the implementing class. They must also be initialized with
a constant value.
 All methods and variables are implicitly public if the interface, itself, is
declared as public.
Understanding relationship between classes and interfaces
Java interface
 To implement an interface
– include the implements clause in a class definition
access class classname [extends superclass] [implements interface
[,interface...]]
{
constant declarations ;
abstract method declarations;
}
 The methods that implement an interface must be declared public.
However, an interface is different from a class in several ways, including:
 You cannot instantiate an interface.
 An interface does not contain any constructors.
 All of the methods in an interface are abstract.
 An interface cannot contain instance fields. The only fields that can
appear in an interface must be declared both static and final.
 An interface is not extended by a class; it is implemented by a class.
 An interface can extend multiple interfaces.
interface shape
{
public String baseclass="shape";
public void Draw();
}
class circle implements shape
{
public void Draw()
{
System.out.println("Drawing Circle
here");
}}
public class inter
{
public static void main(String[] args)
{
circle s=new circle();
// shape s=new circle();
s.Draw();
} }
interface Printable
{
void print();
}
interface Showable
{
void show();
}
class A implements Printable,Showable
{
public void print()
{System.out.println("Hello");}
public void show()
{System.out.println("Welcome");
}
public static void main(String args[])
{
A obj = new A();
obj.print();
obj.show(); }}
 An interface can be declared as member of a class or another
interface – called nested interface.
 How can we define i/f inside a class and how can we access it.
class A
{
interface Message()
{
void msg();
}
}
Class test implements A.Message
{
Public void msg()
{
S.o.p(“Hello”);
}
Public static void main(String args[])
{
A.Message message=new test();
message.msg();
}
}
 Interfaces Can Be Extended:
 One interface can inherit another interface - extend.
 The syntax is the same as for inheriting classes.
// One interface an extend another.
interface A
{
void meth1();
void meth2();
}
// B now includes meth1() and meth2() -- it adds meth3().
interface B extends A
{
void meth3();
}
interface Printable
{
void print();
}
interface Showable extends Printable
{
void show();
}
class A implements Showable
{
public void print()
{System.out.println("Hello");}
public void show()
{System.out.println("Welcome");}
public static void main(String args[])
{
A obj=new A();
obj.print();
obj.show();
}}
Ad

More Related Content

What's hot (20)

Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA
 
Java Lambda Expressions.pptx
Java Lambda Expressions.pptxJava Lambda Expressions.pptx
Java Lambda Expressions.pptx
SameerAhmed593310
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
Lovely Professional University
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
KrutikaWankhade1
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
Spotle.ai
 
interface in c#
interface in c#interface in c#
interface in c#
Deepti Pillai
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
tigerwarn
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Madishetty Prathibha
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
Benj Del Mundo
 
Inheritance C#
Inheritance C#Inheritance C#
Inheritance C#
Raghuveer Guthikonda
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
Vishnu Suresh
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
Vinod Kumar
 
Java interface
Java interfaceJava interface
Java interface
Md. Tanvir Hossain
 
Abstraction java
Abstraction javaAbstraction java
Abstraction java
MahinImran
 
History Of JAVA
History Of JAVAHistory Of JAVA
History Of JAVA
ARSLANAHMED107
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
SIVASHANKARIRAJAN
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
 
this keyword in Java.pptx
this keyword in Java.pptxthis keyword in Java.pptx
this keyword in Java.pptx
ParvizMirzayev2
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
Anup Burange
 

Similar to Java interface (20)

Javainterface
Javainterface Javainterface
Javainterface
prakash GB
 
working with interfaces in java programming
working with interfaces in java programmingworking with interfaces in java programming
working with interfaces in java programming
Parameshwar Maddela
 
Interfaces
InterfacesInterfaces
Interfaces
RBIEBT,MOHALI
 
12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt
VISHNUSHANKARSINGH3
 
Basic_Java_10.pdf
Basic_Java_10.pdfBasic_Java_10.pdf
Basic_Java_10.pdf
KumarUtsav24
 
Interface
InterfaceInterface
Interface
Muthiah Abbhirami
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
manish kumar
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
ssuser7f90ae
 
abstract,final,interface (1).pptx upload
abstract,final,interface (1).pptx uploadabstract,final,interface (1).pptx upload
abstract,final,interface (1).pptx upload
dashpayal697
 
14 interface
14  interface14  interface
14 interface
Ravindra Rathore
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
Kp Sharma
 
Core Java Interface Concepts for BCA Studetns
Core Java Interface Concepts for BCA StudetnsCore Java Interface Concepts for BCA Studetns
Core Java Interface Concepts for BCA Studetns
Jainul Musani
 
Interfaces and abstract classes
Interfaces and abstract classesInterfaces and abstract classes
Interfaces and abstract classes
AKANSH SINGHAL
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .ppt
TabassumMaktum
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .ppt
TabassumMaktum
 
Session 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdfSession 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdf
TabassumMaktum
 
FINAL_DAY10_INTERFACES_roles and benefits.pptx
FINAL_DAY10_INTERFACES_roles and benefits.pptxFINAL_DAY10_INTERFACES_roles and benefits.pptx
FINAL_DAY10_INTERFACES_roles and benefits.pptx
VGaneshKarthikeyan
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
OOPS_Unit2.inheritance and interface objected oriented programming
OOPS_Unit2.inheritance and interface objected oriented programmingOOPS_Unit2.inheritance and interface objected oriented programming
OOPS_Unit2.inheritance and interface objected oriented programming
ssuserf45a65
 
ABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.pptABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
Javainterface
Javainterface Javainterface
Javainterface
prakash GB
 
working with interfaces in java programming
working with interfaces in java programmingworking with interfaces in java programming
working with interfaces in java programming
Parameshwar Maddela
 
12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt
VISHNUSHANKARSINGH3
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
manish kumar
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
ssuser7f90ae
 
abstract,final,interface (1).pptx upload
abstract,final,interface (1).pptx uploadabstract,final,interface (1).pptx upload
abstract,final,interface (1).pptx upload
dashpayal697
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
Kp Sharma
 
Core Java Interface Concepts for BCA Studetns
Core Java Interface Concepts for BCA StudetnsCore Java Interface Concepts for BCA Studetns
Core Java Interface Concepts for BCA Studetns
Jainul Musani
 
Interfaces and abstract classes
Interfaces and abstract classesInterfaces and abstract classes
Interfaces and abstract classes
AKANSH SINGHAL
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .ppt
TabassumMaktum
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .ppt
TabassumMaktum
 
Session 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdfSession 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdf
TabassumMaktum
 
FINAL_DAY10_INTERFACES_roles and benefits.pptx
FINAL_DAY10_INTERFACES_roles and benefits.pptxFINAL_DAY10_INTERFACES_roles and benefits.pptx
FINAL_DAY10_INTERFACES_roles and benefits.pptx
VGaneshKarthikeyan
 
OOPS_Unit2.inheritance and interface objected oriented programming
OOPS_Unit2.inheritance and interface objected oriented programmingOOPS_Unit2.inheritance and interface objected oriented programming
OOPS_Unit2.inheritance and interface objected oriented programming
ssuserf45a65
 
ABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.pptABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
Ad

More from BHUVIJAYAVELU (9)

Eprojectprojectfinalreportgsmmonitoringcontrollingofdevicesusinggsm 090811012...
Eprojectprojectfinalreportgsmmonitoringcontrollingofdevicesusinggsm 090811012...Eprojectprojectfinalreportgsmmonitoringcontrollingofdevicesusinggsm 090811012...
Eprojectprojectfinalreportgsmmonitoringcontrollingofdevicesusinggsm 090811012...
BHUVIJAYAVELU
 
Lecture no1
Lecture no1Lecture no1
Lecture no1
BHUVIJAYAVELU
 
Java arrays
Java arraysJava arrays
Java arrays
BHUVIJAYAVELU
 
Hybrid m-a-t
Hybrid m-a-tHybrid m-a-t
Hybrid m-a-t
BHUVIJAYAVELU
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
BHUVIJAYAVELU
 
Java packages
Java packagesJava packages
Java packages
BHUVIJAYAVELU
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Flow control and error control
Flow control and error controlFlow control and error control
Flow control and error control
BHUVIJAYAVELU
 
3 2--power-aware-cloud
3 2--power-aware-cloud3 2--power-aware-cloud
3 2--power-aware-cloud
BHUVIJAYAVELU
 
Eprojectprojectfinalreportgsmmonitoringcontrollingofdevicesusinggsm 090811012...
Eprojectprojectfinalreportgsmmonitoringcontrollingofdevicesusinggsm 090811012...Eprojectprojectfinalreportgsmmonitoringcontrollingofdevicesusinggsm 090811012...
Eprojectprojectfinalreportgsmmonitoringcontrollingofdevicesusinggsm 090811012...
BHUVIJAYAVELU
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Flow control and error control
Flow control and error controlFlow control and error control
Flow control and error control
BHUVIJAYAVELU
 
3 2--power-aware-cloud
3 2--power-aware-cloud3 2--power-aware-cloud
3 2--power-aware-cloud
BHUVIJAYAVELU
 
Ad

Java interface

  • 2.  An interface is like a class. It has static constants and abstract methods.  Interfaces are declared using the interface keyword,  method signature  constant declarations (variable declarations that are declared to be both static and final).  An interface never contains method implementations (ie function "bodies").
  • 3. access interface name { return-type method-name1(parameter-list); return-type method-name2(parameter-list); type final-varname1 = value; type final-varname2 = value; // ... return-type method-nameN(parameter-list); type final-varnameN = value; }
  • 4.  Access – either public or not used.  Not used – default is an answer.  i/f is only available to other member of the package which it’s declared.  Methods declared have no bodies and end with a semicolon after the parameter list(abstract methods). Each class that includes an interface must implement all of the methods.  Variables – declared implicitly final and static, meaning they cannot be changed by the implementing class. They must also be initialized with a constant value.  All methods and variables are implicitly public if the interface, itself, is declared as public.
  • 5. Understanding relationship between classes and interfaces
  • 7.  To implement an interface – include the implements clause in a class definition access class classname [extends superclass] [implements interface [,interface...]] { constant declarations ; abstract method declarations; }  The methods that implement an interface must be declared public.
  • 8. However, an interface is different from a class in several ways, including:  You cannot instantiate an interface.  An interface does not contain any constructors.  All of the methods in an interface are abstract.  An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.  An interface is not extended by a class; it is implemented by a class.  An interface can extend multiple interfaces.
  • 9. interface shape { public String baseclass="shape"; public void Draw(); } class circle implements shape { public void Draw() { System.out.println("Drawing Circle here"); }} public class inter { public static void main(String[] args) { circle s=new circle(); // shape s=new circle(); s.Draw(); } } interface Printable { void print(); } interface Showable { void show(); } class A implements Printable,Showable { public void print() {System.out.println("Hello");} public void show() {System.out.println("Welcome"); } public static void main(String args[]) { A obj = new A(); obj.print(); obj.show(); }}
  • 10.  An interface can be declared as member of a class or another interface – called nested interface.  How can we define i/f inside a class and how can we access it. class A { interface Message() { void msg(); } } Class test implements A.Message { Public void msg() { S.o.p(“Hello”); } Public static void main(String args[]) { A.Message message=new test(); message.msg(); } }
  • 11.  Interfaces Can Be Extended:  One interface can inherit another interface - extend.  The syntax is the same as for inheriting classes. // One interface an extend another. interface A { void meth1(); void meth2(); } // B now includes meth1() and meth2() -- it adds meth3(). interface B extends A { void meth3(); }
  • 12. interface Printable { void print(); } interface Showable extends Printable { void show(); } class A implements Showable { public void print() {System.out.println("Hello");} public void show() {System.out.println("Welcome");} public static void main(String args[]) { A obj=new A(); obj.print(); obj.show(); }}