An interface, like a class, is a Java reference type.
An interface, like a class, is a Java reference type.
public interface Foo { /* some stuff here */}
Like classes, interfaces can contain...
Like classes, interfaces can contain...
Like classes, interfaces can contain...
properties
methods
Like classes, interfaces can contain...
properties
methods
public interface Foo { // a property public int x = 10; // a method public static void y() { System.out.println("Hello!"); }}
But all is not as it seems.
But all is not as it seems.
But all is not as it seems.
Interfaces are intended to solve a very different problem than classes.
Because of their intended use, there are very specific limitations on what kinds of properties and methods can be placed within an interface.
Interfaces serve as a sort of contract.
Interfaces serve as a sort of contract.
Interfaces serve as a sort of contract.
An interfaces defines a public interface - a set of behaviors - that any class can agree to adhere to.
Java will not compile the code if any class that agreed to adhere to an interface does not properly implement its public interface.
At their core, interfaces specify a set of method signatures.
At their core, interfaces specify a set of method signatures.
At their core, interfaces specify a set of method signatures.
Any class can declare that it implements an interface, meaning it agrees to implement the behaviors the interface specifies.
This contractually guarantees that the class will implement the methods specified in the interface.
At their core, interfaces specify a set of method signatures.
Any class can declare that it implements an interface, meaning it agrees to implement the behaviors the interface specifies.
This contractually guarantees that the class will implement the methods specified in the interface.
Other code can rely on the fact that the class will be able to perform the behaviors the interface specifies.
For example, imagine the following interface:
// your mileage may varypublic interface IdealEmployee { public abstract void enjoyEatingMealsInCubicle(); public abstract void rarelyLeaveBuildingOrCampus(); public abstract void playSillyGamesInOffice(); public abstract void readilyAcceptAFancyJobTitleInsteadOfIncreasedPay();}
For example, imagine the following interface:
// your mileage may varypublic interface IdealEmployee { public abstract void enjoyEatingMealsInCubicle(); public abstract void rarelyLeaveBuildingOrCampus(); public abstract void playSillyGamesInOffice(); public abstract void readilyAcceptAFancyJobTitleInsteadOfIncreasedPay();}
Any class can "sign on" to this interface using the implements keyword.
For example, imagine the following interface:
// your mileage may varypublic interface IdealEmployee { public abstract void enjoyEatingMealsInCubicle(); public abstract void rarelyLeaveBuildingOrCampus(); public abstract void playSillyGamesInOffice(); public abstract void readilyAcceptAFancyJobTitleInsteadOfIncreasedPay();}
Any class can "sign on" to this interface using the implements keyword.
public class RecentGrad implements IdealEmployee { /* some stuff here */}
Classes that implement an interface must implement the methods declared within the interface. The code won't compile otherwise.
Classes that implement an interface must implement the methods declared within the interface. The code won't compile otherwise.
public class RecentCollegeGrad implements IdealEmployee { public void enjoyEatingMealsInCubicle() { // implementation goes here } public void rarelyLeaveBuildingOrCampus() { // implementation goes here } public void playSillyGamesInOffice() { // implementation goes here } public void readilyAcceptAFancyJobTitleInsteadOfIncreasedPay() { // implementation goes here }}
Classes that implement an interface must implement the methods declared within the interface. The code won't compile otherwise.
public class RecentCollegeGrad implements IdealEmployee { public void enjoyEatingMealsInCubicle() { // implementation goes here } public void rarelyLeaveBuildingOrCampus() { // implementation goes here } public void playSillyGamesInOffice() { // implementation goes here } public void readilyAcceptAFancyJobTitleInsteadOfIncreasedPay() { // implementation goes here }}
The value of an interface is that it can ensure the same set of behaviors across several different classes.
The value of an interface is that it can ensure the same set of behaviors across several different classes.
public class OfficePuppy implements IdealEmployee { public void enjoyEatingMealsInCubicle() { // implementation goes here } public void rarelyLeaveBuildingOrCampus() { // implementation goes here } public void playSillyGamesInOffice() { // implementation goes here } public void readilyAcceptAFancyJobTitleInsteadOfIncreasedPay() { // implementation goes here }}
The value of an interface is that it can ensure the same set of behaviors across several different classes.
public class OfficePuppy implements IdealEmployee { public void enjoyEatingMealsInCubicle() { // implementation goes here } public void rarelyLeaveBuildingOrCampus() { // implementation goes here } public void playSillyGamesInOffice() { // implementation goes here } public void readilyAcceptAFancyJobTitleInsteadOfIncreasedPay() { // implementation goes here }}
Unlike classes, interfaces are intended to be abstract and not include implementation details of the behaviors they specify.
Unlike classes, interfaces are intended to be abstract and not include implementation details of the behaviors they specify.
Unlike classes, interfaces are intended to be abstract and not include implementation details of the behaviors they specify.
all methods are abstract by default, even if that is not written explicitly into the code.
all properties and methods in an interface are public, even if that is not written explicitly into the code.
Unlike classes, interfaces are intended to be abstract and not include implementation details of the behaviors they specify.
all methods are abstract by default, even if that is not written explicitly into the code.
all properties and methods in an interface are public, even if that is not written explicitly into the code.
all properties are furthermore static and constant, even if that is not written explicitly into the code.
It is possible to include an instance method with an implementation in an interface. This is called a default method and serves one specific use case, and no other.
It is possible to include an instance method with an implementation in an interface. This is called a default method and serves one specific use case, and no other.
It is possible to include an instance method with an implementation in an interface. This is called a default method and serves one specific use case, and no other.
Imagine our example interface is already in use - developers have writen code abiding by its contractual rules.
Now, imagine the creators of the interface decide to add a new abstract method
public abstract void acceptCutsToCompensationWithNoComplaints();
It is possible to include an instance method with an implementation in an interface. This is called a default method and serves one specific use case, and no other.
Imagine our example interface is already in use - developers have writen code abiding by its contractual rules.
Now, imagine the creators of the interface decide to add a new abstract method
public abstract void acceptCutsToCompensationWithNoComplaints();
It is possible to include an instance method with an implementation in an interface. This is called a default method and serves one specific use case, and no other.
Imagine our example interface is already in use - developers have writen code abiding by its contractual rules.
Now, imagine the creators of the interface decide to add a new abstract method
public abstract void acceptCutsToCompensationWithNoComplaints();
If the developers using the old interface upgraded to the new version, their code would immediately break and not compile.
A default method solves this problem by including a new method in an interface that includes its own default implementation - a placeholder until the developers can write their own.
For example, version 2 of our interface might add the new method, including a default implementation.
For example, version 2 of our interface might add the new method, including a default implementation.
For example, version 2 of our interface might add the new method, including a default implementation.
public interface IdealEmployee { // assume all the methods specified in v1 of the interface are still placed here // the new method, with a default implementation so existing class code doesn't break public default void acceptCutsToCompensationWithNoComplaints() { System.out.println("Thank you so much for giving me this opportunity!"); }}
For example, version 2 of our interface might add the new method, including a default implementation.
public interface IdealEmployee { // assume all the methods specified in v1 of the interface are still placed here // the new method, with a default implementation so existing class code doesn't break public default void acceptCutsToCompensationWithNoComplaints() { System.out.println("Thank you so much for giving me this opportunity!"); }}
v1
of the interface can upgrade to v2
with no worry about their code breaking.Interfaces can also contain implementations of static methods.
Interfaces can also contain implementations of static methods.
public interface IdealEmployee { // assume all the methods previously discussed are still placed here // a behavior shared by all IdealEmployee objects public static void speakDeferrentially(String message) { String protocol = "I'm so sorry to disturb you... I know you're very busy... But"; String output = String.format("%s %s", protocol, message); }}
Unlike with class-based inheritance, it is possible for a class to implement more than one interface.
Unlike with class-based inheritance, it is possible for a class to implement more than one interface.
IndependentThinker
.Unlike with class-based inheritance, it is possible for a class to implement more than one interface.
Imagine we had a second interface called IndependentThinker
.
A single class could implement both the IdealEmployee
and IndependentThinker
interfaces.
Unlike with class-based inheritance, it is possible for a class to implement more than one interface.
Imagine we had a second interface called IndependentThinker
.
A single class could implement both the IdealEmployee
and IndependentThinker
interfaces.
public class RecentCollegeGrad implements IdealEmployee, IndependentThinker { // all abstract methods from IdealEmployee must be implemented in this class // all abstract methods from IndependentThinker must also be implemented in this class}
Interfaces can also inherit from one-another.
Interfaces can also inherit from one-another.
public interface Thinker { public abstract void pauseBeforeRespondingToQuestion(); public abstract void considerAllPossibleImplications(BigEvent bigEvent);}
Interfaces can also inherit from one-another.
public interface Thinker { public abstract void pauseBeforeRespondingToQuestion(); public abstract void considerAllPossibleImplications(BigEvent bigEvent);}
public interface IndependentThinker extends Thinker { public abstract void lookAskanceTowardsHorizon(); public abstract void mumbleToOneself();}
Interfaces can also inherit from one-another.
public interface Thinker { public abstract void pauseBeforeRespondingToQuestion(); public abstract void considerAllPossibleImplications(BigEvent bigEvent);}
public interface IndependentThinker extends Thinker { public abstract void lookAskanceTowardsHorizon(); public abstract void mumbleToOneself();}
A class that implements an interface has to implement any abstract methods in the interface, including those passed down from ancestor interfaces.
A class that implements an interface has to implement any abstract methods in the interface, including those passed down from ancestor interfaces.
public class OfficePuppy implements IndependentThinker { // must implement all abstract methods from IndependentThinker interface public void lookAskanceTowardsHorizon() { // implementation goes here } public void mumbleToOneself() { // implementation goes here } // must also implement all abstract methods from Thinker interface public void pauseBeforeRespondingToQuestion() { // implementation goes here } public void considerAllPossibleImplications() { // implementation goes here }}
Objects that implement a given interface in Java can polymorphically be considered to be of the interface type.
Objects that implement a given interface in Java can polymorphically be considered to be of the interface type.
RecentCollegeGrad
object could be referenced by a IdealEmployee
-typed variable.Objects that implement a given interface in Java can polymorphically be considered to be of the interface type.
RecentCollegeGrad
object could be referenced by a IdealEmployee
-typed variable.// instantiate an object and reference with variable of interface typeIdealEmployee pat = new RecentCollegeGrade("Pat", 21, "Computer Science");
As with all polymorphism, this can be useful for batch operations.
As with all polymorphism, this can be useful for batch operations.
IdealEmployee[] employees = { new RecentCollegeGrade("Pat", 21, "Computer Science"), new OfficePuppy("Fido", "German Shepherd", 4)};
As with all polymorphism, this can be useful for batch operations.
IdealEmployee[] employees = { new RecentCollegeGrade("Pat", 21, "Computer Science"), new OfficePuppy("Fido", "German Shepherd", 4)};
// iterate through each objectfor (IdealEmployee emp : employees) { emp.acceptCutsToCompensationWithNoComplaints();}
As you have seen, interfaces in Java offer an alternative form of inheritance in Java with some similarities - and many differences - with class-based inheritance.
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |