Java Programming: Basics and Application of Abstract Classes

In the world of Java programming, abstract classes play an important role. They resemble interfaces but bring an interesting feature with them: the ability to provide default method implementations. In this blog post, we will delve into abstract classes in Java, their usage, and some important points you should know about them.

What is an abstract class?

An abstract class in Java is a type of class created using the “abstract” keyword. Unlike regular classes, an abstract class can contain both abstract methods without implementation and methods with implementations. The crucial difference from interfaces is that abstract classes can also have concrete methods.

The syntax of an abstract class

Here is a simple example of an abstract class in Java:


java
package com.journaldev.design;

// Abstract class
public abstract class Person {
	
	private String name;
	private String gender;
	
	public Person(String nm, String gen){
		this.name = nm;
		this.gender = gen;
	}
	
	// Abstract method
	public abstract void work();
	
	@Override
	public String toString(){
		return "Name=" + this.name + "::Gender=" + this.gender;
	}

	public void changeName(String newName) {
		this.name = newName;
	}	
}

In this example, you see the abstract class “Person.” It contains an abstract method named “work()” and also a concrete method “changeName()”. Note that the abstract method “work()” has no implementation.

The use of abstract classes in Java

Abstract classes in Java are mainly used to provide a base for subclasses. These subclasses extend abstract classes by implementing the abstract methods and using or overriding the concrete methods.

An example of a subclass that inherits from the abstract class “Person” is the “Employee” class:


java
package com.journaldev.design;

public class Employee extends Person {
	
	private int empId;
	
	public Employee(String nm, String gen, int id) {
		super(nm, gen);
		this.empId = id;
	}

	@Override
	public void work() {
		if(empId == 0){
			System.out.println("Not working");
		}else{
			System.out.println("Working as an employee!!");
		}
	}
	
	public static void main(String args[]){
		Person student = new Employee("Dove", "Female", 0);
		Person employee = new Employee("Pankaj", "Male", 123);
		student.work();
		employee.work();
		employee.changeName("Pankaj Kumar");
		System.out.println(employee.toString());
	}
}

The “Employee” class inherits from “Person” and implements the abstract method “work()”.

Key points about abstract classes in Java

There are some important points you should know about abstract classes in Java:

  1. The “abstract” keyword is used to create an abstract class.
  2. Abstract classes cannot be instantiated.
  3. The “abstract” keyword is used to create abstract methods without implementation.
  4. If a class contains abstract methods, the class itself must also be marked as abstract.
  5. An abstract class can be created without abstract methods as well.
  6. If an abstract class contains no method implementations, it might be better to use an interface.
  7. The subclass of an abstract class in Java must implement all abstract methods unless the subclass is also abstract.
  8. All methods in an interface are implicitly abstract unless they are static or default methods.
  9. An abstract class in Java can implement interfaces without providing the implementation of interface methods.
  10. Abstract classes in Java are used to provide common method implementations for all subclasses or provide a default implementation.
  11. Abstract classes in Java can be executed like any other class if they have a “main()” method.

 

That was an introduction to abstract classes in Java. They provide a powerful way to hierarchically organize the structure and functionality of your classes, facilitating code reuse. If you have any further questions or comments, please share them in the comments section.

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in:

centron Managed Cloud Hosting in Deutschland

Hibernate SessionFactory

JavaScript, Tutorial
Hibernate SessionFactory Hibernate SessionFactory is the factory class through which we get sessions and perform database operations. Content1 Hibernate SessionFactory2 Conclusion Hibernate SessionFactory Hibernate SessionFactory provides three methods through which…