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 methodString getDescription()
and another methodString getName()
that returns the String field of the class. - We have created
Employee
that extendsPerson
. 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, usinggetTime()
API method of GregorianCalendar. It has two methods,double getSalary()
andDate getHireDay()
that return its fields. It overrides thegetDescription()
method ofPerson
, where it returns a formated String, by formating its double field, using theformat(String format, Object... args)
API method of String. - We have also created a class
Student
that also extendsPerson
. 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 thegetDescription()
method ofPerson
, where it returns a String message and its String field. - We create a new
Person
array and add a newEmployee
object and a newStudent
object. Then we call theirgetName()
andgetDescription()
methods, both inherited from thePerson
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.