1.3 Abstract ClassesHomepage  « Java6 Certification « 1.3 Abstract Classes

In this lesson we investigate abstract classes and how to declare and extend them.

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

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 Java6 Tutorials

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

<<  1.2 Interfaces                    1.4 Variables & Scope  >>

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

1.3 Abstract Classes

Abstract Classes

1.4 Variables & Scope

1.5 Overriding & Overloading

1.6 Constructors / Nested Classes