1.2 Interfaces / Abstract ClassesHomepage  « Java5 Certification « 1.2 Interfaces / Abstract Classes

In this lesson we start by seeing how to implement or extend interfaces. After this we investigate abstract classes and how to declare and extend them.

n this lesson we look at the rules for declaring and implementing or extending interfaces. After this we investigate how to declare and extend an abstract class and the rules for doing this

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. Develop code that declares an abstract class. Develop code that extends an abstract class.

Interface Declarationgo to top of page Top

In the first part of 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, which we will talk about below, 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


Abstract Classesgo to top of page Top

Abstract classes are used when the object in question is too abstract to make a concrete class. We saw an examples of this in the Abstraction lesson when we made the Vehicle and Truck classes abstract and make concrete subclasses such as Car and HGV. Of couse we could have abstracted these subclasses further to actual car and hgv types but that would have over complicated the focus of that lesson. There are a few rules involved when creating abstract classes:

  • Abstract classes can never be instantiated.
  • The whole purpose of an abstract class is to be subclassed, therefore an abstract class can never have a modifier of final.
  • If any method on a class is marked as abstract, then the class must also be marked as abstract.
  • An abstract class can contain abstract and non abstract methods.
  • An abstract class can extend an abstract class.



/*
  Some code showing abstract rules
*/
abstract class A { }           // OK

abstract class B extends A { 
    A a = new A();
}                              // No, you can never instantiate an abstract class

abstract final class C { }     // No, cannot use both abstract and final

class D {
    abstract void methodA(); 
}                              // No, if any method is marked as abstract, the class must be abstract

abstract class E {
    abstract void methodA(); 
}                              // OK

abstract class F {
    abstract void methodA(); 
    void methodB() {} 
}                              // OK, an abstract class can contain abstract and non abstract methods

abstract class G extends F {}  // OK, an abstract class can extend an abstract class


Related Java5 Tutorials

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

go to home page Java5 Tutor Homepage go to home page Top





All the Java5 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 / Abstract Classes

Interface Declaration
Abstract Classes

1.3 Variables & Scope

1.4 Overriding & Overloading

1.5 Constructors / Nested Classes