Java final classes, final methods and final data members

We learned about inheritance, method overriding and abstract classes These features are very powerful in designing an object oriented application. Sometimes we may want to make a rule that a method can not to be overridden by subclass or a superclass not to be inherited. This can be achieved by final keyword. This concept is kind of the opposite of abstract.

Methods with  final

If you add final keyword to a method, then you can not override this method in a subclass. Below example explains it, where we made show() method as final and tried to override it in its subclass. When we compile it throws an error.

/*
This is a simple Java program about Inheritance.
Call this file KH_final_Method.java.
*/
class Class1 {
	final void show(){
		System.out.println("Show in class 1");
	}
}

class Class2 extends Class1 {
	void show(){
		System.out.println("Show in class 2");
	}
}

class KH_final_Method {
	public static void main(String args[]) {
		Class2 ob = new Class2();		
		ob.show();		
	}
}

Download the code Run the code

Output:

KH_final_Method.java:15: error: show() in Class2 cannot override show() in Class
1
        void show()
             ^
  overridden method is final
1 error

Classes with final

If you add final keyword to a class, then you can not derive a subclass from it Below example explains it, where we made parent class as final and tried to create a subclass from it. When we compile it throws an error.

/*
This is a simple Java program about Inheritance.
Call this file KH_final_Class.java.
*/
class Class1 {
	final void show(){
		System.out.println("Show in class 1");
	}
}

class Class2 extends Class1 {
	void show() {
		System.out.println("Show in class 2");
	}
}

class KH_final_Class {
	public static void main(String args[]) {
		Class2 ob = new Class2();		
		ob.show();		
	}
}

Download the code Run the code

Output:

KH_final_Class.java:15: error: show() in Class2 cannot override show() in Class1

        void show()
             ^
  overridden method is final
1 error

Data members with final prefix

The final prefix works in a different way than in previous two ways for data members. When we prefix final keyword to any data member of a class then all these data members will become constants. That is we can not change their values in the program. Since we can not change their values, we need to give initial values to them at the time of definition itself. If you try to change value at a later stage, compiler throws the error.

In the below program we have created a class with one data member which is prefixed with final keyword. And we have commented a line where it changes its value. Try to uncomment that line and execute, then understand how our program behaves.

/*
This is a simple Java program about Inheritance.
Call this file KH_final_Variable.java.
*/
class Class1 {
	final int i = 10;
}

class KH_final_Variable {
	public static void main(String args[]) {
		Class1 ob = new Class1();		
		//ob.i = 11;		
		System.out.println("i : " + ob.i);		
	}
}

Download the code Run the code

Output:

i : 10

Output after uncommenting ob.i = 11;

KH_final_Variable.java:14: error: cannot assign a value to final variable i
                ob.i = 11;
                  ^
1 error
<<< Java abstract classes and abstract methodsAccess superclass constructor and superclass members >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .