Encapsulation In Java

Chapter: Java Basics Last Updated: 24-03-2017 04:04:19 UTC

Program:

            /* ............... START ............... */
                
public class Car {
	 
    public int maxSpeed = 200;
    public int price = 100000;
 
    /**
     * @return the maxSpeed
     */
    public int getMaxSpeed() {
        return maxSpeed;
    }
    /**
     * @param maxSpeed the maxSpeed to set
     */
    public void setMaxSpeed(int maxSpeed) {
        this.maxSpeed = maxSpeed;
    }
    /**
     * @return the price
     */
    public int getPrice() {
        return price;
    }
    /**
     * @param price the price to set
     */
    public void setPrice(int price) {
        this.price = price;
    }
}

public class CarFactory {
	 
    public static void main(String[] args) {
        Car car = new Car();
 
        car.maxSpeed = 250;    //valid but bad programming.
        System.out.println("Car max speed is: " + car.maxSpeed ); //valid but bad programming.
 
        car.setMaxSpeed(200);
        System.out.println("Car max speed is: " + car.getMaxSpeed());
 
        /*** Lets see how car price behaves***/
        car.setPrice(1050000);
        System.out.println("Car price is: " + car.getPrice());
 
        //compile error: private modifier protect instance variable to access directly from other class
        //private modifier do will not allow to set or get the price directly from out of class.
        car.price = 2000000;
        System.out.println("Car price is: " + car.price);
    }
}
                /* ............... END ............... */
        

Notes:

  • Encapsulation in java is a process of wrapping code and data together into a single unit.
  • The Java Bean class is the example of fully encapsulated class.
  • Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.
  • Benefits of Encapsulation
  • The fields of a class can be made read-only or write-only.
  • A class can have total control over what is stored in its fields.
  • The users of a class do not know how the class stores its data. A class can change the data type of a field and users of the class do not need to change any of their code.

Tags

Encapsulation, Java , Java Basics

Similar Programs Chapter Last Updated
Variables In Java Example Java Basics 03-11-2017
Java Program To Count Number Of Digits In An Integer Java Basics 29-08-2017
Java Program To Check Whether Number Is Positive Or Negative Java Basics 26-08-2017
Java Program To Multiply Two Floating Point Numbers Java Basics 25-08-2017
Java Program To Print Integer Using User Input Java Basics 25-08-2017
Java Parameterized Method Java Basics 17-05-2017
Java Float To Int Conversion Java Basics 31-03-2017
Nested Class In Java Java Basics 25-03-2017
Java Command Line Argument Java Basics 27-12-2016
Java Main Method Java Basics 06-11-2016
List Using Array In Java Java Basics 23-10-2016
Java Basic Program Example Java Basics 12-10-2016
Java Interface Example Java Basics 19-06-2016
Java Program For Multiplication Of Two Numbers Java Basics 18-06-2016
Java Program For Subtraction Of Two Integer Numbers Java Basics 18-06-2016
Java Program For Addition Of Two Integer Numbers Java Basics 18-06-2016
Java Program For Division Of Two Numbers Java Basics 18-06-2016
Java Class Example Java Basics 17-06-2016
Java Scanner Example Java Basics 02-05-2016
Object Array In Java Java Basics 02-05-2016
Constructor In Java Example Java Basics 02-05-2016
Constructor Chaining In Java Java Basics 02-05-2016
Java Clone Example Java Basics 02-05-2016
Casting In Java Java Basics 02-05-2016
String In Java Example Java Basics 08-08-2016
Static Method In Java Java Basics 08-08-2016
Java Method Example Java Basics 08-08-2016
Java Program To Add Two Numbers Java Basics 15-07-2016
Java Hello World Example Java Basics 06-02-2017

1