Java - Interfaces

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

2 basic design purposes of tagging interfaces

- Creates a common parent -Adds a data type to a class

Implementing Interfaces

- When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. -If a class does not perform all the behaviors of the interface, the class must declare itself as abstract.

an interface may also contain

- constants, default methods, static methods, and nested types. -Method bodies exist only for default methods and static methods.

An interface

- is a reference type in Java -is similar to class. -is a collection of abstract methods

An interface is similar to a class in the following ways

-An interface can contain any number of methods. -is written in a file with a .java extension, with the name of the interface matching the name of the file. -The byte code of an interface appears in a .class file. -Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

Interfaces have the following properties

-An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface. -Each method in an interface is also implicitly abstract, so the abstract keyword is not needed. -Methods in an interface are implicitly public.

an interface is different from a class in several ways, including −

-You cannot instantiate an interface. -An interface does not contain any constructors. -All of the methods in an interface are abstract. -An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final. -An interface is not extended by a class; it is implemented by a class. -An interface can extend multiple interfaces

Writing an interface

-is similar to writing a class -But a class describes the attributes and behaviors of an object. -And an interface contains behaviors that a class implements.

Interfaces Example

/* File name : Animal.java */ interface Animal { public void eat(); public void travel(); }

When implementation interfaces : extend only one class

A class can _______ , but implement many interfaces.

When implementation interfaces : more than one interface

A class can implement _______ at a time.

implements keyword

A class uses the _______ to implement an interface. -The _______ appears in the class declaration following the extends portion of the declaration public class MammalInt implements Animal { ... }

overriding methods defined in interfaces : implementation class

An _________ itself can be abstract and if so, interface methods need not be implemented.

When implementation interfaces : can extend another interface

An interface _________, in a similar way as a class can extend another class.

Extending Interfaces

An interface can extend another interface in the same way that a class can extend another class.

Tagging Interfaces

An interface with no methods example: For example, the MouseListener interface in the java.awt.event package extended java.util.EventListener

Creates a common parent

As with the EventListener interface, which is extended by dozens of other interfaces in the Java API, you can use a tagging interface to create a common parent among a group of interfaces. For example, when an interface extends EventListener, the JVM knows that this particular interface is going to be used in an event delegation scenario.

overriding methods defined in interfaces : signature , return type or subtype

The ____ of the interface method and the same ____________ should be maintained when overriding the methods.

extends keyword

The ________ is used to extend an interface, and the child interface inherits the methods of the parent interface.

Adds a data type to a class

This situation is where the term, tagging comes from. A class that implements a tagging interface does not need to define any methods (since the interface does not have any), but the class becomes an interface type through polymorphism.

overriding methods defined in interfaces : Checked exceptions

________ should not be declared on implementation methods other than the ones declared by the interface method or subclasses of those declared by the interface method.

Unless the class that implements the interface is abstract

all the methods of the interface need to be defined in the class.

Extending Multiple Interfaces

can extend more than one parent interface. -The extends keyword is used once, and the parent interfaces are declared in a comma-separated list. example : public interface Hockey extends Sports, Event

A class implements an interface

inheriting the abstract methods of the interface.

interface keyword

is used to declare an interface /* File name : NameOfInterface.java */ import java.lang.*; // Any number of import statements public interface NameOfInterface { // Any number of final, static fields // Any number of abstract method declarations\ }


Set pelajaran terkait

Ch.2 Open Stax Westcott Courses Physiology

View Set

Live Virtual Machine Lab 10.2: Module 10 Network Security Concept Fundamentals

View Set

PrepU hapter 15: Hospital Nutrition: Defining Nutrition Risk and Feeding Clients

View Set