1.2 InterfacesHomepage  « Java6 Certification « 1.2 Interfaces

In this lesson we look at how to implement or extend interfaces.

Lets take a look at the points outlined at the Oracle Website for this part of the certification.

  • Section 1: Declarations, Initialization and Scoping

    • Develop code that declares an interface. Develop code that implements or extends one or more interfaces.

Interface Declarationgo to top of page Top

In this lesson we look at interfaces and the contracts we provide when using them. We create an interface using the interface keyword, instead of class in the definition. All interface methods are implicitly declared as public and abstract so there is no need to code these modifiers when creating the interface. Interface methods have no body and end in a semicolon. In fact you can think of an interface as a pure abstract class, because every method must be abstract. Lets start with some rules for declaring interfaces:

  • We must adhere to the contract specified in the interface when using it by providing concrete implementations of all methods within the interface.
  • Interface implementations must adhere to the rules for overrides which are discussed in the 1.4 Overloading & Overriding lesson.
  • We can use an abstract class to implement an interface and by doing this defer the contract implementation of the interface to the first concrete subclass of the abstract class.
  • An interface can extend one or more other interfaces, it does not implement them.
  • A class can implement one or more interfaces.
  • A class can extend another class and implement one or more interfaces, but the extend keyword must come first.

  Some code showing interface rules
interface A { }                        // OK
interface B { }                        // OK
interface C { }                        // OK
class D { }                            
class E { }                            
abstract class F { }                   
interface B implements A { }           // No, an interface can't implement another interface
interface B extends A { }              // OK, an interface can extend another interface
interface B implements D { }           // No, an interface can't implement a class
interface B extends C, A { }           // OK, an interface can extend multiple interfaces
class D implements E { }               // No, a class can't implement another class
class D implements A { }               // OK
class D implements A, B { }            // OK, a class can implement one or more interfaces
class E extends D implements A { }     // OK, we can extend a class and implement an interface
class E implements A extends D { }     // No, extend keyword must come first
abstract class F implements A { }      // OK, an abstract class can implement an interface

Related Java6 Tutorials

Objects & Classes - Class Structure and Syntax
OO Concepts - Interfaces

go to home page Java6 Tutor Homepage go to home page Top

All the Java6 Certification Declarations, Initialization and Scoping lessons are listed below. Click a link to go to that lesson.

1: Declarations

1.1 Classes, Packages & Imports

1.2 Interfaces

Interface Declaration

1.3 Abstract Classes

1.4 Variables & Scope

1.5 Overriding & Overloading

1.6 Constructors / Nested Classes