Java Interface

Ace your homework & exams now with Quizwiz!

Difference Between Abstract Classes and Interfaces

- A class can extend only one class (abstract or not), but implement multiple interfaces. - Interface variables are implicitly public static final. - Abstract Classes can declare any modifiers on instance variables. - Interfaces (as of Java 8) provide default methods that are implemented, but all non-default methods are public abstract. - Abstract Classes can have any declared modifiers on their methods.

Abstract Classes Vs Interface

- Abstract Classes can have instance variables that are not automatically public static final. - Abstract Classes can also have methods that are not public. - Interfaces are used as a contract that are used polymorphically as reference types to guarantee access to certain methods.

Interface

- An abstract type that acts as a "contract" for classes, requiring them to provide implementation for stated methods. - Used to guarantee the availability of methods in implementing classes. - All methods are public abstract. - Are fields are public static final. - Interfaces are written similar to classes, but use the keyword interface instead of class in their declaration. public interface Doable {} - To implement an interface in a class use the implements keyword. If the class is a subclass (extends a super class), use implements after the extends keyword: public class Chore extends Task implements Doable {} - Interfaces make compiler enforced contracts. A class that implements an interface must provide an implementation for every interface method, or be an abstract class. So if the class is a concrete class and not abstract class, it must have an implementation for every method in the interface.

Java 8 Interface Changes

- Before Java Version 8 new methods added to an Interface would breaking existing implementations of the Interface, because the new method work cause existing classes to be considered abstract since they hadn't implemented all the methods in the updated Interface. - With Java 8 the default keyword was introduced. - default methods: interface methods that are implemented in the Interface. - Implementing classes are not required to implement default methods, but can override them. public interface Doable { default void doAThing() { int things = 0; System.out.println("Done!"); checkTasks(); } void checkTasks(); } - Interfaces are allowed to have static methods with definitions. - static methods can be called without creating an instance of the class (or interface) first - static methods can only call other static methods directly. They must use local reference variables to call non-static methods. - static interface methods cannot call other interface methods that are not also static or default.

Multiple Inheritance

- If two interfaces contain default methods with same name, a class that implements both will have a conflict. - The super keyword can be used to specify which interface's implementation to use. Domesticated.super.doWork(); Worker.super.doWork();

Interfaces & Inheritance

- Interface methods do not have implementations and interface variables are static, so no states or behaviors are inherited. - Interfaces do not have a traditional type of inheritance, but often said to have a "type" inheritance. A class that implements and interface is polymorphically considered in instance of an interface. - Name interfaces as adjectives or verbs. An interface describes what behaviors a class should have, it provides none of its own. Example Interface names: Runnable, Serializable, Functional - A class can only extend one other concrete or abstract class. But a class can implement multiple interfaces. - Interfaces can extend other interfaces, with no limits on the quantity allowed. - Interfaces cannot extend classes, and do not implement other interfaces.


Related study sets

Chemical Bonding I: The Lewis Model

View Set

Chapter 47: Management of Patients With Gastric and Duodenal Disorders

View Set

Lesson 3 - Worksheet: Medical Terms

View Set

Social Work Research Mid-term exam

View Set

Unit Two: Protestant Reformation

View Set