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.
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.
Nice tutorial, I think you have covered everything about abstract classes and methods. Please provide some difference between abstract class and interface.
- Amit
Methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behaviour. Variables declared in a Java interface are by default final. An abstract class may contain non-final variables. Members of a Java interface are public by default. A Java abstract class can have the usual flavours of class members like private, protected, etc. A Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”. An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces. A Java class can implement multiple interfaces but it can extend only one abstract class. Happy Learning :)
- Aliasger Motiwala
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
You should have a concrete class extending the abstract class and provide implementation of abstract methods, you can also have anonymous class implementation. After that it’s just inheritance.
- Pankaj
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
Yes, Abstract classes can have constructors unlike Interfaces. However we cannot directly instantiate an Abstract class to create an instance. The purpose of the Abstract class constructors (we can have multiple abstract class constructors with different arguments) is to initialize the final variables from subclasses (kind of lazy initialization)
- Vishnu Prakash
To Achieve Constructor Chaining Abstract Class Should have Constructor, though Object Cant be Created for it. Every Java Class Will inherit From Object class… So Even Abstract Class Will inherit From Object Class. So If There is Inheritance between Classes then Constructor Chaining is Mandatory!! Constructor Chaining means Sub Class Constructor Calling its Immediate Super Class Constructor.
- Raj
Abstract class can’t be instantiated. It is wrong what about anonymous class.
- manish
No. Abstract Classes cannot be instantiated. Let us say we have got an abstract class absClass with one unimplemented abstract method doSayHello. When we implement doSayHello method in the curly brackets, we are doing it under the pretence that we are extending an unnamed class but not by instantiating the abstract class itself. absClass ab = new absClass(){ @Override public void doSayHello() { System.out.println(“Hello World !”); } }; ab.doSayHello(); // prints “Hello World !”
- Yunus Atheist
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
public interface LoginAuth{ public String encryptPassword(String pass); public void checkDBforUser(); } Now suppose you have 3 databases in your application. Then each and every implementation for that database needs to define the above 2 methods: public class DBMySQL implements LoginAuth{ // Needs to implement both methods } public class DBOracle implements LoginAuth{ // Needs to implement both methods } public class DBAbc implements LoginAuth{ // Needs to implement both methods } But what if encryptPassword() is not database dependent, and it’s the same for each class? Then the above would not be a good approach. Instead, consider this approach: public abstract class LoginAuth{ public String encryptPassword(String pass){ // Implement the same default behavior here // that is shared by all subclasses. } // Each subclass needs to provide their own implementation of this only: public abstract void checkDBforUser(); } Now in each child class, we only need to implement one method - the method that is database dependent. Tried my best to make you understand. Happy Learning :)
- Aliasger Motiwala
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