Skip to main content

Object-Oriented Programming in Java — Abstraction

To reduce complexity, abstraction in Object-Oriented Programming refers to showing only the essential features of an object to the user and hiding the inner details. In other words, the user only needs to know “what an object does?” rather than “how it does?”


Example

Image by Author


The above sketch is a good real-world example of abstraction.

A user can only use and connect with the application’s limited functionality and is uninformed of the system architecture or how the application was built up. Typically, users are only involved with an application’s functionality.

Nothing at all is hidden from an administrator, who has access to many more features of the application. The administrator can monitor user activity, understand how the application was created, and implement new features by implementing them in the system.

In the previous example, the abstraction is implemented to the user but not to the administrator.

Image by Author


Let’s take a look at another example of abstraction. Take a look at the Volume control on a television remote. We ask the television to turn up the volume with a single click of a button. Assume the button invokes the volumeUp() function. The television responds by making a louder sound than before. We have no idea how the T.V.’s inner circuitry implements this, but we do know the exposed function needed to communicate with the T.V.’s volume.


An Example from Java


One can easily see abstraction in action in Java. Let’s look at the Java Math class as an example. This class contains a plethora of built-in methods that can be used by the programmer to get things done.

Let’s use the following methods in our code to access the built-in functionality.



You can see the output if you execute this program in your machine too.


Explanation of the above code


To find the smallest of two numbers, use the Math.min() method.

To find 2 to the power of 3, use the Math.pow() method.


However, the user is not required to be aware of the implementation of these two methods within the Math class.


Abstract Data Types


An abstract data type (or class) is one that only defines ‘what functions are to be performed?’ rather than ‘how functions are to be performed?’

The concept of abstract data types only provides users with the essentials, namely the functionality of those data types, while the ‘how the implementation should be done to accomplish the required functionality?’ part remains hidden.

An example of an abstract data type is a built-in stack class in Java, for which the user is aware of the push(), pop(), size(), and other methods but is unaware of how they are implemented.


How to Reach Abstraction?


To achieve abstraction in Java, we use the following components:

1. Interfaces

2. Abstract Classes


Keyword: abstract


Abstraction is impossible to achieve in Java without the use of the abstract keyword. The abstract keyword can only be used with methods and classes.

When the keyword abstract is used with classes or methods, those methods or classes only specify “what operations should be done,” and whoever uses this method or class in their code must deal with the method or class’s implementation details.

Isn’t the definition of abstraction the same? That is, to tell someone what should be done and how it is to be done is another story.


Abstract Classes and Methods

Abstract Methods


Definition

A method with the keyword abstract in its declaration is known as an abstract method.


Rules to consider

In contrast to a concrete/normal Java method, an abstract method lacks a body/definition, instead consisting of only a declaration or method signature within an abstract class or an interface (more on these later).

An abstract method can only be declared within an abstract class or interface.

In other words, in order to implement any abstract method, a class must be declared as an abstract class, because non-abstract classes cannot have abstract methods.

Because an abstract method must be implemented in another class, it cannot be declared private.


Declaration

Moving on to the syntax, the generalized declaration of an abstract method looks like this.

public abstract void methodName(parameter(s));

The declaration of an abstract method includes,

1. An access identifier

2. The keyword abstract

3. A return type

4. A name of the method

5. The parameter(s) to be passed

6. A semicolon(;) to end the declaration


At this point, one may ask about the definition or body of an abstract method, e.g., “Where do we implement the body of an abstract method?”


Abstract Classes


Definition

An abstract class is one that is declared with the keyword abstract.


Rules to consider

An abstract class cannot be instantiated, that is, an object of an abstract class cannot be created.

An abstract class can have the declaration of abstract method(s), but it is not required to have any (because the body of an abstract method cannot be implemented in an abstract class).

In an abstract class, non-abstract/normal methods can be implemented.

An abstract class must be inherited in order to be used.

