Loading...

Introduction to Abstract Class In Java with Examples

Introduction to Abstract Class In Java with Examples

Introduction

Java is an object-oriented programming language, used for distributed environments on the internet. It’s a high-level language that is easy to read and understand, popularly used in mobile, Web, GUI applications, etc. As we proceed with this blog, we will discuss abstraction, a key topic in OOPS, especially in Java. In the next section, we will explain how to implement it at 100%. Its syntax, the number of ways to implement it, as well as real-world examples. We also cover abstract methods, classes, and constructors with examples to really understand the topic. Also, we move to abstract methods, classes, and constructors with examples to really understand the full topic.

Define: Abstraction in Java

An abstraction is an act of hiding the internal implementation details and showing only the main functionalities/information to the user. So functionality is public whereas implementation is private.Java is based on the OOPS which is objected oriented programming language so every object has its own methods and its attributes.

If we take an example: When you go to an ATM machine no one tells you how the internal mechanism works but if you press the button then you can withdraw money one worries about the internal mechanism, technologies, and background works. So here as a user, the implementation is hiding only the withdrawn functionality that is shown. Similarly, in java objects, we only expose the functionality and hide its implementation which is known as Abstraction. To provide the security we use the abstraction concept because what if I say about how it works then we can create another same model with the same internal mechanism and details. Also, you will find a way to withdraw the money of 2000 instead of 100rs so that’s why abstraction is important to protect multilevel security. Also to get simple not to confuse the user.

2 Ways to achieve abstraction

Abstraction is of two types: partial abstraction using abstract classes and complete abstraction using interfaces. Through the interface, we can build 100% abstraction but with abstract classes, it can reach up to 100% (between 0 to 100)abstraction. In this article, we will discuss these two types and their implementation, syntax along with practical

Abstract class

If any class consists of only a declaration of a method hiding the definition using an abstract keyword in a class and defining in the subclasses having abstract methods and regular methods which is known as abstract class.

Interface

In java, we cannot achieve multiple inheritances because if any property child wants to get from the two base classes it’s going to be ambiguous so in order to achieve we have to implement the concept of the interface.

So we use two keywords ‘interface’, and ‘implements’. In these we use only abstract methods(only method declaration)

interface A{ void Ashow(); } interface B{ void Bshow(); } class AB implements A,B{ //multiple interfaces public Ashow(){ System.out.println("A is shown") } public void Bshow(){ System.out.println("B is shown") } } public class Main{ public Static void main(string args[]){ AB obj=new AB(); obj.Ashow(); obj.Bshow(); } }
Code language: Java (java)

output:

A is shown B is shown

What is an Abstract class in java?

In a class, instance variables and methods are present along with constructors, and modifiers, if any class contains at least one abstract method then the class is said to be an abstract class. It can contain abstract methods and normal methods too.

Rules of Java Abstract Class

  • In abstract classes, we will not instantiate/create the objects of the particular class. We do this by using the ‘abstract’ keyword, it can contain normal methods and abstract methods too.
  • To get access we must be inherited from another class only.
  • It may contain both the normal methods and abstract methods too.

for example

abstract class Sound{ public void cat(){ System.out.println("meow meow"); } }
Code language: Java (java)
Sound obj=new Sound() //it will give as a error,as we discussed above that we can't create an object to the Sound class
Code language: Bash (bash)

What is Abstract Method in java?

So the implementation of the abstract method will be written in the derived class. Note that we can’t create an object in these classes. We write with the ‘abstract’ keyword in these types of classes and also need to be familiar with inheritance to start with.

let’s take an example:

abstract void show();
Code language: Java (java)

As we already discussed it doesn’t have a body it’s just what we use and implemented in a subclass or we can say derived class. Just makes sure you have a knowledge of the inheritance concept.

abstract class A{ abstract void show(); }
Code language: Java (java)

Implementing an abstract method in an abstract class.

Point to Remember about Abstract Class and Abstract Method :

