Java Lesson 16: Abstract Classes

Hello everybody,

Michael here, and today’s post will be on abstract classes in Java.

First off, let’s start by discussing abstract classes. What is an abstract class? Well, let’s start off by explaining the concept of data abstraction. In Java (or any other programming language), data abstraction is the process of hiding certain details of the program and showing only essential information to the user. An abstract class is a restricted class that cannot be used to create objects-abstract classes can only be accessed if other classes inherit them (I’ll explain that more later). Also, just as regular classes can have regular methods, abstract classes can have abstract methods, which are methods that don’t have a body and can only be used in abstract classes.

  • It probably seems obvious to you guys, but the magic word  you would use to make a class or method abstract is abstract .
  • Abstract methods are created without a body, but the body of an abstract method is created in a subclass.

Alright, now that I’ve given you guys some explanation, let’s create an abstract class:

abstract class Abstract
{
public abstract void password();

public void username()
{
System.out.println(“MIKE2020”);
}
}

In this example, I created an abstract class with an abstract method-password-and a regular method-username(). Since abstract methods don’t have a body, the password() method doesn’t have a body either; the body of the password() method will be inherited from a subclass.

  • Also, I know it seems like a no-brainer, but abstract classes and methods would be denoted with the abstract key-word.

Another thing I wanted to note is that it’s not possible to create an object of an abstract class, since you can only access abstract classes by inheriting them from subclasses. Trying to create an object of an abstract class will generate an error, so statements like this aren’t possible in Java:

Abstract myObj = new Abstract();

But wait-I did say that you can access abstract classes and their methods by inheriting them from subclasses. Here’s how to do that:

class Login extends Abstract
{
@Override
public void password()
{
System.out.println(“abstract10n”);
}
}

In this example, I created a subclass of Abstract called Login (the fact that it’s a subclass is denoted by the extends key-word). Since Login is a subclass of Abstract, I was able to create the body of the password() method (which consists of a single System.out.println() command). Keep in mind that since Login isn’t an abstract class, password() isn’t an abstract method.

Now that we’ve got an abstract class and a subclass, what else do we need? We would need a main class to run the program. Here’s the main class:

public class Credentials
{
public static void main (String [] args)
{
Login login1 = new Login();
login1.username();
login1.password();
}
}

Here’s the output:

run:
MIKE2020
abstract10n
BUILD SUCCESSFUL (total time: 2 seconds)

In this example, I created the main class Credentials to run the program. To access the username() and password() methods, I had to create an object of the Login class, not the Abstract class, because I can only create objects of the subclass, not the abstract class. Also, I can only access the abstract class’s methods through the subclass, not through the abstract class itself.

Anyway, you might be wondering “What is the purpose of abstraction?”. The point of abstraction is that it improves the security of your program, as abstraction hides certain details of a program and only shows a program’s important details. Program details hidden in an abstract class are harder to access, as you would need to create a new subclass to access the abstract class and all of its methods.

Thanks for reading,

Michael