Abstract class example

With this example we are going to demonstrate how to create and use an abstract class. In short, we have created an abstract class with an abstract method, that is extended by two other classes, as described below:

  • We have created an abstract class Person, with a String field and a constructor using its field. It has an abstract method String getDescription() and another method String getName() that returns the String field of the class.
  • We have created Employee that extends Person. It has a double field and a Date field. It has a constructor where it calls its super constructor to initialize its super String field, and creates a new GregorianCalendar with the given double s, int year, int month, int day and creates a Date, using getTime() API method of GregorianCalendar. It has two methods, double getSalary() and Date getHireDay() that return its fields. It overrides the getDescription() method of Person, where it returns a formated String, by formating its double field, using the format(String format, Object... args) API method of String.
  • We have also created a class Student that also extends Person. It has a String field. In its constructor it uses the superclass constructor to initialize the superclass String field with the first given String and then initializes its String field with the second given String. It also overrides the getDescription() method of Person, where it returns a String message and its String field.
  • We create a new Person array and add a new Employee object and a new Student object. Then we call their getName() and getDescription() methods, both inherited from the Person class.

Let’s take a look at the code snippet that follows:  

package com.javacodegeeks.snippets.core;

import java.util.Date;
import java.util.GregorianCalendar;

public class Abstract {

    public static void main(String[] args) {

  Person[] people = new Person[2];

  // fill the people array with Student and Employee objects

  people[0] = new Employee("Harry James", 50000, 1989, 10, 1);

  people[1] = new Student("Maria Morris", "computer science");

  // print out names and descriptions of all Person objects

  for (Person p : people) {

System.out.println(p.getName() + ", " + p.getDescription());


abstract class Person {

    private String fullname;

    public Person(String n) {

  fullname = n;

    public abstract String getDescription();

    public String getName() {

  return fullname;

class Employee extends Person {

    private double salary;
    private Date hireDay;

    public Employee(String n, double s, int year, int month, int day) {


  salary = s;

  GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);

  hireDay = calendar.getTime();

    public double getSalary() {

  return salary;

    public Date getHireDay() {

  return hireDay;

    public String getDescription() {

  return String.format("an employee with a salary of $%.2f", salary);

class Student extends Person {

    private String major;

    public Student(String n, String m) {

  // pass n to superclass constructor


  major = m;

    public String getDescription() {

  return "a student majoring in " + major;


Harry James, an employee with a salary of $50000.00
Maria Morris, a student majoring in computer science

This was an example of how to create and use an abstract class in Java.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button