class

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) {

  super(n);

  salary = s;

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

  hireDay = calendar.getTime();
    }

    public double getSalary() {

  return salary;
    }

    public Date getHireDay() {

  return hireDay;
    }

    @Override
    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

  super(n);

  major = m;
    }

    @Override
    public String getDescription() {

  return "a student majoring in " + major;
    }
}

Output:

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.

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.
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
Back to top button