What is Interface In Java?

1.5K views 3 minutes read
A+A-
Reset

Introduction:

Interface is definition of set of actions that an object of particular type can do. Interface is group of related abstract methods. Interface also may contain fields. Interface contains method declarations. Constructors are not there in interface so interface can not be used to create instances.

Defining interface is very simple and similar to class definition. We use keyword interface to define an interface.

Example: Here interface named Vehicle is defined

public interface Vehicle{
String model = "VXR8 GTS";// String model; only will show compile time error 
void startEngine();
void speedUp();
void applyBreak();
void stopEngine();

}
public interface LoadCarrier{

void load();
void unLoad();
}

By default all the methods inside interface are public abstract and all the fields are final static. So when we do not initialize the field variable, it will produce compile time errors.

Interface can extend another interface. Actually an interface can extend number of interfaces, that is interfaces support multiple inheritance. We use keyword extends followed by comma separated list of parent interfaces.

Example: An interface inheriting both interfaces Vehicle and LoadCarrier

public interface Truck extends Vehicle, LoadCarrier{

public void getPower();
}

We can implement the interface using implements keywords. A class implementing particular interface need to implement all the methods in the interface.

Example: A class MyTrucks implements the interface Truck.

Public class MyTruck implements Truck{

// All the methods from the interface Truck and its parent interfaces

}

Here The class MyTruck must contain definitions of all the seven methods. One from interface Truck and other six methods from parent interfaces of Tuck.

A class can implement multiple interfaces using keyword implements followed by comma separated list of interfaces.

Example: A class MyTrucks implementing multiple interfaces Vehicle and LoadCarrier.

public class MyTruck implements Vehicle, LoadCarrier{

//This class must implement all the methods of interface Vehicle and LoadCarrier.  
}

In above example MyTruck class must contain definitions for all the six methods, four methods from interface Vehicle and two methods from LoadCarrier.

If you have to inherit a super class and extend the interface Use Extends first then after implements. If we reverse the order of extends and implements it will produce error.

Example: Here Class MyTruck inherit a class and implement an interface.

public class Type1 extends MyTruck implements LoadCarrier{

// Type1 implements LoadCarrier extends MyTruck{  } //will produce error  

}

Remember This:

  • All the methods in interface are abstract. So if you try to define method body it will produce error.
  • All the fields in interface are static and final by default. So member fields should be initialized within interfaces.
  • Interface members can not be private and protected because these are used in the other classes.
  • Interfaces can not be final.
  • We can not create objects from interface but we can create references. Since constructors should not be there inside interface.
  • We should have to implement all the methods in interface.

Note: Some interfaces are called marker interfaces which do not contain even a single method. Example Serializable, Clonnable and Remote.

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Index

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.