nyu-daniel-zint / intro-to-computer-science-2024-spring / interfaces
+ - 0:00:00
Notes for current slide
Notes for next slide

Interfaces

1 / 20

Overview

3 / 20

Overview

Concept

An interface, like a class, is a Java reference type.

3 / 20

Overview

Concept

An interface, like a class, is a Java reference type.

public interface Foo {
/* some stuff here */
}
3 / 20

Overview

Similarity to classes

Like classes, interfaces can contain...

4 / 20

Overview

Similarity to classes

Like classes, interfaces can contain...

  • properties
4 / 20

Overview

Similarity to classes

Like classes, interfaces can contain...

  • properties

  • methods

4 / 20

Overview

Similarity to classes

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!");
}
}
4 / 20

Overview

False advertising

But all is not as it seems.

5 / 20

Overview

False advertising

But all is not as it seems.

  • Interfaces are intended to solve a very different problem than classes.
5 / 20

Overview

False advertising

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.

5 / 20

Raison d'être

6 / 20

Raison d'être

Concept

Interfaces serve as a sort of contract.

6 / 20

Raison d'être

Concept

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.
6 / 20

Raison d'être

Concept

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.

6 / 20

Raison d'être

Contractual agreement

7 / 20

Raison d'être

Contractual agreement

At their core, interfaces specify a set of method signatures.

7 / 20

Raison d'être

Contractual agreement

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.
7 / 20

Raison d'être

Contractual agreement

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.

7 / 20

Raison d'être

Contractual agreement

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.

7 / 20

Example

8 / 20

Example

A facetious interface

For example, imagine the following interface:

// your mileage may vary
public interface IdealEmployee {
public abstract void enjoyEatingMealsInCubicle();
public abstract void rarelyLeaveBuildingOrCampus();
public abstract void playSillyGamesInOffice();
public abstract void readilyAcceptAFancyJobTitleInsteadOfIncreasedPay();
}
8 / 20

Example

A facetious interface

For example, imagine the following interface:

// your mileage may vary
public 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.

8 / 20

Example

A facetious interface

For example, imagine the following interface:

// your mileage may vary
public 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 */
}
8 / 20

Example

Abiding by the contract

Classes that implement an interface must implement the methods declared within the interface. The code won't compile otherwise.

9 / 20

Example

Abiding by the contract

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
}
}
9 / 20

Example

Abiding by the contract

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 class may contain other properties and methods in addition to those specified in the interface.
9 / 20

Example

Ensuring consistent behavior across classes

The value of an interface is that it can ensure the same set of behaviors across several different classes.

10 / 20

Example

Ensuring consistent behavior across 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
}
}
10 / 20

Example

Ensuring consistent behavior across 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 expectation would be that the implementation of these behaviors is significantly different among the different classes that implement them.
10 / 20

Differences from classes

11 / 20

Differences from classes

Concept

Unlike classes, interfaces are intended to be abstract and not include implementation details of the behaviors they specify.

11 / 20

Differences from classes

Concept

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.
11 / 20

Differences from classes

Concept

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.

11 / 20

Differences from classes

Concept

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.

11 / 20

Methods With Implementations

12 / 20

Methods With Implementations

Default methods

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.

12 / 20

Methods With Implementations

Default methods

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.
12 / 20

Methods With Implementations

Default methods

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();
12 / 20

Methods With Implementations

Default methods

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.
12 / 20

Methods With Implementations

Default methods

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.

12 / 20

Methods With Implementations

Default methods (continued)

For example, version 2 of our interface might add the new method, including a default implementation.

13 / 20

Methods With Implementations

Default methods (continued)

For example, version 2 of our interface might add the new method, including a default implementation.

13 / 20

Methods With Implementations

Default methods (continued)

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!");
}
}
13 / 20

Methods With Implementations

Default methods (continued)

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!");
}
}
  • In this way, developers using v1 of the interface can upgrade to v2 with no worry about their code breaking.
13 / 20

Methods With Implementations

Static methods

Interfaces can also contain implementations of static methods.

14 / 20

Methods With Implementations

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);
}
}
14 / 20

