Abstract class in java

An abstract class in java is a class that is non-concrete. By means of non-concrete is something that is not complete. You must think what’s the mean of not complete? How we can say the class is incomplete?
The answer is here if a class contains an abstract method then class known as non-concrete (incomplete class). In this article, we will discuss the use of abstract class in java and java abstract class example
Here is the table content of the article will we will cover this topic.
1. What is an abstract class in Java?
2. Properties of an abstract class?
3. How to create an Abstract class in java?
4. Why can’t we create the object of an abstract class?

5. Can an abstract class be final in java?
6. Can an abstract class have a constructor?
7. How to use abstract class and abstract methods in Real life?

What is an abstract class in Java

A class that is declared with abstract keyword is known as abstract class. An abstract class can have abstract methods (Method without body) and concrete/non-abstact methods (Methods with the body) also. A normal class can’t have any abstract method. In this article, we will discuss the abstract class in java, what are the properties of the abstract class, why we use it?

The abstract class provides the abstraction level from 1 to 100%.  The level of abstraction is totally depending upon the abstract methods. If an abstract class has an abstract method and non- abstract method, then the abstraction level lies between 1 to 100. But if an abstract class contains only abstract method then it provides 100% abstraction. We can use any number of abstract methods in the abstract class as per the use.

Properties of an abstract class

1. An abstract class is always declared with an abstract keyword. If a class is declared with an
abstract keyword then JVM considers it as abstract class.
2. If we are declaring an abstract method in any class then it must be an abstract class otherwise it gives a compilation error. Because this is the general rule of the abstract class.
3. We can’t create an object of Abstract class. It means we can’t instantiate the abstract class.
4. It can contain abstract methods and non-abstract methods.
5. It can contain constructors and static methods.
6. It can contain final methods.
7. If you are declaring a class an abstract class, then there must be a class that extends it and
provide implementation to abstract methods in it.
8. If you inherit an abstract class in the Concrete class then you must need to provide
implementations to all the abstract methods in it. Otherwise, it gives a compilation error.
9. If you inherit an abstract class in another abstract class then it depends upon the developer whether he wants to provide the implementation in this class or later.

Before we learn about it in detail, we need to understand abstract methods.

How to create an Abstract class in java?

//Declaration of the abstract class by using abstract keyword
 abstract  class className 
 {
    //abstract method because its hasn’t body
    abstract access_Specifier  methodName();
 } 

abstract:  It is a keyword that must be used at the time of declaration.
class_Name:  You can provide class names according to your functionality.
abstract method: Each abstract class has at least one abstract method.

//Declaration of the abstract class by using abstract keyword
abstract class ExampleOfAbstractClass
{
   //This is abstract method because it hasn’t body.  
   abstract void sound();
}
abstract class Course 
{
	public void courseDetail()
	{
		System.out.println("Course name: MCA");
		System.out.println("Course Duration : 2020 - 2023");
	}
	
	public abstract void studentData();
}

class StudentRecord extends Course
{

	@Override
	public void studentData() 
	{
		Student student = new Student(1, "Ram", "MCA");
		
		System.out.println("RollNo: "+student.getRollNo());
		System.out.println("Name: "+student.getName());
		System.out.println("Class Name: "+student.getClassName());
	}
}

class HostelRecord extends Course
{

	@Override
	public void studentData() 
	{
		Student student = new Student(2, "Krishan", "MCA");
		
		System.out.println("RollNo: "+student.getRollNo());
		System.out.println("Name: "+student.getName());
		System.out.println("Class Name: "+student.getClassName());
	}
}

public class MainClass
{
	public static void main(String arg[])
	{
		StudentRecord studentRecord = new StudentRecord();
		studentRecord.courseDetail();
		studentRecord.studentData();
		
		HostelRecord hostelRecord = new HostelRecord();
		hostelRecord.courseDetail();
		hostelRecord.studentData();
	}
}

class Student
{
	int rollNo;
	String name;
	String className;
	
