SCJP Study Guide:
Declarations, Initialization and Scoping


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend


Previous Next vertical dots separating previous/next from contents/index/pdf Contents
XyzWs SCJP Study Guide: Class

Classes


Definition


A class is a blueprint that defines the variables and the methods common to all objects of a certain kind.

In the Java™ programming language, a type that defines the implementation of a particular kind of object. A class definition defines instance and class variables and methods, as well as specifying the interfaces the class implements and the immediate superclass of the class. If the superclass is not explicitly specified, the superclass will implicitly be Object.

Class declarations define new reference types and describe how they are implemented.

Class Declaration

A class declaration looks like this:

[ClassModifier] class ClassName [ extends SuperClassName ] [ implements InterfaceNames ] {
. . .
}

The items between [ and ] are optional. A class declaration defines the following aspects of the class:

  • ClassModifier declare whether the class is public, abstract, or final
  • A compile-time error occurs if the same modifier appears more than once in a class declaration
  • A compile-time error occurs if a class is declared both final and abstract
  • ClassName sets the name of the class you are declaring
  • SuperClassName is the name of ClassName's superclass
  • InterfaceNames is a comma-delimited list of the interfaces implemented by ClassName

Of the items in a class declaration, only the class keyword and the class name are required in a class declaration. The other items are optional. If you do not make an explicit declaration for the optional items, the Java compiler assumes certain defaults: a non-final, non-public, non-abstract, subclass of java.lang.Object that implements no interfaces.

The java.lang.Object class is the top of the class hierarchy. All classes are descendants from this class and inherit methods from it. Useful methods inherited from Object include toString, equals, clone, getClass, wait, notify, and notifyAll.

Class Modifiers


A Class Modifier is one of the following:

Annotation  public  protected  private  abstract  static  final  strictfp

More than one of the class modifiers can be used together. If two or more class modifiers appear in a class declaration, then it is customary, though not required, that they appear in the order consistent with that shown above.

Not all modifiers are applicable to all kinds of class declarations.

  • The access modifier public pertains only to top level classes and to member classes.
  • The access modifiers protected and private pertain only to member classes within a directly enclosing class declaration.
  • The access modifier static pertains only to member classes.
  • A compile-time error occurs if the same modifier appears more than once in a class declaration
  • A compile-time error occurs if a class is declared both final and abstract

A special class modifier is called defalut or package modifier that is no modifer at all. The public, protected, private, and default (package) modifers are the accessibility modifiers.

final Classes

A class can be declared final if its definition is complete and no subclasses are desired or required. A compile-time error occurs if the name of a final class appears in the extends clause of another class declaration; this implies that a final class cannot have any subclasses. A compile-time error occurs if a class is declared both final and abstract, because the implementation of such a class could never be completed.

For example, the SampleExtendsFinalClass cannot subclass the final class SampleFinal. You will get a compile-time error.

final class SampleFinal {
  ...
}

class SampleExtendsFinalClass extends SampleFinal {
  ...
}

Because a final class never has any subclasses, the methods of a final class are never overridden. All methods in a final class are implicitly final, since there's no way to override them. So the compiler has the same efficiency options as it does if you explicitly declare a method final.

abstract Classes

A class is called abstract if it has at least one abstract method. What is an abstract method? An abstract method is a method without implementation. In this way, an abstract class can define a complete programming interface for its subclasses but allows its subclasses to fill in the implementation details of those methods. The following code is an example of an abstract class:

 
public abstract class SampleAbstractClass {
    public abstract void doSomething();
}

An abstract class is not required to have an abstract method in it. The following code is defined an abstract class without abstract method(s):

public abstract class SampleAbstractClass {
    public void doSomething() { ... };
 
}

Any class that has an abstract method in it or that does not provide an implementation for any abstract methods declared in its superclasses or implemented interfaces must be declared as an abstract class.

pubic class SampleAbstractClass {
    public abstract void doSomething(); //compile time error occurs
}
 
or 
 
public abstract class AbstractBase {
    public abstract void doSomething(); 
}
 
public class SampleAbstractClass extends AbstractBase {
    //without implement doSomething.
}

You will get compile time error in the above two examples. To avoid getting compile time error, the keyword abstract has to be place in the front SampleAbstractClass class. When a class inherits from an abstract class, it must implement every abstract method defined by the abstract class. Implementation is possible by overriding the methods specified in the abstract class. Otherwise, this class must be defined as abstract class.

An abstract method can not have a method's body even empty body such as "{}", otherwise a compile error occurs.

 
public abstract class SampleAbstractClass {
    public abstract void doSomething() {}; //Compile time error occurs.
}

Abstract classes can not be instantiated. Any attempt to instantite an abstract class, the compile time error occurs. An abstract class is designed to act as a base class (to be inherited by other classes). Abstract class is a design concept in program development and provides a base upon which other classes are built.

 
public abstract class SampleAbstractClass {
    public abstract void doSomething();
 
    public static void main(String[] args) {
        A a = new A();   //Compile time error occurs
    }
 
}

An abstract class can contain instance member variables.

If an abstract class contains only abstract method declarations, it should be implemented as an interface instead. Like interfaces, abstract classes can specify methods that must be implemented in inheriting classes. Unlike interfaces, a class can inherit only one abstract class.

strictfp Classes

The effect of the strictfp modifier is to make all float or double expressions within the class declaration be explicitly FP-strict. This implies that all methods declared in the class, and all nested types declared in the class, are implicitly strictfp.

Note also that all float or double expressions within all variable initializers, instance initializers, static initializers and constructors of the class will also be explicitly FP-strict.


Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |