Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry facebook
  • Androidberry twitter
  • Androidberry google plus

Java Tutorials


Basic Introduction

Decision Making

Looping

Classes and Objects

Java Class
Java Object
Java Object Array
Java Nesting of Classes
Java Methods
Java Method Overloading
Java Constructor
Java Constructor Overloading
Java This Keyword
Java Garbage Collection
Java Static Keyword
Java Arrays
Java String
Java StringBuffer
Java String vs StringBuffer
Java Vector Class
Java Vector vs Array
Java Wrapper Classes

Inheritance

Java Inheritance
Java Single Inheritance
Java Super Keyword
Java Multiple Inheritance
Java Hierarchical Inheritance
Java Multilevel Inheritance
Java Method Overriding
Java Abstract Classes,Methods
Java Final Variables, Methods
Java Dynamic Dispatch Methods
Java Visibility Controls

Interfaces

Java Interface
Java Class vs Interface
Java Multiple Inheritance
Java Nested Interface

Packages

Java Packages
Java Built in packages
Java Package Access Specifiers

Exception Handling

Multithreading

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java Final Keyword


The final keyword allows us to prevent same thing defining again or modifying later in the program.

Uses of final keyword

The keyword final has 3 uses for making

Final Keyword in java
  1. Final Variable
  2. Final Methods
  3. Final Classes

1. Final Variable

By declaring a variable with final keyword the access is restricted for making inheritable. The value of final variable never be changed in the program. They always behave like a class variables and they do not take any space on indivisual object of the class.

Syntax

final type variable_name = constant_value;

Example

final int max = 300;

Program Example

Program to show use of final variables in the program.

class Base
{
    final int max = 300;
    Base()
    {
       max = 400;    // produces an error   
    }
}

class Derived extends Base
{
    Derived()
    {
       max = 500;    // produces an error
    }
    
    public static void main(String args[])
    {
       Derived d1 = new Derived();
    }
}
	

Output

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The final field A.max cannot be assigned
The final field A.max cannot be assigned

In above program we have declared final variable as max in the Base class. We have tried to change the value of it inside the Base class as well in derived class, Produces same error.


2. Final Methods

The method which is defined as final it ensures that the functionality defined in the method will never be changed.

Syntax

final type method_name()
{
    ...
    ...
}

Example

final int show()
{
    final method code
    ...
}

Program Example

class Base
{
    final void display()
    {
        System.out.println("I'm from base class");
    }
}

class Derived extends Base
{
    void display() // produces an error
    {
        System.out.println("I'm from Derived class");
    }
    public static void main(String args[])
    {
        Derived d1 = new Derived();
        d1.show();
    }
}
	

Output

Error : Cannot override the final method from Base

In above example, we have declared the display method as final in the Base class. When we tried to redefine in the Child class then we get an error.


3. Final Class

To prevent the classes being further subclasses is called as a final class. It is Necessary for the security reasons in java.

Syntax

final class className
{
     ...
     ...   
}

Example

final class First
{
    ...
    ...
}

Program Example

final class Base
{
    void Message()
    {
        System.out.println("I'm from base class");
    }
}

class Derived extends Base // produces an Error
{
    void Message()
    {
        System.out.println("I'm from Derived class");
    }
    public static void main(String args[])
    {
        Derived d1 = new Derived();
        d1.Message();
    }
}
	

Output

Error : The type Derived cannot subclass the final class Base

<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo