๐Ÿ“š

ย >ย 

๐Ÿ’ปย 

ย >ย 

โœ๏ธ

Interfaces

2 min readโ€ขjuly 11, 2024

Milo Chang

Milo Chang

Milo Chang

Milo Chang

What is an interfaceโ“

An interface is like an abstract class, butย a class can inherit multiple interfacesย while only one abstract class can be inherited. Like abstract classes, you use interfaces when there are several classes that will have similar methods available.ย Even though you don't declare them abstract, all methods in an interface are abstract.

Interfaces cannot be instantiated.ย You can't do "Interface a = new Interface();". ๐Ÿšซ

In an interface, you define methods and variables that the child classes will inherit.

Inside an interface, all methods are abstract methods.ย 

  • An abstract method must be left with no bodyย (no braces, just put a semicolon after the method header)
  • An abstract method is basically a promise that the method will exist and be implemented in a child class
  • Any regular class derived from your interface has to implement the abstract methods (you have to give the method a body)

Example: ๐Ÿคจ

public interface EatInterface 
{
ย  ย public void hungry(int x);
}

public interface DrinkInterface
{
ย  ย public String thirsty(boolean isDehydrated);
}

As you can see, ย interfaces are usually just filled with method headers that end with a semicolon.

Why use an interface in this situation? ๐Ÿคทโ€โ™‚๏ธ There are certain types of food that you can both eat and drink (like ramen). By using interfaces, you can inherit functions related to eating from one interface and other functions related to drinking from another interface.

Important Points: ๐Ÿ‘€

  • If you implement interfaces, the code will not compile until you give a body to the abstract methods (this is the same as an abstract class)
public class Ramen implements EatInterface, DrinkInterface
{
ย  ย public void hungry(int x)
ย  ย {
ย  ย  ย  System.out.println("Keep Eating");
ย  ย }

ย  ย public String thirsty(boolean isDehydrated)
ย  ย {
ย  ย  ย  return "More water";
ย  ย }

ย  ย // There may be instance variables, constructors, and other methods not
ย  ย // shown.
}
  • Note that the class "implements" the interface
  • A class can extend a parent class and implement interfaces; if there was a class called Noodle, you could do:
public class Ramen extends Noodle, implements EatInterface, DrinkInterface
{
ย  ย // There may be instance variables, constructors, and other methods not
ย  ย // shown.
}
  • A class can inherit many interfaces (this is different from abstract classes)
  • All methods in an interface are abstract (this is different from abstract classes)
  • Variables (also called fields) in an interface are static and final variables (these variables belong to the class and cannot be changed)
  • Interfaces can be used to contain constants (like Physics constants)

Summary: ๐ŸŽ‰โœจ๐Ÿ˜Ž

  • An interface is like an abstract class except all methods are abstract
  • An interface cannot be instantiated
  • All variables in an interface are static and final
  • A class that implements interfaces must give a body to all of the abstract methods
  • A class can inherit multiple interfaces
  • For methods in an interface, just put a semicolon after the method header (no braces)

<< Hide Menu

๐Ÿ“š

ย >ย 

๐Ÿ’ปย 

ย >ย 

โœ๏ธ

Interfaces

2 min readโ€ขjuly 11, 2024

Milo Chang

Milo Chang

Milo Chang

Milo Chang

What is an interfaceโ“

An interface is like an abstract class, butย a class can inherit multiple interfacesย while only one abstract class can be inherited. Like abstract classes, you use interfaces when there are several classes that will have similar methods available.ย Even though you don't declare them abstract, all methods in an interface are abstract.

Interfaces cannot be instantiated.ย You can't do "Interface a = new Interface();". ๐Ÿšซ

In an interface, you define methods and variables that the child classes will inherit.

Inside an interface, all methods are abstract methods.ย 

  • An abstract method must be left with no bodyย (no braces, just put a semicolon after the method header)
  • An abstract method is basically a promise that the method will exist and be implemented in a child class
  • Any regular class derived from your interface has to implement the abstract methods (you have to give the method a body)

Example: ๐Ÿคจ

public interface EatInterface 
{
ย  ย public void hungry(int x);
}

public interface DrinkInterface
{
ย  ย public String thirsty(boolean isDehydrated);
}

As you can see, ย interfaces are usually just filled with method headers that end with a semicolon.

Why use an interface in this situation? ๐Ÿคทโ€โ™‚๏ธ There are certain types of food that you can both eat and drink (like ramen). By using interfaces, you can inherit functions related to eating from one interface and other functions related to drinking from another interface.

Important Points: ๐Ÿ‘€

  • If you implement interfaces, the code will not compile until you give a body to the abstract methods (this is the same as an abstract class)
public class Ramen implements EatInterface, DrinkInterface
{
ย  ย public void hungry(int x)
ย  ย {
ย  ย  ย  System.out.println("Keep Eating");
ย  ย }

ย  ย public String thirsty(boolean isDehydrated)
ย  ย {
ย  ย  ย  return "More water";
ย  ย }

ย  ย // There may be instance variables, constructors, and other methods not
ย  ย // shown.
}
  • Note that the class "implements" the interface
  • A class can extend a parent class and implement interfaces; if there was a class called Noodle, you could do:
public class Ramen extends Noodle, implements EatInterface, DrinkInterface
{
ย  ย // There may be instance variables, constructors, and other methods not
ย  ย // shown.
}
  • A class can inherit many interfaces (this is different from abstract classes)
  • All methods in an interface are abstract (this is different from abstract classes)
  • Variables (also called fields) in an interface are static and final variables (these variables belong to the class and cannot be changed)
  • Interfaces can be used to contain constants (like Physics constants)

Summary: ๐ŸŽ‰โœจ๐Ÿ˜Ž

  • An interface is like an abstract class except all methods are abstract
  • An interface cannot be instantiated
  • All variables in an interface are static and final
  • A class that implements interfaces must give a body to all of the abstract methods
  • A class can inherit multiple interfaces
  • For methods in an interface, just put a semicolon after the method header (no braces)