Pages

What is Interface?

What is Interface?

An interface contains Abstract methods and static constants.

Abstract method: A method that doesn't have a body is called as Abstract method.

Main Advantage: In order to achieve 100% abstraction in java we will use interfaces.

Abstraction: It means hiding the implementation details of methods.

For Example:

A client comes to you and shows your project. Showing entire functionality and code to others is a security problem. So, we will show our project in the form of Interfaces.

I have a project related to Retail domain like Flipkart ,Amazon etc.,.I will manifest my project in below format:

interface myproject{
void Add_item_to_bag();
void ShippingAddress();
void PaymentProcess();
void ReviewDetails();
void PlaceOrder();
void getOrderConfirmationMail();
int product_id =0;
}
By seeing above, anyone can get the high-level information of my project.
Let's discuss more in details technically,
Who is allowed in the interface ? Just observe below interface
interface myinterface{
public static final int a=10; // or declare int a=10;
; // valid statement
abstract String firstmethod(); // or declare String firstmethod();
void method_in_interface();
public static void staticmethod(){ // static methods are also allowed
System.out.println("static method in interface");
}
}
By seeing above interface we can conclude that,

1) The initialization of variables is compulsory. It means if we declare " int a; " we will get compile-time error. So it must be initialized like "int a=10;"
----> If we declare int a=10, internally compiler will convert it to "public static final int a=10;"

2) Only abstract methods are allowed i.e., the return type must be specified like void (or) String etc...otherwise we will get compile-time errors

3) Since Java 8, static methods are allowed in an interface

#####Sample Demo Program on Interface#####

interface myinterfacename {
;
public static final int a = 10;
int b = 20;

abstract String myinterfacenamemethod();

String myinterfacenamemethod1();

void method_in_interface();

public static void staticmethod() {
System.out.println("static method in interface");
}
}


class interfaceImplement implements myinterfacename {
public String myinterfacenamemethod() {
return "method in interface with abstract keyword and return type is String";
}

public String myinterfacenamemethod1() {
return "method in interface without abstract keyword and return type is String";
}

public void method_in_interface() {
System.out.println("method in interface with return type void");
}
}


public class interfaceDemo extends interfaceImplement {
public static void main(String[] args) {
interfaceDemo ia = new interfaceDemo();
System.out.println("Value of a using object="+ia.a); // access value of variable using object
System.out.println("Value of b interface name="+myinterfacename.b); // access value of variable using interface name
System.out.println(ia.myinterfacenamemethod()); // access methods using object
System.out.println(ia.myinterfacenamemethod1());
ia.method_in_interface();
myinterfacename.staticmethod(); // calling static method using interface name
}
}
Output:
interface in java
interface in java
Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.