Abstract Class in Java with Example

Abstract classes are used in java to create a class with some default method implementation for subclasses. An abstract class can have abstract method without body and it can have methods with implementation also. abstract keyword is used to create a abstract class. Abstract classes can’t be instantiated and mostly used to provide base for sub-classes to extend and implement the abstract methods and override or use the implemented methods in abstract class.

Here is a simple example of 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;
	}
	
}

Notice that work() is an abstract method and it has no body. Here is a concrete class example extending abstract class.

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 and learn why we should always use Override annotation when overriding a method.

Important Points about Abstract class

  • abstract keyword is used to make a class abstract.
  • Abstract class can’t be instantiated.
  • We can use abstract keyword to create an abstract method, an abstract method doesn’t have body.
  • If a class have abstract methods, then the class also needs to be made abstract using abstract keyword, else it will not compile.
  • Its not necessary to have abstract classes to have abstract method.
  • If abstract class doesn’t have any method implementation, its better to use interface because java doesn’t support multiple class inheritance.
  • The subclass of abstract class must implement all the abstract methods unless the subclass is also an abstract class.
  • 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.
  • Abstract classes can implement interfaces without even providing the implementation of interface methods.
  • Abstract classes are used to provide common method implementation to all the subclasses or to provide default implementation.
  • We can run abstract class like any other class if it has main() method.

Thats all for the abstract classes in java, if I have missed anything please comment.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current ye@r *

Subscribe to JournalDev Newsletter
Get the FREE access to Monthly Newsletter and Free PDF eBooks
*No Spam Guaranteed. By entering your email address, you agree also subscribing to our newsletter.
Oops! - Something went wrong.
Close
Introduction to Software Development 380 Page eBook Free Download Now
Exclusive Offer: Citrix™ Whitepaper on Denial of Service Attack Download Now