Powered by Blogger.

Interface in Java



Interface in Java

Hello Friends today we learn about Interface in Java
An interface is a reference type in Java, it is similar to class, it is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.

Interface is similar to a class:

An interface can contain any number of methods.

  • An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
  • The byte code of an interface appears in a .class file.
  • Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

How Interface is different from a class:


  • 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.

Why use Interface ?

It is used to achieve fully abstraction.
By interface, we can support the functionality of multiple inheritance.
It can be used to achieve loose coupling.

Example of Java interface

interface printable{
void print();
}
 
class A6 implements printable{
public void print(){System.out.println("Hello");}
 
public static void main(String args[]){
A6 obj = new A6();
obj.print();
 }
}  




facebook
google-plus
twitter
pinterest

No comments :

Post a Comment

HTML Tutorial

images

About Sitesbay

This blog is related to Java Tutorial, C++ Tutorial, C Tutorial, Online Earning tips and more in very simple and easy way. Here i will try to give complete idea related to all new technology.

Java Tutorial

Java is Object oriented programming language, It is more secure and high performance language.

C++ Tutorial

C++ is also Object oriented programming language, It is more simple and easy to learn.

 
https://sitesbay.blogspot.com/?utm_medium=d2898efa59afcc0bc411921036e22b767ca37b09