Reflect on the content covered relative to Classes and objec
Solution
Encapsulation: provides a valuable service to your design: It hides the implementation. It enables compliance with the Object Oriented Principle: Separation Of Concerns (SOC). The concept protects the objects within the type from being altered in a way that is unintended by the Developer. Encapsulation also abstracts away the Dependencies that the Encapsulated Class Method requires from the calling Software Entity. Encapsulation helps the Developer to comply with the “O” in the S.O.L.I.D. Design Principles: Open / Closed Principle.
Inheritance: The Ability to Enable Objects to Share Common Functionality of the Parent Object. Inheritance Promotes the Reuse of Existing Object’s Code Elements. Conventional use of Inheritance is Class based. This assumes that the relationship between the Parent Class and the Children Classes has an “Is A” relationship. The Derived Child Class “Is A” a variation of its Parent Class. In the Animal Kingdom an “Is A” relationship is implicit. Unfortunately in Programming Kingdom it is not implicit. In reality, most programming inheritance structure are really a “Behaves Like” relationship more that an “Is A” relationship. Interface Inheritance, or Composition, should be used when it is clear that the Derived Type is not a variation of its Base Type. An improper inheritance model leads to unmanageable code over the life cycle of the code base due to inevitable change requests and feature/functionality additions.
Polymorphism:The Ability to Derive Different Concrete Behaviors from a Common Abstract Type. In Class Inheritance we can accomplish Polymorphic Behavior by deriving Classes from a Common Base “Parent” Class. We then create different child variations of the parent. The variation is Object “Typed” as a variation of the Parent Type.In Interface inheritance we create the “Behaves Like” Polymorphic Behavior by using the Contract of the Interface as the Base Behavior. We implement the Interface Behavior Definitions in the concrete Types to Inherit the Interface behavior.
Data Abstractions:The Encapsulation of Data within a Single Object that includes Primitives and other Complex Objects as Related Members. Data is State. State Objects are Classes that only contain Properties and Constructors. Properties are initialized by Constructors. These Data Abstractions are called Data Transfer Objects: A DTO. A DTO abstracts the details of the consumable properties, and their initialization process, from the consuming method. The DTO can manage the property object through access modifiers within the getters and setters for the property using the three available class constructors. The Data Abstraction can be a simple encapsulation DTO that holds a single primitive:
Public bool IsSuccess {get; set}
The DTO Data Abstraction can be a Container. Acting as a Transport Package for other DTO Types.
for example:
Lets assume there is a class Employee which gets data about the employee from the DB, So we can have a getEmployeeData() method as public which can be accessed from outside. Let\'s assume there are separate lastName and firstName fields but the user wants it concatenated as full name so there is also a private method getFullName() which will concatenate them.
There will be another EmployeeBean class with the fields and proper getters and setters.
Let\'s go through the code to make it clear -
Employee Class
public class Employee {
public EmployeeBean getEmployeeData(String empId){
System.out.println(\"Going to DB to get employee Data\");
// goes to the stub method rather than going to DB
EmployeeBean employeeBean = getData();
//Will give Compilation error, data can be set only through setters method
//employeeBean.age = 10;
String name = getFullName(employeeBean.getFirstName(), employeeBean.getLastName());
employeeBean.setFullName(name);
System.out.println(\"Employee bean - \" + \"Age - \" + employeeBean.getAge() +
\" Name - \" + employeeBean.getFullName() + \" Emp ID - \" + employeeBean.getEmpId());
return employeeBean;
}
// private method to concatenate first and last name
private String getFullName(String firstName, String lastName){
return firstName + \" \" + lastName;
}
// Stub method
private EmployeeBean getData(){
EmployeeBean employeeBean = new EmployeeBean();
employeeBean.setEmpId(\"E001\");
employeeBean.setLastName(\"Mishra\");
employeeBean.setFirstName(\"Pyaremohan\");
employeeBean.setAge(35);
return employeeBean;
}
}
Employee Bean Class
public class EmployeeBean {
private String lastName;
private String firstName;
private String fullName;
private String empId;
private int age;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
}
Test Class
public class EmployeeTest {
public static void main(String[] args) {
Employee employee = new Employee();
//employee.getData(); // WILL GIVE COMPILATION ERROR
employee.getEmployeeData(\"E001\");
}
}
So it can be seen how in EmployeeBean all the variables are private and access is provided through setters and getters methods.
In class Employee there is getEmployeeData() method which can be accessed from outside. Since getFullName() has some logic specific to the class so that is made private.
Benefits of Encapsulation
To maintain code that is changed frequently by keeping that in one place thus providing maintainability and flexibility.
Encapsulation allows you to control the access to the class.
Points to note -
Encapsulation is one of the four fundamental OOPS concept. Other being, inheritance, polymorphism and abstraction.
Encapsulation means keeping code and data together in a class.
An encapsulated class will have private instance variable and only the methods within that class can access those variables.
Outside classes will be given access to any particular class through one or more public methods only.
Encapsulation in Java is achieved through the access modifiers- public, private, protected and default.