The abstract class’s inheriting class must implement all of the abstract methods declared in the parent abstract class.

Everything else in an abstract class is the same as it is in a regular Java class, such as a constructor, static variables, and methods.


Declaration

In terms of syntax, the following is the declaration of an abstract class in Java.

abstract class ClassName {

    // Implementation here

}


Implementation

In Java, abstract classes are being used to achieve abstraction. Consider modeling an animal kingdom with Java.

1. A base abstract class named Animal

2. A child class named Dog

3. A child class named Cat

4. A child class named Sheep


All of these animals make different sounds.


Image by Author


In the preceding example, one can see that all of the animals’ common traits should be implemented in the Animal class. All other type-specific traits should be implemented within the child's classes. The abstract classes provide the programmer with the same functionality.


Let’s implement this.



We can see from the above example how useful an abstract class can be,

Because all animals can move, and this is a common trait, the move() method is implemented in the Animal class, and all child classes can use it without any implementation.

Because all of the animals make different sounds, an abstract method is declared in the Animal class, and all of the child classes must @Override this method in their own unique ways.


Hope this can help. Share your thoughts too.

Comments

Popular posts from this blog

Hello World project in ROS on Windows 10

We've seen how to install ROS in Windows 10 in this article, this is the time to start programming. What is the very first thing we do once we setup a new Programming Language? Hello World !!! Lets see how to write Hello World in ROS on Windows machine. Before going into the programming, make sure you've installed Gedit on your Windows machine. You can get it from here. Also, setup the path for Gedit and restart your machine to ensure the path definition works. First open the ROS terminal and check for the working directory. We have to be in the catkin workspace. If you've already created it, go to that directory. Or else, create it like this. Create a src folder inside catkin_ws. Inside that newly created src folder, lets create a new package in it. The command to create a ROS package is as follows. >catkin_create_pkg [PACKAGE_NAME] [DEPENDENT_PACKAGE_1] ....[DEPENDENT_PACKAGE_N] ‘std_msgs’ and ‘roscpp’ were added as optional dependent packages...

SQL Joins

You’ll nearly always need to connect many tables if you want to extract anything useful out of data. A join clause in SQL joins columns from one or more tables into a new table, similar to a join operation in relational algebra. In this article, let’s see how the joins work in SQL. We are going to explore the following SQL Joins.   ∘ 1 — (INNER) JOIN   ∘ 2 — LEFT (OUTER) JOIN   ∘ 3 — RIGHT (OUTER) JOIN   ∘ 4 — FULL (OUTER) JOIN AND UNION   ∘ When to use it? Also, I’m going to use the following tables for the above-mentioned SQL Joins. 1 — (INNER) JOIN Returns records with values in both tables that are the same. Image from Author As long as the condition is met, the INNER JOIN keyword selects all rows from both tables. This keyword will produce a result-set by merging all rows from both tables that satisfy the criteria, i.e. the common field’s value will be the same. Syntax: SELECT table1.column1,table1.column2,table2.column1,…. FROM table1 INNER JOIN ...

Collection Framework in Java

Data Structure As a definition, the data structure is a specific method of organizing data in a computer so that it may be used efficiently. In this article, we are going to see about the Collection Framework which enables us to implement some important Data Structures available on Java. Topics covered:   ∘ Data Structure   ∘ Collection Framework   ∘ Class vs Interface   ∘ Lists   ∘ Set   ∘ Map   ∘ Difference between List, Set, and Map interface in Java Collection Framework A Java collection is a grouping of separate objects that are represented as a single entity. Java collections, like data, provide all actions such as searching, sorting, insertion, modification, deletion, and so on. Java Collections is a fairly broad topic, and as a newbie, it might be tough to find your way around. We’ve covered all you need to know to get started with Java Collections. Image from  https://www.geeksforgeeks.org/how-to-learn-java-collections-a-complete-guide/ W...