Java Interface with Examples

Interface is a service requirement specification.interface is an 100% pure abstract class.interface is a combination of public abstract methods and public static final constants.by using we ca achieve abstraction.we ca achieve multiple inheritance by using interface.

We can declare an Interface with interface keyword.

syntax for Interface


public interface interfaceName{
//public,abstract methods
//public,static,final fields
}

Example


package com.java2study;

public interface Vehicle{
String vehicle_Id = "a123";

void showWheels();

}

In Above String vehicle_Id = "a123"; is equal to public static final String vehicle_Id = "a123";

In Above void showWheels(); is equal to public abstract void showWheels();

we need to provide implementation for interface methods in subclasses. To use Java interface our class must implements interface. means we can implement interface by using implements keyword.

Every variables present in interface is public ,static, final by default.

Every method present in interface is public abstract methods bydefault.

interface itself implicitly public abstract.

Example to java iterface

Vehicle.java


package com.java2study;

public interface Vehicle{
String vehicle_Id = "a123";

void showwheels();

}


Car.java


package com.java2study;

public class Car implements Vehicle {

@Override
public void showwheels() {
System.out.println("car has 4 wheels");

}
public static void main(String[] args) {
Car c = new Car();
c.showwheels();
System.out.println(c.vehicle_Id);


}



}

output


car has 4 wheels
a123

Bike.java


package com.java2study;

public class Bike implements Vehicle {

@Override
public void showwheels() {
System.out.println("bike has 2 wheels");

}
public static void main(String[] args) {
Bike b = new Bike();
b.showwheels();
System.out.println(b.vehicle_Id);

}

}

output


bike has 2 wheels
a123

Important points about interfaces

1.interfaces are totally incomplete classes.which means interface does not contain any method implementation. See the snippet below


package com.java2study;

public interface Computer {
public abstract void showConfiguration();

}

interface must be implemented in its child class and have to provide implementation for all the methods present in interface.


package com.java2study;

public class Laptop implements Computer {

public void showConfiguration() {
System.out.println("RAM is 5GB");
System.out.println("HardDisk of 500GB");

}


public static void main(String[] args) {
Computer c = new Laptop();
c.showConfiguration();

}

}

output


RAM is 5GB
HardDisk of 500GB

we cannot create object  directly to the interface but we can create object indirectly.

2.we have to provide implementation for the interface methods is possible only in its implementation class

3.A class can extend only one class.

4.A class can implement ‘n’ number of interfaces.

5.An interface can extends more than one interface .


package com.java2study;

public interface A extends B,C,D,E{


}

6.Interfaces should not be final.

7.An interface does not contain Constructors.

8.inside interface we are not allowed to write any static blocks and instance blocks.

as we said every variable in interface is public,static,final and variables have to be initialised in interface only.


package com.java2study;

public interface Vehicle{
String vehicle_Id = "a123";
}

Interface cannot implements or extends a class. Since defined things cannot be made as undefined things.

What is Marker interface?

it is an interface and it does not contain any methods.such type of interfaces are is known as marker interface.

EX:Serializable interface.RandomAccessInterface

No comments:

Post a Comment

-->