Java With Us
Home   |   Tutorial   |   Programs  

Java Tutorial

Introduction to Java
Hello World Program
Variables and Data types
More about data types
Displaying text using print and println
Displaying text using printf
Java Comments
Naming conventions for Identifiers
Mathematical operations in Java
Taking input from the user

Classes and Objects
Introduction to object oriented programming
The constituents of a Class
Creating objects and calling methods
Get and Set Methods
Default constructor provided by the compiler
Access Specfiers
Scope and lifetime of Variables
Call by value and Call by Reference

A few more topics
Casting
Class as a reference data type
Constants or literals
Final variables
Increment and decrement operators
Manipulating Strings
Operators
Overloading constructors and methods
Static methods and variables
The Java API
The Math class
this keyword
Wrapper classes

Control Structures
Control Statements
Repetition statements
Nested loops
Formulating algorithms
Branching Statements

Arrays
Arrays introduction
Processing arrays using 1oops
Searching and sorting arrays
Array of objects
Multi dimensional arrays
Taking input as command line arguments
Using ellipsis to accept variable number of arguments

Inheritance
Inheritance introduction
Relation between a super class and sub class
Final classes and methods
The protected access specifier
Class Object

Polymorphism
Introduction
Interfaces
Packages
Abstract classes and methods

Exception handling
Exception handling introduction
Exception hierarchy
Nested try catch blocks
Throwing exceptions


Final Classes and Methods

Inheritance is surely one of the highly useful features in Java. But at times, it may be desired that a class should not be extendable by other classes to prevent exploitation. For such purpose, we have the final keyword. We have already seen even what final variables are. Final classes and methods are also similar. A class declared as final cannot be extended while a method declared as final cannot be overridden in its subclasses. A method or a class is declared to be final using the final keyword. Though a final class cannot be extended, it can extend other classes. In simpler words, a final class can be a sub class but not a super class.

final public class A {
    //code
}

The final keyword can be placed either before or after the access specifier. The following declaration of class A is equivalent to the above.

public final class A {
    //code
}

To make it more clear, there should be no specifiers between a class name and the keyword class. All other specifiers may be placed to the left of the keyword class in any order.

Final methods are also declared in a similar way. Here too, there should be no specifiers stated between the return type and the method name. For example, to make a static method declared with the public access specifier final, the three specifiers, public, static and final may be placed in any order to the left of the return type.

public final void someMethod() {
    //code
}

When we attempt to extend a final class or override a final method, compilation errors occur.

class B extends A { // compilation error, A is final
}

We will now look into a small trivial example to understand the necessity of final classes and methods. Consider that you are a developer writing classes for use by others. You have two classes named Car and Vehicle defined in the following way and of course with may other methods of your own.

public class Car {

    public void move() {
        System.out.println("Moving on road");
    }
}

class MoveAVehicle {

    public static void move(Car c) {
        c.move();
    }
}

Now, since the class Car is not final, other people may extend it to their own classes. Following is an example:

class Aeroplane extends Car {

    public void move() {
        System.out.println("Moving in air");
    }
}

This is logically wrong. An aeroplane is definitely not a Car. And a statement like the following would print absurd data on the screen.

MoveAVehicle( new Aeroplane() );

In order to avoid such illogical extending of classes; classes and methods may be marked as final. The example cited above is just illustrative. The actual scenario in real world programming is much more complex.

Privacy Policy