Abstraction in Java

We would cover the following:

  1. What is Abstraction?
  2. Abstract Classes and Methods in Java
  3. Example of Abstract Class in Java
  4. Inheriting an Abstract Class
  5. Instantiating an Inherited Abstract Class
  6. Abstract Methods in Java

 

1.  What is Abstraction?

Abstraction in Object Oriented Programming is the concept of making the user focus on the relevant aspects of the problem without getting much involved in the low-level details.

For example, we all use the remote control to operate electronic devices. Here we focus on how to use the buttons to operate the device, but we don’t get involved in the electronic principles that guides the communication between the remote control and the device. This low level details is hidden from the user and in fact is not relevant to the user.

In the same way,  in OOP, abstraction hides the implementation details from the user while presenting only the relevant features. This is also referred to as Information Hiding.

Abstraction in java is achieved by the use of abstract classes, abstract methods and interfaces.

 

2. Abstract Classes and Methods

An abstract class is created using the abstract keyword in the class declaration. The following rules applies to abstract classes and methods:

  • Abstract class is declared using the abstract keyword
  • An abstract class may or may not contain an abstract method (method without a body)
  • If a class contains an abstract method, then that class must be declared as an abstract class
  • An abstract class cannot be instantiated i.e you cannot create an object from an abstract class
  • To use an abstract class, you must create another class that inherits from the abstract class. This subclass would then provide implementation for the abstract methods
  • Derived classes from abstract classed provide implementation for the abstract methods

 

3. Abstraction Example

Here we would go through an example of an abstract class. Let’s create class called Person.

/*
 * Abstract class Person
 * Written by Kindson The Genius
 * Date : December 21, 2018
 */

import java.time.LocalDate;
import java.time.Period;
import java.util.Date;

public abstract class Person {
	private String Firstname;
	private String Lastname;
	private String Address;
	private LocalDate DateOfBirth;
	
	//Constructor with all four parameters
	public Person(String Firstname, String Lastname, String Address, LocalDate DateOfBirth)
	{
		System.out.println("Creating a new person object");
		this.Firstname = Firstname;
		this.Lastname = Lastname;
		this.Address = Address;
		this.DateOfBirth = DateOfBirth;
	}
	
	//Constructor with no parameter
	public Person() {
		
	}
	
	public String Fullname()
	{
		return "Name: " + this.Firstname + ", " + this.Lastname;
	}
	
	public void setAddress(String Address)
	{
		this.Address = Address;
	}
	
	public int GetAge()
	{
		LocalDate currentDate = LocalDate.now();
		return Period.between(this.DateOfBirth, currentDate).getYears();	
	}
}

Listing 1.0: Abstract class for Person

 

Copy this program in Listing 1.0 into your java IDE and run it. Make sure you add the necessary imports.  Everything works fine. The only problem is that this class is abstract and so we cannot instantiate. You can try to instantiate it using the code below  in listing 1.1 in the main method and check what happens.

public class PersonTester {

	public static void main(String[] args) {
		//Creating and instance of the person class
		Person person = new Person();
	}
}

Listing 1.1: Creating an instance of an abstract class

If you run to code above that tries to create an instance of the Person class, you will get the error below:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Cannot instantiate the type Person

	at abstractstuff.PersonTester.main(PersonTester.java:7)

Listing 1.2: You cannot instantiate an abstract class

 

4. Inheriting an Abstract Class

To use the abstract class, we need to create a subclass that inherits from it. In Listing 1.3, we create a class Student that extends the Person class.

//Student class inherits from Person class
public class Student extends Person {
	private int Level;
	private String RegNo;
	
	public Student(String RegNo)
	{
		this.RegNo = RegNo;
	}
	
	public String getRegNO()
	{
		return RegNo;
	}
	
	public int getLevel()
	{
		return Level;
	}	
}

Listing 1.3: Student Class inherits from the Person class

 

5. Instantiating the Inherited Class

Now we can instantiate the student class.

From the main method we are going a new Student object of type Student.

We would also create a new Student object of type Person

public class PersonTester {

	public static void main(String[] args) {
		//Creating and instance of the person class
		Student student = new Student("KTH3948");
		Person person = new Student ("OSI9893");
		
		String name = person.Fullname(); //Sub class calls the parent class method
		int age = person.GetAge(); //Sub class calls the parent class method
	}
}

Listing 1.4: Inheriting an abstract class

Try to run the above code and view the output

 

6. About Abstract Method

A method can also be declared as abstract by using the abstract keyword.  Take note of the following:

  • If a method is abstract, then the class containing the method must also be abstract. An attempt to declare an abstract method is a class that is not abstract would produce and error (“abstract method can only be declared in an abstract class).
  • An abstract method contains only the method declaration, no body is defined
  • Abstract method declaration ends with a semicolon(;)
kindsonthegenius

kindsonthegenius

Kindson Munonye is currently completing his doctoral program in Software Engineering in Budapest University of Technology and Economics

View all posts by kindsonthegenius →

Leave a Reply

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