Browse By Unit
Milo Chang
Milo Chang
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.
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.
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.
}
public class Ramen extends Noodle, implements EatInterface, DrinkInterface
{
// There may be instance variables, constructors, and other methods not
// shown.
}
<< Hide Menu
Milo Chang
Milo Chang
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.
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.
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.
}
public class Ramen extends Noodle, implements EatInterface, DrinkInterface
{
// There may be instance variables, constructors, and other methods not
// shown.
}
© 2024 Fiveable Inc. All rights reserved.