Abstract in Java

Abstraction is a general concept that denotes the progress of modeling “real things” into programming language.

Abstraction is the process of selecting data to show only the relevant information to the user.

In simple terms, abstraction “displays” only the relevant attributes of objects and “hides” the unnecessary details.

For example, when we are driving a car, we are only concerned about driving the car like start/stop the car, accelerate/ break, etc. We are not concerned about how the actual start/stop mechanism or accelerate/brake process works internally. We are just not interested in those details.

What we are concerned about is the “abstract” view of these operations that will help us to propel the car forward and reach our destination. This is a simple example of abstraction.

Thus the car has all the mechanisms and processes in place but from the end user’s perspective, i.e. car driver’s perspective he/she will be interested only in the abstract view of these processes.

Abstraction reduces the programming efforts and thereby the complexity. An end-user using the application need not be concerned about how a particular feature is implemented. He/she can just use the features as required.

Thus in abstraction, we deal with ideas and not the events. This means that we hide the implementation details from the user and expose only the functionality to the end-user. Thereby the user will only know “what it does” rather than “how it does”.

In a nutshell, an abstract class can be described as shown below.

abstract class

An abstract method is a method preceded by an ‘abstract’ keyword without any implementation. An abstract method is declared inside an abstract class.

An abstract method is the one that makes a class incomplete as it doesn’t have an implementation. Hence when we include an abstract method in the class, naturally the class becomes incomplete.

We can use the abstract method by implementing it in a subclass i.e. a class inherits the abstract class and then implements or provides the code for all the abstract methods declared in the abstract class by overriding them.

Thus it becomes compulsory to override the abstract method in the subclass. If the abstract method is not implemented in the subclass as well, then we have to declare the subclass also as “abstract”.

The general declaration of the abstract method is:

abstract void methodName (parameter_list);

While writing the abstract method, we need to remember the following rules:

  • A class containing one or more abstract methods is an abstract class.
  • Certain other keywords should not be used with the abstract keyword.

following points need to be discussed –Thus, the following combinations are illegal in Java.

  1. final
  2. abstract native
  3. abstract static
  4. abstract private
  5. abstract synchronized
  6. abstract strictfp

an example of an abstract class and an abstract method.

//abstract class abstract class Bank{        abstract int getInterestRate();    }    //concrete classclass Citi extends Bank{        int getInterestRate(){return 7;}    }//concrete classclass HSBC extends Bank{        int getInterestRate(){return 6;}    }         class Main{        public static void main(String args[]){            Bank b;          b = new Citi ();      // concrete class object        System.out.println("Citi Rate of Interest is: "+b.getInterestRate()+"%");            b = new HSBC ();        // concrete class object        System.out.println("HSBC Rate of Interest is: "+b.getInterestRate()+"%");        }}   

Output:

Example of Abstract Method

In the above example, we have a Bank class. In this class, we have an abstract method, getInterestRate (). Then we declare two classes – ICICI and BOI that inherit from the Bank class. Both these classes implement the getInterestRate () method by returning their respective interest rates.

Then in the main method, we create a bank object. First, the bank object contains an object of ICICI class and displays the interest rate. Next, the BOI object is created and it displays the interest rate.

Thus, we can assume that the Bank class is a sort of a sketch or a structure that allows us to get an interest rate. From this structure, we can create as many concrete classes as we want, and then we can get the respective interest rates for each bank object (this is shown in the main method).

What Is The Use Of An Abstract Class In Java

Why do we use an abstract class when in reality it does not have any implementation of its own?

Along with the answer to the above question, we will also illustrate how to use an abstract class in the example that follows.

Let’s consider an example of Vehicles. We know that Vehicles can be of many types. We can have Cars, Scooters, bikes, mopeds, buses, etc. Though there are many types of vehicles, they have some properties or attributes that are common to all the vehicles irrespective of their types.

For example, each vehicle has a model, chassis number, color, etc. Each of them has functions like start, stop, accelerate, brake, etc. Now each vehicle will have the above properties and methods that are common to the others as well. At the same time as a vehicle user, we might not be interested in some aspects.

For example, if a person is driving a car, what he/she will be interested in is just to start and stop the vehicle or accelerate or brake the vehicle. He/she will not be interested in knowing how the vehicle starts or stop. We are only interested in the abstract working of the functions and not in their details.

Leave a comment

Design a site like this with WordPress.com
Get started