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.
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
keyword is used to create an abstract class in java.abstract
keyword to create an abstract method, an abstract method doesn’t have body.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.
Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev
Nice tutorial, I think you have covered everything about abstract classes and methods. Please provide some difference between abstract class and interface.
- Amit
There are two classes person and Employee. You have created the object of person class. Is it ok? and where is ‘changeName’ method?
- Kapil
This is is not showing parameter value . i have use Person student= new Employee(“Deepak”,“Male”,20);
- deepak
1. abstract class can’t be instantiated 2. abstract class can contain definition of function with respect to these features, my question is— can we call those function which are defined in abstract class by reference of that class? if yes then why and how?
- Firoj Mujawar
can we override a non abstract method of abstract class . If yes then what is the use of making a method abstract in abstract class.
- vishnu
Abstract class can contain Constructor, if we cannot create an instance what is the use of this constructor.
- Raghuveer Kurdi
Abstract class can’t be instantiated. It is wrong what about anonymous class.
- manish
Hi Pankaj, Can u please tell me where we can use interfaces and abstract classes in realtime,could you please give me in realtime example.
- Rajesh
can i have more day to day life examoles on abstract class…
- shaurya gaurav mehta
i need to submit this morning. i have working out my self but i am unable to define it. define an interface that consists of methods that can operate on classes employee and student
- odelami emmanuel
Hey could you please explain that , How do you use “employee.changeName(“Pankaj Kumar”);” ? What is changeName ?
- lahiru
I feel like the Person, Employee relationship is better represented by an interface rather than an abstract class because a Person can be an Employee but it is not a necessity. A person can be more than one thing therefore A Person should implement an interface called Employee because they can also be something else like a manager or a supervisor.
- Stanley
Why Abstract Class is used in Java? I want to know the sole purpose of Abstract Class other than its providing Data hiding and forces the programmer to implement all the methods in Abstract class.
- Yoganathan
Replying to JournalDev
you can have default implementation and abstract methods that you want all the implementation classes to implement. However with Java 8 default methods in interfaces, Interface and Abstract class are almost same.
- Pankaj
It was a wonderful recap of what i learned java docs from various resources in internet. Thanks for covering entire picture of core java. God bless you!!
- sathish
Hi, Thank you in advace for yout awsome tutorials and topis. in the section of ‘Abstract class in Java Important Points’. point number 5 I guess is not correct. because to have an abstract method, its class must be abstract also. If I`m wrong it would be greate to clear me. Thanks
- Morteza
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
Does abstract class contains Constructor? If yes, then what is the use of it?
- Bhagyashri Chaudhari
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
Important points are easy to understand about the abstract class thank you for your explanation ;)
- Venkatesh
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
Can u please explain one Real World Use Case for Abstract class and abstract Method b
- Anshul Daksh
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.