• No results found

Interfaces

N/A
N/A
Protected

Academic year: 2020

Share "Interfaces"

Copied!
27
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Interfaces

• Specify behavior of objects

• Used for existing classes to create “contracts” with certain behavior (methods)

(3)

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

(4)

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

(5)

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

(6)

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

(7)

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 overhttps://

docs.oracle.com/javase/8/docs/api/java/lang/Iterabl e.html

(8)

How to Make an Interface

Use the “interface” keyword in front of the

interface name

• Example:

“public interface Edible”

(9)

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

(10)

Using Interface Variables

Is the following code valid?

List<Integer> someList = new ArrayList<Integer>(); • List is an interface

• ArrayList is a class

(11)

Using Interface Variables

Is the following code valid?

List<Integer> someList = new ArrayList<Integer>(); • List is an interface

• ArrayList is a class

• Yes

– Why?

(12)

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

(13)

Using Interface Variables

List<Integer> someList = new ArrayList<Integer>();

13

We are creating a variable of type “ArrayList<Integer>”

(14)

Using Interface Variables

List<Integer> someList = new ArrayList<Integer>();

14

(15)

Using Interface Variables

List<Integer> someList = new ArrayList<Integer>();

15

someList can use any methods defined in the List interface

(16)

Using Interface Variables

List<Integer> someList = new ArrayList<Integer>();

16

(17)

Interface Rules

• Cannot create an instance of an interface

• Can have an inheritance hierarchy

One interface can inherit (extend) from anotherAn 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

(18)

Example – Interface Example

Oxygen.java • TextFile.java

• Compressable.java

• Main.java

(19)

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

(20)

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

(21)

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

(22)

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

(23)

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

(24)

How to Make an Abstract Class

Use the “abstract” keyword in front of the

class name

• Example:

“public abstract class Animal”

(25)

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()

(26)

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

(27)

Example - Abstract Example

Animal.java

RealAnimal.java

MythicalAnimal.javaEdible.java

Donkey.javaDragon.javaOgre.javaMain.java

References

Related documents

Additionally, by integrating event sponsorship and employee volunteering in the one framework and studying them for the first time together as CSR initiatives relative to the

Orange County, CA California Court of Appeal Justice Richard Aronson Justice William Bedsworth Justice Richard Fybel Justice Raymond Ikola Justice Eileen Moore

Reclaim the way to declare variables hold words, run specialized workloads and spies influenced cryptography and retrieve variables explicitly and personalization company, we need a

- Perform overall risk assessment of your organization - Know the most common methods of penetration. - Consider including certain key steps - Include a

8.5.2 Consolidating Seismic Interpretations in a Reference Project If many Work Projects need to access the same seismic interpretation, you must synchronize the seismic

If law is the tool of the human rights corpus, but legal meth- ods produce modern, universal subjects which are entitled yet victimized (a paradox Wendy Brown and Make Mutua

Defines interfaces ⇒ Abstract Data Types and classes: Collection Set SortedSet List Queue HashSet LinkedHash. Set TreeSet ArrayList

Kemper made a motion to approve the facility request, requested by Green Stride on Sunday October 22, 2017 as long as stipulations of the signs and porter potties are abided by