Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎

2018-11-20 Interfaces

posted Nov 20, 2018, 6:23 AM by Konstantinovich Samuel   [ updated Nov 20, 2018, 11:48 AM ]
Goal: Interfaces

In real life:
Objects have ways to interact with them like buttons and switches.
In java, methods provide the tools that the outside world requires to interact with that object.
This is not an interface.

In java:
Interfaces in Java allow us to create a collection of methods to guarantee a class's interaction.
An interface does not contain method implementation, constructors, or instance variables.
The keyword interface is used to define this collection of methods.

Interfaces are often named as an adjective. Some are nouns (see List in java) but
the idea that the adjective can describe what it can do is more in the spirit of what an interface is.
Examples of adjective interface names: cloneable, comparable, runnable
( or the fictional ones like: smashable , killable, fixable, etc)

*Interfaces cannot be instantiated, but they do create an is-a relationship which means that you can have a variable type (reference type) that is an interface.

(You probably won't extend interfaces)


Example of creation of an interface:

public interface Washable

  void mop();
  void scrub(int times);
  void scrape(int times);
}


class Floor implements Washable{

   // The compiler will now require that methods
   // mop, scrub, and scrape
   // all be implemented. Compilation will fail if those
   // methods are missing from this class.

}


Abstract classes vs Interfaces:


Interfaces do not have any implementation. They are not classes, and cannot have any instance variables.

1. They are good when you want unrelated classes to share a behavior.

2. They allow a single class to implement multiple behaviors. e.g. both Breakable and Fixable


An abstract class is meant to provide a superclass from which other classes can inherit and thus share a common design.

Abstract classes CAN include instance variables, and some concrete methods.

Abstract classes:

1. let you define some behaviors; and force your subclasses to provide others.

2. are an excellent way to create planned inheritance hierarchies, as they are a good choice for non-leaf classes.

3. Limit a class to extend ONE abstract class.





Interfaces are a way to get around single inheritance in java. You CAN have features from multiple java files. You can only extend one class, but you can implement any number of other interfaces.

Here is an example (ignore the fact that the circle has public access)
The Mutable versions of the point and circle could both be "moved" in the same way using a common interface.
This is not explicitly defined here, but you could have moveTo(x,y) and/or moveBy(x,y) methods in the interface.




Interfaces you should be aware of:

List<T>

Comparable<T>

Cloneable




Finally, if you understand the following statements about interfaces, then you should be good:
"Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at compile time. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile."

"When you define a new interface, you are defining a new reference data type. You can use interface names anywhere you can use any other data type name. If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface."

Comments