Java abstract class

java abstract class

Java abstract class

Abstract class in java is used to implement partial abstraction. Abstraction is nothing but a concept in Object Oriented Programming which is used to hide the complexity and only useful information shown to the users. We will discuss about abstraction and abstract class in java. How they are related and how we can implement in our day to day projects.

java abstract class


Abstraction in java

To understand Abstraction, lets take an example first. 

You drive car, ride bike, use mobile phones, watch TV etc in your day to day life. But did you every think, how these things working internally? How your mobile phone is able to connect with the person who is miles away from you? How you are able to watch hundreds of channels on your TV? What’s going on internally? The truth is, we don’t care. We don’t care how they are working and which component is responsible to perform what operation. We just want to drive our car, watch channels on TV, call from our cell phone. What’s going inside is none of our business.

And that is Abstraction. Abstraction is the feature of Object Oriented Programming which hides complexity and represents things in simple and easy manner.  Internal processing is hidden from the outside world and only the basic functionality is shown.

Ways to implement abstraction in java

There is two ways you can implement abstraction in your java program as discussed below :

1. Abstract Class. (Used to implement partial abstraction)
2. Interface. (Used to implement full abstraction)

Abstract Class in java

You can create an abstract class with the help of “abstract” keyword in java. This keyword can be applied to only abstract class and abstract methods.

Abstract class in java is implementation of abstraction but it can not implement full abstraction. Abstract class is partial abstraction. You can have abstract as well as non abstract methods inside abstract class. But if a class is having single abstract method then that class must be abstract class.

Basic syntax of abstract class

Take a look at below syntax.

Note*: You don’t need to provide definition of abstract methods. Child class will have to provide the definition of the abstract methods as per their requirement.

abstract class ClassName {
         abstract public void methodName();
}


Some important things to remember about abstract class

Before you implement abstract class in your program, you first need to understand some basic concepts of abstract class in java as below :

  1. abstract class is created using abstract keyword.
  2. If a class is having at least one abstract method, then that class must be abstract class.
  3. You can not declare abstract methods as private. Private is not permitted with abstract keyword.
  4. You can not use final non access modifier with abstract methods.
  5. You can not make your abstract methods static. Static can not be used with abstract methods.
  6. You can create constructor of abstract class.
  7. You can create any number of non abstract methods and data members in your abstract class. Because abstract class does not implement fully abstraction. So abstract class is treated as normal class.
  8. An abstract class can implement interface and can provide definition of methods declared in that interface.
  9. Child class must implement all the abstract methods declared in abstract class.
  10. You can not use final keyword along with abstract keyword.
  11. JVM internally creates the object of abstract class. That’s why, inheritance is possible with the abstract class.


An example of abstract class

In this example, we will create a simple abstract class with some abstract methods. Child class will have to provide definition of all abstract methods of abstract class.

abstract class ATM {
       public void initialSetup() {
           // We will perform initial setup which is required an ATM to work.
       }
       abstract public void atmInterface();
       abstract public boolean performTransaction(double amount);
}

class Bank extends ATM {

      public void atmInterface() {
        // bank needs to provide their own interface.
        System.out.println("Interface setup is done.");
      }

      public boolean performTransaction(double amount) {
        // bank needs to provide implementation detail on how they perform transaction.
        System.out.println("Transaction Complete of : " + amount);
        return true;
      }
}

class RunATM {

      public static void main(String arg[]) {

        ATM atm = new Bank();
        atm.initalSetup();
        atm.atmInterface();
        atm.performTransaction(100);
      }
}

Look at the example above. This is the perfect example to understand abstraction or abstract class. See, ATM class is an abstract class with two abstract methods and one non abstract method which common to all child classes.

We do transaction on ATM machine almost every day. Everyone knows that different banks may have different interface to perform transaction. That’s is because each bank implement their own logic to perform ATM transactions.

As you can see in above example, we have one ATM abstract class with one non abstract method which is “initialSetup()”. This method is used to setup ATM machine to it’s working state. Why we made it non abstract, because setup is similar to each and every bank. So each bank needs to just call intialSetup() which is make ATM machine functional.

But ATM has two different abstract methods atmInterface() and performTransaction(). Why, because each has their own interface and the way they perform transactions. So we are leaving it on the bank.

Now, you have one question in your mind. Why do we need abstract class for this. We can simple create a parent class with some methods and child class will inherit them. So why do we need to create abstract class ?

You are creating a complex system here. People can loose their money, they can loose their wealth with a simple mistake. If you want your system to work properly, we need to set some rules. And if there are some rules, then they must be followed.

In above example, if you don’t create your class as abstract then child class will not take is seriously. They can override or may not override base class methods. Which can lead to serious implementation break down.

So we made it abstract so that child class will have to follow the design of the base class. They need to follow the rules of base class. Otherwise system will not be functional.

Why we can not make abstract class as final

As We can not create object of abstract class. Abstract class can only be inherited by the child class. Final is a sign to the JVM, that do not let anyone inherit this class. If, neither we can make the object of base class nor we can inherit it, then the class will simply be garbage.  That’s why java does not allow final keyword to be used along with abstract class.

Why we can not make abstract methods as final

Reason is same, final is a sign to the JVM, that do not let anyone override this method. But as per abstract class definition, all the abstract methods must be implemented in child class. That’s why, java does not allow final to be used along with abstract methods.

Why we can not make abstract methods as static

Static is related to class rather than the object. Child class inherits base class methods and data members by creating it’s object internally. But in the case of static, child class is not able to access it. So if overriding is not possible with static methods, then is does not make any sense to make abstract class as static. Because overriding must happen in case of abstract methods.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *