ch 9 interfaces

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

Differences between interfaces and abstract methods

abstract classes and interfaces can contain abstract methods and static constants, and neither can be instantiated. However, an abstract class can also contain other types of fields, and it can define static and regular methods. A class can only inherit one other class—abstract or not—but it can implement more than one interface.

Advantages of an interface

• A class can only directly inherit one other class, but it can directly implement multiple interfaces. • Any object created from a class that implements an interface can be used wherever the interface is accepted.

How to use inheritance with interfaces

• An interface can inherit one or more other interfaces by specifying the inherited interfaces in an extends clause. • An interface can't inherit a class. • A class that implements an interface must implement all the methods declared by the interface as well as all the methods declared by any inherited interfaces unless the class is defined as abstract. • A class that implements an interface can use any of the constants declared in the interface as well as any constants declared by any inherited interfaces. public interface InterfaceName extends InterfaceNamel[, InterfaceName2]...

Advantages of an abstract class

• Can use instance variables and constants as well as static variables and constants. Interfaces can only use static constants. • Can define regular methods that contain code as well as abstract methods that don't contain code. An interface can only define abstract methods. • Can define static methods. An interface can't.

How to code an interface

• Declaring an interface is similar to declaring a class except that you use the inter face keyword instead of the class keyword. • All methods are automatically declared public and abstract, and all fields are automatically declared public, static, and final. Although you can code the public, abstract, and final keywords, they're optional. • Interface methods can't be static.

How to inherit a class and implement an interface

• If a class inherits another class that implements an interface, the subclass automatically implements the interface. • If a class inherits another class that implements an interface, the subclass has access to any methods of the interface that are implemented by the superclass and can override those methods. ->public class SubclassName extends SuperclassName implements Interfacel [, Interface2]...{}

How to implement an interface

• To declare a class that implements an interface, you use the implements keyword. Then, you provide an implementation for each method defined by the interface. • A class that implements an interface can use any constant defined by that interface. public class ClassName implements Interfacel[, Interface2]...{}


Set pelajaran terkait

Exam 2 Corporate Finance conceptual questions

View Set

Interview with Cristiano Ronaldo

View Set

España - La Vida y Las Costumbres

View Set

Oxygen Hemoglobin Dissociation Curves

View Set

PrepU ch 39 Management of Patients with Oral and Esophageal Disorders

View Set