Implementing Multiple Interfaces

15 / 20

Implementing Multiple Interfaces

Direct implementation of multiples

Unlike with class-based inheritance, it is possible for a class to implement more than one interface.

15 / 20

Implementing Multiple Interfaces

Direct implementation of multiples

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.
15 / 20

Implementing Multiple Interfaces

Direct implementation of multiples

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.

15 / 20

Implementing Multiple Interfaces

Direct implementation of multiples

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
}
15 / 20

Implementing Multiple Interfaces

Indirect implementation of multiples

Interfaces can also inherit from one-another.

16 / 20

Implementing Multiple Interfaces

Indirect implementation of multiples

Interfaces can also inherit from one-another.

  • For example, let's imagine a simple parent interface:
public interface Thinker {
public abstract void pauseBeforeRespondingToQuestion();
public abstract void considerAllPossibleImplications(BigEvent bigEvent);
}
16 / 20

Implementing Multiple Interfaces

Indirect implementation of multiples

Interfaces can also inherit from one-another.

  • For example, let's imagine a simple parent interface:
public interface Thinker {
public abstract void pauseBeforeRespondingToQuestion();
public abstract void considerAllPossibleImplications(BigEvent bigEvent);
}
  • And a child interface:
public interface IndependentThinker extends Thinker {
public abstract void lookAskanceTowardsHorizon();
public abstract void mumbleToOneself();
}
16 / 20

Implementing Multiple Interfaces

Indirect implementation of multiples

Interfaces can also inherit from one-another.

  • For example, let's imagine a simple parent interface:
public interface Thinker {
public abstract void pauseBeforeRespondingToQuestion();
public abstract void considerAllPossibleImplications(BigEvent bigEvent);
}
  • And a child interface:
public interface IndependentThinker extends Thinker {
public abstract void lookAskanceTowardsHorizon();
public abstract void mumbleToOneself();
}
  • As with class-based inheritance, a child interface inherits all the methods and properties of the parent, including abstract methods.
16 / 20

Implementing Multiple Interfaces

Indirect implementation of multiples (continued)

A class that implements an interface has to implement any abstract methods in the interface, including those passed down from ancestor interfaces.

17 / 20

Implementing Multiple Interfaces

Indirect implementation of multiples (continued)

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
}
}
17 / 20

Polymorphism

18 / 20

Polymorphism

Concept

Objects that implement a given interface in Java can polymorphically be considered to be of the interface type.

18 / 20

Polymorphism

Concept

Objects that implement a given interface in Java can polymorphically be considered to be of the interface type.

  • For example, a RecentCollegeGrad object could be referenced by a IdealEmployee-typed variable.
18 / 20

Polymorphism

Concept

Objects that implement a given interface in Java can polymorphically be considered to be of the interface type.

  • For example, a RecentCollegeGrad object could be referenced by a IdealEmployee-typed variable.
// instantiate an object and reference with variable of interface type
IdealEmployee pat = new RecentCollegeGrade("Pat", 21, "Computer Science");
18 / 20

Polymorphism

Batch operations

As with all polymorphism, this can be useful for batch operations.

19 / 20

Polymorphism

Batch operations

As with all polymorphism, this can be useful for batch operations.

  • Create an array data structure with a bunch of objects that all implement the interface
IdealEmployee[] employees = {
new RecentCollegeGrade("Pat", 21, "Computer Science"),
new OfficePuppy("Fido", "German Shepherd", 4)
};
19 / 20

Polymorphism

Batch operations

As with all polymorphism, this can be useful for batch operations.

  • Create an array data structure with a bunch of objects that all implement the interface
IdealEmployee[] employees = {
new RecentCollegeGrade("Pat", 21, "Computer Science"),
new OfficePuppy("Fido", "German Shepherd", 4)
};
  • Loop through them and trigger some behaviors they all have in common.
// iterate through each object
for (IdealEmployee emp : employees) {
emp.acceptCutsToCompensationWithNoComplaints();
}
19 / 20

Conclusions

20 / 20

Conclusions

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.

20 / 20
Paused

Help

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