	public Student(int rollNo, String name, String className) {
		this.rollNo = rollNo;
		this.name = name;
		this.className = className;
	}
	public int getRollNo() {
		return rollNo;
	}
	public void setRollNo(int rollNo) {
		this.rollNo = rollNo;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
}

Output: Course name: MCA
Course Duration : 2020 – 2023
RollNo: 1
Name: Ram
Class Name: MCA
Course name: MCA
Course Duration : 2020 – 2023
RollNo: 2
Name: Krishan
Class Name: MCA

Why can’t we create the object of an abstract class?

Abstract classes are not complete because they have abstract methods and abstract methods have nobody. If java allows creating an object of the abstract class, then it will lead to two problems:
1. The first problem occurs because the abstract method has nobody. If someone calls the
abstract method using that object, then What would happen?
2. Abstract classes are only like a template, to create an object of class the class should be
concrete.  because an object is concrete.

How to use abstract class and abstract methods in Real life?

Let’s say, I am the principal of a college and I want details of each department of the college. In my college have various departments like Art, science, commerce, etc. I want to know the department name, how many teachers in each department(mandatory information) and some additional information. So, I will create a structure that will be followed by each department. I will create an abstract class with an abstract method that will be inherited by each department. So that each department must provide the implementation to the abstract method.

Abstract class in java

Here we have three classes one class is abstract (Department) and the other two are concrete classes(Art class and science class). These concrete class inheriting the abstract class so they must provide the implementation of abstract methods.

abstract class Department
{
	String collegeName = "KUK";
	public void collegeName()
	{
		System.out.println("Name of college = "+ collegeName);
	}
	//abstract methods
	public abstract void deptName();
	public abstract void noOfTeachers();
	
}

class ArtDepartment extends Department
{
	public void showData()
	{
		System.out.println("This is Art department's data");
	}
	
	@Override
	public void deptName() 
	{
		System.out.println("Department name is Art");
	}

	@Override
	public void noOfTeachers() 
	{
		System.out.println("Number of teacher are 10");
	}
}

class ScienceDepartment extends Department
{
	public void getStudentsData()
	{
		System.out.println("This data is belongs to science students");
	}
	
	@Override
	public void deptName() 
	{
		System.out.println("Department name is Science");
	}

	@Override
	public void noOfTeachers() 
	{
		System.out.println("Number of teacher are 20");
	}
}

class MainClass 
{
	public static void main(String arg[])
	{
		// Displaying information of different departments
		
		ArtDepartment artDepartment = new ArtDepartment();
		artDepartment.showData();
		artDepartment.deptName();
		artDepartment.noOfTeachers();
		
		System.out.println("");
		
		ScienceDepartment scienceDepartment = new ScienceDepartment();
		scienceDepartment.getStudentsData();
		scienceDepartment.deptName();
		scienceDepartment.noOfTeachers();
	}
}

Output:
This is Art department’s data
Department name is Art
Number of teacher are 10
This data is belongs to science students
Department name is Science
Number of teacher are 20

2 thoughts on “Abstract class in java”

  1. How to create an Abstract class in java? –> For this example – variables access modifier should be private right ? without declaring access modifier as private you are setting and getting the values of the variables? Is that correct? Please response

    class Student
    {
    int rollNo;
    String name;
    String className;

    public Student(int rollNo, String name, String className) {
    this.rollNo = rollNo;
    this.name = name;
    this.className = className;
    }
    public int getRollNo() {
    return rollNo;
    }
    public void setRollNo(int rollNo) {
    this.rollNo = rollNo;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getClassName() {
    return className;
    }
    public void setClassName(String className) {
    this.className = className;
    }
    }

    Reply
  2. How to use abstract class and abstract methods in Real life?
    in the below example – you’re trying to print the college name but in the program example, college name is not printed in the console. Can you please check this program
    abstract class Department
    {
    String collegeName = “KUK”;
    public void collegeName()
    {
    System.out.println(“Name of college = “+ collegeName);
    }

    Reply

Leave a Comment