01 logo

Abstraction in Java

Java Abstraction

By ShravaniPublished 3 years ago 3 min read
1
Java Abstraction

Abstraction is hiding internal implementation and just highlighting the setup services that we are offering. Data abstraction is the process of hiding certain details and showing only essential information to the user.

The abstract keyword is a non-access modifier, used for classes and methods:

Let us understand this with a real world example:-

Example 1: As in the car case,relevant parts like stearing, horn, accelerator, break ets are shown to driver because they are neccesary for driver,but the driver need not to know about the internal functioning like engine etc...

Example 2: Another real life example of an abstraction is ATM machine. All are performing operations on the ATM machine like cash withdrawal, money transfer, retive mini-statement, balance enquiry etc.... but we don't know the internal details / working about ATM.

Thus showing relevant data to the users and hiding internal implementation from the user is abstraction.

Data abstraction can be used to provide security for the data from the unauthorized methods.

Now let us understand what is abstract method. Lets us understand this too with with an example:-

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon)

Class Car {

no.of wheels;

void start(){

System.out.println("car is running");

}

}

Class Scooter{

no.of wheels;

void start(){

System.out.println("Scooter is running");

}

}

Here I have created and created same variable and same method in each clases.what can I do here is I can create one abstract class so,that both the clases can extend that class like this way we can make code reusibility.

abstract Class Vehicle{

no.of wheels;

abstract void start();

}

In the above class I have created one variable and one method but I haven't provided any details to variable and method .so,here the method without any body is called abstract method.

A method without any body(no implementation) is know as Abstract method.

Now let us understand what is abstract class ?

A class which is declared as abstract keyword in java is know as abstract class. An abstract class can have abstract (without body) and non-abstract method(methods with body). Abstract classes cannot be instantiated, but they can be subclassed.

Ways to archieve abstraction:-

  • Abstract class(0 to 100%)
  • interfaces(100%)

Points to remember :-

  • A method without body is know as abstract method.
  • An abstract class can have both abstract and non-abstract methods.
  • If a class has an abstract method,then the class should be declared an abstract as well.
  • If a regular class extends an abstract class, than the class must have to implement all the abstract methods of abstract parent class or it has to be declared abstract as well.
  • Abstract class cannot be initiated means we can't create an object of abstract class.
  • Abstract class can be subclassed.

Example of an Abstract class that has an abstract method:

abstract Class Vehicle{

no.of wheels;

abstract void start();

}

Class Car extends Vehicle{

no.of wheels = 4;

void start(){

System.out.println("car is running");

}

}

Class Scooter extends Vehicle{

no.of wheels=2;

void start(){

System.out.println("scooter is running");

}

}

Class Solution{

public static void main(String[] args){

Car c1=new Car();

Scooter s1=new Scooter();

c1.start();

s1.start();

}

}

Output:

car is running

scooter is running

Abstraction is a general concept which you can find in the real world as well as in oop's languages.Any object in the real world ,like your coffee machine ,ATM machine, or class in your current software project , that hides internals details provide an abstraction.

Thanks for reading! keep coding, keep learning.

If u like this article then hit the like button and share others to help them.

how to
1

About the Creator

Shravani

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.