0% found this document useful (0 votes)
11 views4 pages

Interface

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Interface

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Interface :

==========
Interface is mechanism to achieve abstraction java.
Interface is similar to abstract class but having all methods are abstract type
i.e it can not have method body.
Interface is just like class which contain abstract method only.(till 1.7
version).
By using interface keyword we can create interface.
To achieve interface java provides "implements" keyword.

Syntax : public interface A{


----------
----------
}

1) Interface methods are bydafault public and abstract.

Example :

package interfaceExamples;

public interface A {

public abstract void m1();

void m2();
}

2)we can not take non abstract methods in interface.

Example :

package interfaceExamples;

public interface A {

public abstract void m1();

public void m2() {

}
}

3)Interface variables are bydefault public static and final.

Example :

package interfaceExamples;

public interface A {

public static final int a = 90;


int b = 900;
}

4)we can not create object of interface.

package interfaceExamples;

public interface A {

public static void main(String[] args) {

A a = new A();
}
}

Example :

package interfaceExamples;

public interface Bank {

public abstract void withdraw();

void deposit();
}

package interfaceExamples;

public class SBI implements Bank {

@Override
public void withdraw() {
System.out.println("withdraw logic by SBI Bank");
}

@Override
public void deposit() {
System.out.println("Deposit logic by SBI Bank");

public static void main(String[] args) {


SBI sbi=new SBI();
sbi.withdraw();
sbi.deposit();

Bank bank=new SBI();


bank.withdraw();
bank.deposit();

}
}

package interfaceExamples;

public class ICICI implements Bank {

@Override
public void withdraw() {
System.out.println("withdraw logic by ICICI Bank");

@Override
public void deposit() {
System.out.println("Deposit logic by ICICI Bank");

public static void main(String[] args) {

ICICI icici = new ICICI();


icici.withdraw();
icici.deposit();

Bank bank = new ICICI();


bank.deposit();
bank.withdraw();

Example 2:

package interfaceExamples;

public interface Shape {

public void calArea();


}

package interfaceExamples;

public class Rectangle implements Shape{

@Override
public void calArea() {
System.out.println("logic to calculate area of Rectangle");
}

public static void main(String[] args) {


Rectangle rc=new Rectangle();
rc.calArea();

Shape shape=new Rectangle();


shape.calArea();
}
}

package interfaceExamples;

public class Circle implements Shape{

@Override
public void calArea() {
System.out.println("logic to calculate area of circle");

public static void main(String[] args) {

Circle c=new Circle();


c.calArea();

Shape s=new Circle();


s.calArea();
}

You might also like