  • We cannot create an object for the abstract classes.
  • Also, we need to use abstract keyword while using it.
  • Implementation of an abstract method should be written in derived classes.
  • The abstract method doesn’t have a body. It must be declared in the abstract class only otherwise it shows an error.
  • In abstract classes, it can consist of abstract methods and normal methods which we saw in the above example as there is an abstract display method and void show method too.

Now we will implement with a real-world example to get more clarity.

Understanding the Abstract class in its real-world context

Let’s take a car, bike, or train these are the transportation ways to travel we have some properties in common so it’s important to have a transportation abstract class so that we use some methods in this abstract class that can be implemented in the derived or sub-classes. These allow flexibility and reusable code which we can implement in further classes.No need to worry we will implement this one as an example down the line.

Another example is: Let’s take a Bank account as an example, we will have a savings account, and a current account. They share some things in common so we can create an Account as an abstract class where we can define the withdrawal method, interest of double return type method, and overdrawn method too. This allows for a reusable and DRY(don’t repeat yourself) code.

Example of an abstract class in Java

We will create a Transportation abstract class with a constructor, along with methods so that we have different types of transportation abstract of the subclasses like a train, bus, bike, and cycle.

abstract class Transportation{ String name; Transporation(String name){ this.name=name; } abstract void motion(); }
Code language: Java (java)

Abstract class having a constructor, data members and methods

In this, it has a transportation class that has a name data member which is for the name of the transportation also we created a void motion method that is used in subclasses like Bike, cycle, train, etc.

class Bike extends Transportation{ int totalWheels; Bike(String name,int totalWheels){ super(name); this.totalWheels=totalWheels; } void motion(){ System.out.println("My "+name+" bike is running on"+totalWheels+"Wheels"); } } class Car extends Transporatation{ int totalWheels; Car(String name,int totalWheels){ super(name); this.totalWheels=totalWheels; } void motion(){ System.out.println("My "+name+" car is running on"+totalWheels+"Wheels"); } // public class Main{ public Static void main(string args[]){ Bike obj=new Bike("KTM",2); obj.motion(); Car mycar=new Car("Ford",4); mycar.motion(); } }
Code language: Java (java)

output:

My KTM bike is running on 2 Wheels My Ford car is running on 4 Wheels
Code language: Bash (bash)

Implementation of an Abstract Class and an Abstract Method in Java

We can also write abstract class B and method so need to create another class C that extends class B so that we can use it in class C and so on …

abstract class A{ //abstract class abstract void display(); //abstract method void show(){ //not an abstract (normal way of declaration) method System.out.println("Show method is called"); } } class B extends A{ void display(){ System.out.println("CLASS A"); } } public class Main{ public Static void main(string args[]){ B obj=new B(); obj.display(); obj.show(); }
Code language: Java (java)

output:

CLASS A Show method is called
Code language: Bash (bash)

Example abstract class with abstract method

So, the class inherits the data member name from the Transportation class and provides an implementation of the motion() method using the totalWheels of the Bike, Car. Similarly, you can create other subclasses like Train that would provide their own implementation of the motion() method. In this way, the Transportation class acts as an abstraction for different types of transportation and the subclasses like Car, Bike, and Train provide their own implementation of the abstract method motion()

Conclusion

In this way, we can hide the implementation using interfaces (100% complete abstraction) and abstraction(0-100% partial abstraction). Having privacy and allowing only functionalities part to the user. We also mentioned using abstract classes and abstract methods with real-world context applications.

What is an abstract class in Java?

In a class instance variables and methods are present along with constructors, and modifiers, if any class contains at least one abstract method then the class is said to be an abstract class.

What is an abstract class?

In an abstract class, if we implement interfaces partially then we create derived from creating objects that also define what the methods gonna perform in the subclass only.

What is abstract class purpose?

These allow flexibility and reusable code which we can implement in sub classes so that we can code less in a better way.

Give example of an abstract class

Let’s take a Bank account as an example then we will have a savings account, a current account. They share in common so we can create an Account as an abstract class where we can define the withdrawal method, interest of double return type method, and overdrawn method too. This allows for a reusable and DRY(don’t repeat yourself) code.

Sharing is caring

Did you like what Nithin Reddy wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far