DEV Community

Cover image for ๐Ÿš€ Day 17 of My Automation Journey โ€“ Understanding Abstraction & Final Keyword in Java
bala d kaveri
bala d kaveri

Posted on

๐Ÿš€ Day 17 of My Automation Journey โ€“ Understanding Abstraction & Final Keyword in Java

Welcome back to Day 17 of My Automation Journey! โ˜•๐Ÿ’ป

In the previous days, I explored important Java concepts like:

  • ๐Ÿ” Encapsulation
  • ๐Ÿ“ฆ Packages
  • ๐Ÿ” Method Overriding
  • ๐Ÿ”‘ Access Modifiers

Today I learned another very important Object-Oriented Programming concept:

โœจ Abstraction

I also explored the final keyword and understood how Java statements like:

System.out.println();
Enter fullscreen mode Exit fullscreen mode

actually work behind the scenes.

Letโ€™s break everything down step by step.

๐Ÿงฉ What is Abstraction?

Abstraction means hiding implementation details and showing only essential features to the user.

In simple words:

๐Ÿ‘‰ The user only sees what the system does, not how it does it.

Real-life Example ๐Ÿง

When you use an ATM machine:

  • You click Withdraw
  • Enter amount
  • Cash is dispensed

But you donโ€™t see the internal banking logic.

That hidden logic is Abstraction.

โš™๏ธ How to Achieve Abstraction in Java

We achieve abstraction using the abstract keyword.

It can be used in:

| Level        | Description                |
| ------------ | -------------------------- |
| Class Level  | Creating an abstract class |
| Method Level | Creating abstract methods  |
Enter fullscreen mode Exit fullscreen mode

๐Ÿ— Abstract Class in Java

An abstract class:

โœ” Cannot be instantiated directly
โœ” Can contain abstract methods
โœ” Can contain normal methods
โœ” Can have constructors
โœ” Can have instance variables

Important note:

โ— Abstract methods do not have method body.

Example:

public abstract void withdraw();
๐Ÿง Example โ€“ ATM Abstraction

Letโ€™s understand abstraction using an ATM banking example.

Abstract Class

package abstractdemo;

public abstract class ATM {

    public abstract void withdraw();
    public abstract void balance();

    ATM(){
        System.out.println("constructor");
    }

    public void deposit() {
        System.out.println("deposit amount 100");
    }
}
Enter fullscreen mode Exit fullscreen mode

What we did here

  • withdraw() โ†’ abstract method
  • balance() โ†’ abstract method
  • deposit() โ†’ normal method

constructor โ†’ allowed in abstract class

So an abstract class can contain both abstract and non-abstract methods.

๐Ÿฆ Implementing Abstract Class

Now another class extends ATM and implements the methods.

package abstractdemo;

public class IciciBank extends ATM {

    @Override
    public final void withdraw() {
        System.out.println("withdrawn amount 1000");
    }

    @Override
    public void balance() {
        System.out.println("balance amount 5000");
    }

}
Enter fullscreen mode Exit fullscreen mode

Here we implemented the abstract methods.

Interesting thing here:

public final void withdraw()
Enter fullscreen mode Exit fullscreen mode

The final keyword prevents further overriding.

๐Ÿ‘จโ€๐Ÿ’ป User Class

Now we create the main class.

package abstractdemo;

public class User extends IciciBank {

    public static void main(String[] args) {

        User rajiv = new User();
        rajiv.balance();
        rajiv.withdraw();

    }

}
Enter fullscreen mode Exit fullscreen mode

Output

constructor
balance amount 5000
withdrawn amount 1000
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Œ What is System.out.println() in Java?

We use this statement almost everywhere.

But what does it really mean?

System.out.println("Hello");
Enter fullscreen mode Exit fullscreen mode

System

System is a class in Java.

It belongs to:

java.lang package

This package is automatically imported in every Java program.

out

outis a static object of PrintStream class.

It represents the standard output stream.

That means:

๐Ÿ‘‰ Output will be printed in the console.

println()

println() is a method of PrintStream class.

It prints the text and moves the cursor to the next line.

Example:

System.out.println("Hello");
Enter fullscreen mode Exit fullscreen mode

Output

Hello
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”’ Final Keyword in Java

The final keyword is used to restrict modification.

It can be applied to:

| Usage          | Meaning              |
| -------------- | -------------------- |
| Final Variable | Value cannot change  |
| Final Method   | Cannot be overridden |
| Final Class    | Cannot be inherited  |
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฎ Final Variable Example

final int age = 20;

Enter fullscreen mode Exit fullscreen mode

If we try:

age = 30;
Enter fullscreen mode Exit fullscreen mode

Java will throw a compile-time error.

Because final variables cannot be modified.

๐Ÿง  Why Do We Need Object-Oriented Programming?

OOP helps developers write better and scalable code.

Benefits

1๏ธโƒฃ Easier to understand
2๏ธโƒฃ Code Reusability (Inheritance)
3๏ธโƒฃ Flexibility (Polymorphism)
4๏ธโƒฃ Data Security (Encapsulation)

๐Ÿงฑ Four Pillars of OOP

Object-Oriented Programming is built on four main concepts.

1๏ธโƒฃ Inheritance
2๏ธโƒฃ Polymorphism
3๏ธโƒฃ Encapsulation
4๏ธโƒฃ Abstraction

All these concepts help developers build modular and maintainable applications.

๐Ÿ’ก My Key Learning Today

Today I understood:

โœ” What abstraction really means
โœ” How abstract classes work
โœ” Abstract classes can contain constructors and normal methods
โœ” How System.out.println() works internally
โœ” How the final keyword prevents modification

This concept is very important for framework design and large applications.

โœจ Final Thoughts

Abstraction is one of the most powerful concepts in Java.

It helps developers hide complexity and expose only necessary functionality, making applications cleaner and easier to maintain.

Day 17 helped me understand how abstraction works both in theory and with real code examples.

See you in Day 18 of My Automation Journey! ๐Ÿš€

๐Ÿค– A Small Note
I used ChatGPT to help structure and refine this blog while ensuring the concepts remain aligned with my trainerโ€™s explanations.

Top comments (0)