Login    New User    Forgot Password    Help      

Final Abstraction

Java Tutorials

In Javafinal keyword is applied in various context. The final keyword is a modifier means the final class can't be extended, a variable can't be modified, and also a method can't be override.

Final classes

The class declared as final can't be subclass or extend. The final class declared as follows :

public final class MyFinalClass

final methods

The final method  can be declare as follows:

public final String convertCurrency()

The final method can't be override in a subclass.

Final fields

The field declared as final behaves like constant. Means once it is declared, it can't be changed. Before compiling,only once it can be set; after that you can't change it's value. Attempt to change in it's value lead to exception or compile time error.

You can declare the final fields as :

public final double radius = 126.45;

public final int PI = 3.145; 

Java Final Keyword

  • A java variable can be declared using the keyword final. Then the final variable can be assigned only once.
  • A variable that is declared as final and not initialized is called a blank final variable. A blank final variable forces the constructors to initialise it.
  • Java classes declared as final cannot be extended. Restricting inheritance!
  • Methods declared as final cannot be overridden. In methods private is equal to final, but in variables it is not.
  • final parameters – values of the parameters cannot be changed after initialization. Do a small java exercise to find out the implications of final parameters in method overriding.
  • Java local classes can only reference local variables and parameters that are declared as final.
  • A visible advantage of declaring a java variable as static final is, the compiled java class results in faster performance.

The abstract Keyword 

Abstract keyword used for method declaration declares the methods without implementations. 

Abstract class in java have abstract methods that is not implemented in abstract class, but implemented  in subclasses in java program. If  the class in java program is not required to get instantiated than that class use the abstract keyword but this class rather is available for other classes to extend by other classes. 

Abstract keyword will be used in  method declaration to declare that method without providing the implementation in that java program. 

In other words we can say that, it formally unfinished class as well as  method,  that marked with the help of keyword abstract. 
Defining abstract is a way of preventing someone from instantiating a class that is supposed to be extended first. In java program abstract class is deliberately missing similar to like an interface which will missing all method bodies in the program. Abstract class provides a way to extend an actual class. We will not use new on abstract classes but will use abstract references in the java program, that always point to objects of the class that extends an abstract class.

In java program for practical use of an abstract class, we will define a non-abstract class that extends an abstract one. This will use any of the inherited non-abstract methods.

Most of the time abstract class may extend another abstract class. In that condition it need not implement all in the non-abstract methods. An abstract keyword used both on classes and methods.  In case of class declared with an abstract keyword may not be instantiated that is the only thing that abstract keyword doing. 


// OVERRIDING and Final keyword
// Runtime polymorphism
/* A Simple Abstract class
Abstract class cannot be initialised in maim.  Abstract cannot have its own body. Constructors cannot be declared as abstract final or any other type
Abstract shows only a structure type

abstract class base
int l;
int b;
int h;
int a;
        abstract void calarea(int x,int y ,int z);


        /* Method declared as final cannot be overridden . */               
        final void showdata()


class square extends base
        void calarea(int x,int y ,int z)
        void area(int x)
        System.out.print("Area of Square is ");


class rectangle extends square
        void area(int x,int y)
        l= x;
        b= y;
        System.out.print("Area of Rectangle is ");

class solid extends square
        void area(int x,int y,int z)
        System.out.print("Volume of solid is ");

class herit7
public static void main(String args[])

square ob1=new square();

rectangle ob2=new rectangle();

solid ob3=new solid();


Java Tutorials