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:  

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
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.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
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 ....
I agree to the Terms and Privacy Policy

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
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button