Today we are going to explore
the concept of oops in java
"abstraction"
by the end of this blog U have a clear idea about this concept
let's begin
Before dive in to
before directly jumping into abstraction concept
We have set some essential fuondation for it
so let's know about the
"final" --- keyword
The final keyword in java
In Java, the final keyword is used to restrict changes and make code more secure and predictable.
It can be applied to variables, methods, and classes to prevent modification, overriding, or inheritance.
This helps in creating constant values, stable methods, and immutable classes.
- Final variable cannot be changed once assigned
- Final method cannot be overridden
- Final class cannot be inherited (we can't create child class)
look at this
here we are reassigning variable final
class Parent {
static final int CAPACITY = 4;
public static void main(String[] args) {
CAPACITY = 5;
}
}
Output:
it throws a compile time error
we cant change it again when intialization is done already
Local Final Variable
A local final variable must be assigned once.
class Geeks {
public static void main(String[] args) {
final int i;
i = 20;
System.out.println(i);
}
}
Output
20
here we got 20 bcz the variable with final keywrod must be intialized once
Final class
A class declared as final cannot be extended.
A final class cannot be inherited, meaning no other class can extend it.
This is commonly used for security or when the implementation should remain unchanged, such as in core classes like String.
Prevents inheritance, ensuring the class behavior cannot be modified
Commonly used for immutable and secure classes
final class A {
// fields and methods
}
// Illegal
class B extends A { }
Here the final class A can't be extended
Final classes are useful when creating immutable classes such as String or wrapper classes.
Final Method:
When a method is declared with final keyword, it is called a final method in Java.
A final method, on the other hand, can be inherited but cannot be overridden by subclasses, ensuring that the original implementation remains intact.
- Cannot be overridden in subclasses
- Helps maintain consistent behavior across all derived classes
class A {
final void m1() {
System.out.println("Final method");
}
}
class B extends A {
void m1() { } // compile-time error
}
OUtput:
here m1() method can't be overriden
Advantages of final Keyword
- Supports immutability by preventing reassignment
- Helps compiler and JVM optimize code in some scenarios
- Makes behavior predictable since values or methods stay unchanged
- Prevents accidental or unauthorized modification of critical logic
- Preserves API contracts by avoiding unwanted overriding
Hope u got a clear vision about the "final" keyword
Gettin to Abstraction concept:
Abstraction in Java is the process of hiding internal implementation details and showing only essential functionality to the user.
It focuses on what an object does rather than how it does it.
- It hides the complex details and shows only essential features.
- Abstract classes may have methods without implementation and must be implemented by subclasses.
- By abstracting functionality, changes in the implementation do not affect the code that depends on the abstraction
We can achieve abstraction in java
in two way
One is by using interface
and
another one is by using abstract
in this blog
i am going to achieve this by Abstract class
let see example for this
public class Parent{
public void study();
public static void main(String[] args){
Parent parent=new Parent();
parent.study();
System.out.println();
}
}
when we compile this
the output will be
it clearly says missing method body
or
declare a method as a abstract
So now let give as abstract for the method
public class Parent{
public abstract void study();
public static void main(String[] args){
Parent parent=new Parent();
parent.study();
System.out.println();
}
}
here again one more error
let's read it properly
now it's too clearly says that
if the class have a one abstract method
the same class must have written as abstract
So let's declare the class with abstract keyword
public abstract class Parent{
public abstract void study();
public static void main(String[] args){
Parent parent=new Parent();
parent.study();
System.out.println();
}
}
output:
in this too
the error clearly says
if the class is abstract
the class shouldn't have the object
(cannot be instantiated)
in the same class only the object creation is not possible right?
so let's inherit from another class
now create a child class to access study()
Parent.java file
public abstract class Parent{
public abstract void study();
public void spending(){
System.out.println("parent spending ");
}
public static void main(String[] args){
System.out.println();
}
}
Child.java file
public class Child extends Parent{
public static void main(String[] args){
Child child = new Child();
child.spending();
System.out.println();
}
}
output
now error says that
in Child class you are accesing the abstract method from the parent class
to access it u must declare your class as an abstract
it simply says
-> u must change your class to abstract
ok let's change our class with the abstract
public abstract class Child extends Parent{
public static void main(String[] args){
Child child = new Child();
child.study();
System.out.println();
}
}
now again same error occurs
it say like before
for the abstract class shouldn't have a object
without object we can't access the static method from another class
i think the java is kidding with we
Fun time
the code is continously playing with we
so now let's play with this
we have learnt about final keyword right
let's implement it now
the question comesup can we declare final and abstract keyword together is this possible
public abstract class Parent{
public final abstract void study();
public void spending(){
System.out.println("parent spending ");
}
public static void main(String[] args){
System.out.println();
}
}
output:
here the error
says
these two combinations of modifiers cannot be declared same time
here the when i saw illegal combination in the error
it directly remember one movie dialouge
IN singam movie when the danny is getting arrest
surya says that dialogue to danny
"ILLEGAL ENTRY"
https://www.youtube.com/shorts/QW4tmQYowsE?t=107&feature=share
Sudden stuck
And i stucked in this
why these two keywords cannot declared in same
so
i asked to intelligence
for clarification
Clarification
the intelligence said that
more thing about it
- In Java, final and abstract cannot be used together on the same method because they mean opposite things.
What abstract means
An abstract method:
public abstract void study();
- Has no implementation.
- Must be overridden by a subclass.
What final means
A final method:
public final void study() {
System.out.println("Studying");
}
- Has an implementation.
- Cannot be overridden by subclasses.
Why they conflict
abstract says:
"Subclasses must override this method."
final says:
"Subclasses cannot override this method."
These requirements contradict each other.
It's like saying:
"This door must be opened."
"This door must never be opened."
So Java reports an error.
Rule to remember
abstract = must override / must implement
final = cannot override / cannot change
Only way
By declaring a study() method
in the Child class
The parent class is just abstracted that method
but the Child method can Override it
if didn't know about method override
u can check my blog
link --->
Code:
public class Child extends Parent{
public void study(){
System.out.println("Studying Info Tech");
}
public static void main(String[] args){
Child child = new Child();
child.study();
System.out.println();
}
}
Output:
so the finaly we achieved this by using method overriding
however we've made so many error in this
by reading the error we achieved it
Takeaway:
The simple to remember the concept of abstraction is
by creating any using abstract
we can't access it in same class
but
we can access it from another class using inherit
and by method overriding
so before know about abstraction
we must know about inheritance , method overriding, static and non static and more basic concepts
References:
Final keyword :
https://www.geeksforgeeks.org/java/final-keyword-in-java/
Abstraction in java:











Top comments (1)
Nice Structural Write up!, Thanks for your Blog...