© Ron Dai 2019
R. DaiLearn Java with Mathhttps://doi.org/10.1007/978-1-4842-5209-3_27

27. Encapsulation and Polymorphism

Ron Dai1 
(1)
Seattle, WA, USA
 

In addition to “abstraction” and “inheritance,” there are another two principles in OOP, “encapsulation” and “polymorphism.”

Encapsulation

You may have heard the phrase “information hiding,” which intends to conceal the detailed implementations of an object behind a higher level of abstraction. Information hiding is mainly for security concerns, while encapsulation is to keep data and class implementation details inside a class for complexity concerns. However, encapsulation combines internal data and methods and enables its internal data to be accessible from outside through its public methods. And the class has private instance variables that are only accessible by methods in the same class. This helps managing code that is to be updated frequently. This is known as: “encapsulate what varies,” which is one of the best practice design principles.

In the Student class created in an earlier chapter, we have the following private field and public methods.
  • private int age; ../images/485723_1_En_27_Chapter/485723_1_En_27_Figa_HTML.gif only accessible from inside class

  • public void setAge(int age); ../images/485723_1_En_27_Chapter/485723_1_En_27_Figa_HTML.gif setter accessible from outside

  • public int getAge(); ../images/485723_1_En_27_Chapter/485723_1_En_27_Figa_HTML.gif getter accessible from outside

This is a simple example of encapsulation, in terms of how we set a student’s age value and how we access the age information.
public class TestStudent {
       public static void main(String[] args) {
              Student student = new Student("John",  "Doe");
                /*
                  student.age = 20;
                  This line will give compiler error
                  age field can't be used directly as it is private
                */
              student.setAge(20);
              System.out.println("Student name: " + student.getFirstName() + " " + student.getLastName() + "; age: " + student.getAge());
       }
}

There is a difference between abstraction and encapsulation. Abstraction is hiding complexity (i.e., implementation details) by using interfaces, while encapsulation is wrapping code (i.e., implementation) and data (i.e., value of variables) within the same class.

Polymorphism

“Poly” means many. “Morph” indicates form or shape. “Polymorphism” is an object’s ability to present the same interface with many different forms. There are many examples of this in Java programming design.
  • With one interface, we can create multiple classes. Each class implements the same method with different details.

  • In the basic class design, we can create multiple constructors with different input parameters.

  • Similarly , we can use the same method name with a different set of input parameters in a class design. This is also called “overloaded methods.”

  • In a subclass, we can “override a method” defined originally in its superclass.

Problems

  1. 1.

    Write an interface called GeometricObject, which declares two abstract methods: getPerimeter() and getArea().

     
  2. 2.

    Write the implementation class Circle, with a protected variable radius, which implements the interface GeometricObject.

     
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.168.172