Head First Java Chapter 04 summary

Udesh Kavinda
4 min readOct 20, 2021

Bullet Points

  • Classes define what an object knows and what an object does.
  • Things an object knows are its instance variable.
  • Things an object does are its method.
  • Methods can use instance variables so that objects of the same type can behave differently.
  • A method can have parameters, which means you can pass one or more values into the method.
  • The number and type of values you pass in must match the order and type of the parameters declared by the method.
  • Values passed in and out of methods can be implicitly promoted to a larger type or explicitly cast to a smaller type.
  • The values you pass as an arguments to a method can be a literal value or a variable of the declared parameter type.
  • A method must declare a return type, A void return type means the method doesn’t return anything.
  • if a method declares a non-void return type, it must return a value compatible with the declared return type.

Instance Variable Vs Local Variable.

  • Instance variable always gets a default value. these default values are
  • Instance variables are declared inside a class but not within a method.
  • Local variables are declared within a method
  • Local variables must be initialized before use

Equality of primitive or reference

  • Two primitives can be compared using the == operator.
  • When == is used for equality check of references it just tells if both reference point to the same Heap Address.

Encapsulation.

  • To support encapsulation, the instance variable should be hidden by the use of private access modifiers.
  • The instance variables can only be accessed using a getter or a setter method with public access modifiers.
  • Any place where a particular value can be used, a method call that returns that type can be used.

The whole idea behind encapsulation is to hide the implementation details from users. If a data member is private it means it can only be accessed within the same class. No outside class can access private data member (variable) of other class.

However if we setup public getter and setter methods to update (for example void setSSN(int ssn))and read (for example int getSSN()) the private data fields then the outside class can access those private data fields via public methods.

This way data can only be accessed by public methods thus making the private fields and their implementation hidden for outside classes. That’s why encapsulation is known as data hiding. Lets see an example to understand this concept better.

Example of Encapsulation in Java

How to implement encapsulation in java:
1) Make the instance variables private so that they cannot be accessed directly from outside the class. You can only set and get values of these variables through the methods of the class.
2) Have getter and setter methods in the class to set and get the values of the fields.

class EncapsulationDemo{
private int ssn;
private String empName;
private int empAge;
//Getter and Setter methods
public int getEmpSSN(){
return ssn;
}
public String getEmpName(){
return empName;
}
public int getEmpAge(){
return empAge;
}
public void setEmpAge(int newValue){
empAge = newValue;
}
public void setEmpName(String newValue){
empName = newValue;
}
public void setEmpSSN(int newValue){
ssn = newValue;
}
}
public class EncapsTest{
public static void main(String args[]){
EncapsulationDemo obj = new EncapsulationDemo();
obj.setEmpName("Mario");
obj.setEmpAge(32);
obj.setEmpSSN(112233);
System.out.println("Employee Name: " + obj.getEmpName());
System.out.println("Employee SSN: " + obj.getEmpSSN());
System.out.println("Employee Age: " + obj.getEmpAge());
}
}

Output:

Employee Name: Mario
Employee SSN: 112233
Employee Age: 32

In above example all the three data members (or data fields) are private which cannot be accessed directly. These fields can be accessed via public methods only. Fields empName, ssn and empAge are made hidden data fields using encapsulation technique of OOPs.

Advantages of encapsulation

  1. It improves maintainability and flexibility and re-usability: for e.g. In the above code the implementation code of void setEmpName(String name) and String getEmpName() can be changed at any point of time. Since the implementation is purely hidden for outside classes they would still be accessing the private field empName using the same methods (setEmpName(String name) and getEmpName()). Hence the code can be maintained at any point of time without breaking the classes that uses the code. This improves the re-usability of the underlying class.
  2. The fields can be made read-only (If we don’t define setter methods in the class) or write-only (If we don’t define the getter methods in the class). For e.g. If we have a field(or variable) that we don’t want to be changed so we simply define the variable as private and instead of set and get both we just need to define the get method for that variable. Since the set method is not present there is no way an outside class can modify the value of that field.
  3. User would not be knowing what is going on behind the scene. They would only be knowing that to update a field call set method and to read a field call get method but what these set and get methods are doing is purely hidden from them.

--

--