Thursday 6 September 2018

Why an abstract class has a constructor?

Constructor do not create an object, they are used to initialize the object properties.

Constructor in abstract class is useful, while initializing the common properties of sub classes.

For example, I am going to build an employee management system with below requirements.
a.   Employee should have id, firstName and lastName.
b.   System should maintain the created date and modified date of the employee information.
c.   Address should have information about House Number, street, city and country.
d.   System should maintain the created date and modified date of the address information.

We can design the model classes like below.

public abstract class AbstractModel {
         private String id;
         private Date createdDate;
         private Date modifiedDate;
}


public class Employee extends AbstractModel {
         private String firstName;
         private String lastName;
}

public class Address {
         private String houseNum;
         private String street;
         private String city;
         private String country;
}




Since the fields id, createdDate and modifiedDate are common for models Employee and Address, we can use the abstract class constructor to define these common variables.

public abstract class AbstractModel {
         private String id;
         private Date createdDate;
         private Date modifiedDate;

         public AbstractModel() {
                  id = UUID.randomUUID().toString();
                  createdDate = new Date();
                  modifiedDate = new Date();
         }
        
}

Find the below working application.

AbstractModel.java
package com.sample.model;

import java.util.UUID;
import java.util.Date;

public abstract class AbstractModel {
 private String id;
 private Date createdDate;
 private Date modifiedDate;

 public AbstractModel() {
  id = UUID.randomUUID().toString();
  createdDate = new Date();
  modifiedDate = new Date();
 }

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public Date getCreatedDate() {
  return createdDate;
 }

 public void setCreatedDate(Date createdDate) {
  this.createdDate = createdDate;
 }

 public Date getModifiedDate() {
  return modifiedDate;
 }

 public void setModifiedDate(Date modifiedDate) {
  this.modifiedDate = modifiedDate;
 }

 @Override
 public String toString() {
  return "AbstractEntity [id=" + id + ", createdDate=" + createdDate + ", modifiedDate=" + modifiedDate + "]";
 }

}


Employee.java
package com.sample.model;

public class Employee extends AbstractModel {
 private String firstName;
 private String lastName;

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 @Override
 public String toString() {
  return "Employee [firstName=" + firstName + ", lastName=" + lastName + ", getId()=" + getId()
    + ", getCreatedDate()=" + getCreatedDate() + ", getModifiedDate()=" + getModifiedDate()
    + ", toString()=" + super.toString() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
    + "]";
 }

}

Address.java
package com.sample.model;

public class Address {
 private String houseNum;
 private String street;
 private String city;
 private String country;

 public String getHouseNum() {
  return houseNum;
 }

 public void setHouseNum(String houseNum) {
  this.houseNum = houseNum;
 }

 public String getStreet() {
  return street;
 }

 public void setStreet(String street) {
  this.street = street;
 }

 public String getCity() {
  return city;
 }

 public void setCity(String city) {
  this.city = city;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 @Override
 public String toString() {
  return "Address [houseNum=" + houseNum + ", street=" + street + ", city=" + city + ", country=" + country
    + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString()
    + "]";
 }

}

Application.java

package com.sample.app;

import com.sample.model.Employee;

public class Application {
 public static void main(String args[]) {
  Employee emp = new Employee();
  emp.setFirstName("Krishna");
  emp.setLastName("Ponnam");

  System.out.println(emp);
 }
}


Output
Employee [firstName=Krishna, lastName=Ponnam, getId()=fd5f179a-f59b-4d38-be80-b789b0049b9e, getCreatedDate()=Thu Sep 06 09:10:54 IST 2018, getModifiedDate()=Thu Sep 06 09:10:54 IST 2018, toString()=AbstractEntity [id=fd5f179a-f59b-4d38-be80-b789b0049b9e, createdDate=Thu Sep 06 09:10:54 IST 2018, modifiedDate=Thu Sep 06 09:10:54 IST 2018], getClass()=class com.sample.model.Employee, hashCode()=1360875712]

You may like

No comments:

Post a Comment