Interfaces
• Specify behavior of objects
• Used for existing classes to create “contracts” with certain behavior (methods)
Interfaces
• Can only contain constants and abstract
methods
– All methods are assumed to be “public abstract”
• Do not need to explicitly state these key words
– All data fields are “public static final”
• Do not need to explicitly state these key words
When to Use Interfaces
• When you want to specify a set of behavior
• When this behavior can be shared among several
classes
• Abstract class
– When your class is a sub-type of the abstract class • Interface
– When your class uses common methods of the interface
When to Use Interfaces
• Start with functions in a class
• If you notice this functionality can be shared
• Consider abstracting this functionality from the class to an interface
• Have the class(es) implement this interface
Interface Examples
• Comparable
– Used when an object can be compared to another object
– Used by sorting libraries of the Collections class – https://
docs.oracle.com/javase/8/docs/api/java/lang/Compara ble.html
• Serializable
– Used when your object can be serialized and written to disk
– https://
docs.oracle.com/javase/8/docs/api/java/io/Serializable .html
Interface Examples
• Runnable
– Used by any classes that are executed by a thread – The run method acts like a main method and is
where the execution of threads start
– https://
docs.oracle.com/javase/8/docs/api/java/lang/Runna ble.html
• Iterable
– Used by objects (lists) which can be iterated over – https://
docs.oracle.com/javase/8/docs/api/java/lang/Iterabl e.html
How to Make an Interface
• Use the “interface” keyword in front of the
interface name
• Example:
– “public interface Edible”
How to Use an Interface
• Several useful interfaces already exist in java • To use these interfaces, use the key word
“implements” after the class name
• Examples
– “public class Food implements Edible”
– “public class Card implements Comparable<Card>”
• Implementing classes must define the methods in the interface
– This ensures the contract between the class and the
Using Interface Variables
• Is the following code valid?
– List<Integer> someList = new ArrayList<Integer>(); • List is an interface
• ArrayList is a class
Using Interface Variables
• Is the following code valid?
– List<Integer> someList = new ArrayList<Integer>(); • List is an interface
• ArrayList is a class
• Yes
– Why?
Using Interface Variables
List<Integer> someList = new ArrayList<Integer>();
– This is very common syntax when using interfaces – Often preferred over using the class name
– Can use this to call methds of the interface – Cannot use this to call methods of the class
Using Interface Variables
List<Integer> someList = new ArrayList<Integer>();
13
We are creating a variable of type “ArrayList<Integer>”
Using Interface Variables
List<Integer> someList = new ArrayList<Integer>();
14
Using Interface Variables
List<Integer> someList = new ArrayList<Integer>();
15
someList can use any methods defined in the List interface
Using Interface Variables
List<Integer> someList = new ArrayList<Integer>();
16
Interface Rules
• Cannot create an instance of an interface
• Can have an inheritance hierarchy
– One interface can inherit (extend) from another – An interface cannot extend from classes
• A class can implement any number of
interfaces
– A class can only extend one class
• Interfaces are the solution to this
Example – Interface Example
• Oxygen.java • TextFile.java
• Compressable.java
• Main.java
Abstract Classes
• Classes that cannot be instantiated
• An object cannot be created from an Abstract class
• Classes can inherit from abstract classes • Not very popular
– Can be replaced by regular classes and interfaces
When to Use Abstract Classes
• Sometimes there will be classes that should exist but should not be instantiated
• Example
– An animal Animal
• If I asked you to make an animal, you would typically
respond with “what type of animal?”.
• This question indicated that an animal cannot be created without knowing the type of animal
• Therefore an Animal class cannot be instantiated, but
subclasses can
When to Use Abstract Classes
• Start with normal classes
• If you notice common variables
• Consider making an abstract to hold these common variables
– Include set/get methods too
• If there is common functionality without variables
– Consider using an interface
Abstract Class Examples
• Typically found at the top of the inheritance chain of your favorite classes
• Shape
– https://docs.oracle.com/javafx/2/api/javafx/scene/s
hape/Shape.html
• InputStream – https://
docs.oracle.com/javase/8/docs/api/java/io/InputStr eam.html
Abstract Class Examples
• AbstractCollection
– https://
docs.oracle.com/javase/8/docs/api/java/util/Abstra ctCollection.html
• Clock – https://
docs.oracle.com/javase/8/docs/api/java/time/Clock .html
How to Make an Abstract Class
• Use the “abstract” keyword in front of the
class name
• Example:
– “public abstract class Animal”
Abstract Methods
• Abstract classes can have abstract methods • An abstract method is an unimplemented
method of an abstract class
• A regular class cannot have an abstract method • Example
– AbstractCollection class
• public abstract int size()
Abstract Class Rules
• An abstract class can have any number of abstract methods (including none)
– All non-abstract subclasses must implement these
methods
• An abstract class can extend another abstract
class
• An abstract class can extend a regular class • Abstract classes still have constructors
– Called via subclasses
Example - Abstract Example
• Animal.java
• RealAnimal.java
• MythicalAnimal.java • Edible.java
• Donkey.java • Dragon.java • Ogre.java • Main.java