Final Classes, Methods, and Variables - Java Object Oriented Design

Java examples for Object Oriented Design:final

Introduction

The final modifier is used with classes, methods, and variables to tell that they will never change.

It has different meanings.

A final class cannot be subclassed.

A final method cannot be overridden by any subclasses.

A final variable cannot change in value.

Final Variables

Final variables are called constants because they do not change in value at any time.

The following statements are examples of declaring constants:

 
public static final int CONST = 6; 
static final String TITLE = "test"; 
 
 

Final Methods

Final methods never can be overridden by a subclass.

public final void getSignature () { 
    // body of method 
} 
 

Final Classes

To disable a class to have subclass, use the final modifier in the class's declaration.

public final class ChatServer { 
    // body of method 
}

Related Tutorials