Tutorial

Abstract Class in Java

Published on August 3, 2022
Default avatar

By Pankaj

Abstract Class in Java

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Abstract class in Java is similar to interface except that it can contain default method implementation. An abstract class can have an abstract method without body and it can have methods with implementation also. abstract keyword is used to create a abstract class and method. Abstract class in java can’t be instantiated. An abstract class is mostly used to provide a base for subclasses to extend and implement the abstract methods and override or use the implemented methods in abstract class.

Abstract Class in Java

abstract class in java, java abstract class example Here is a simple example of an Abstract Class in 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;
	}	
}

Notice that work() is an abstract method and it has no-body. Here is a concrete class example extending an abstract class in 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 employee!!");
		}
	}
	
	public static void main(String args[]){
		//coding in terms of abstract classes
		Person student = new Employee("Dove","Female",0);
		Person employee = new Employee("Pankaj","Male",123);
		student.work();
		employee.work();
		//using method implemented in abstract class - inheritance
		employee.changeName("Pankaj Kumar");
		System.out.println(employee.toString());
	}

}

Note that subclass Employee inherits the properties and methods of superclass Person using inheritance in java. Also notice the use of Override annotation in Employee class. Read more for why we should always use Override annotation when overriding a method.

Abstract class in Java Important Points

  1. abstract keyword is used to create an abstract class in java.
  2. Abstract class in java can’t be instantiated.
  3. We can use abstract keyword to create an abstract method, an abstract method doesn’t have body.
  4. If a class have abstract methods, then the class should also be abstract using abstract keyword, else it will not compile.
  5. It’s not necessary for an abstract class to have abstract method. We can mark a class as abstract even if it doesn’t declare any abstract methods.
  6. If abstract class doesn’t have any method implementation, its better to use interface because java doesn’t support multiple class inheritance.
  7. The subclass of abstract class in java must implement all the abstract methods unless the subclass is also an abstract class.
  8. All the methods in an interface are implicitly abstract unless the interface methods are static or default. Static methods and default methods in interfaces are added in Java 8, for more details read Java 8 interface changes.
  9. Java Abstract class can implement interfaces without even providing the implementation of interface methods.
  10. Java Abstract class is used to provide common method implementation to all the subclasses or to provide default implementation.
  11. We can run abstract class in java like any other class if it has main() method.

That’s all for an abstract class in Java. If I missed anything important, please let us know through comments.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
September 26, 2021

how can we invoke any static method from abstract class?

- Priyanka

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    April 8, 2020

    please explain me about abstract with easy example because i didn’t it in better way. i request you and please also explain me all those points in better ways

    - ANJALI THAKUR

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      September 11, 2019

      Can u please explain one Real World Use Case for Abstract class and abstract Method b

      - Anshul Daksh

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        June 26, 2019

        By using the static keyword we can access the concrete methods in the abstract class. Can any one tell without using the static how can we access the concrete methods in the abstract class?

        - Udaya Bhargavi

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          May 28, 2019

          Important points are easy to understand about the abstract class thank you for your explanation ;)

          - Venkatesh

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            March 31, 2019

            Respected admin, i would like to point out an improvement in point number 5 of the *Important points* written at the end, i believe the correct statement should be : It’s not necessary FOR AN abstract class to have abstract method. We can mark a class as abstract even if it doesn’t declare any abstract methods. instead of It’s not necessary TO HAVE abstract class to have abstract method. We can mark a class as abstract even if it doesn’t declare any abstract methods… It’s really ambiguous otherwise . i hope the admin would act promptly.

            - Hissaan

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              March 6, 2019

              what is the main role of abstract class in java?

              - aryan

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                March 2, 2019

                Does abstract class contains Constructor? If yes, then what is the use of it?

                - Bhagyashri Chaudhari

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  September 24, 2018

                  you ain’t mentioned what’s the use of it.

                  - Deepak gupta

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    September 23, 2018

                    The subclass of abstract class in java must implement all the abstract methods unless the subclass is also an abstract class. On a similar note, If Class3 extends abstract Class2 and abstract Class2 extends abstract Class1, then Class3 should implement all the abstract methods of Class1 and Class2. Class 2 need not implement methods of Class1 since Class2 is also abstract.

                    - Praveen

                      Try DigitalOcean for free

                      Click below to sign up and get $200 of credit to try our products over 60 days!

                      Sign up

                      Join the Tech Talk
                      Success! Thank you! Please check your email for further details.

                      Please complete your information!

                      Get our biweekly newsletter

                      Sign up for Infrastructure as a Newsletter.

                      Hollie's Hub for Good

                      Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

                      Become a contributor

                      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                      Welcome to the developer cloud

                      DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

                      Learn more
                      DigitalOcean Cloud Control Panel