Skip to main content

Introduction to Java Programming

  What is Java? Java is a programming language and a platform. Java is a high-level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Firstly, it was called  Greentalk  and the file extension was  .gt . After that, it was called  Oak . Initially Java was designed for small, embedded systems in electronic appliances like set-top boxes. Platform:  Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API(Application Programming Interface), it is called a platform. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High performance, Multithreaded, Architecture neutral, Object-oriented, Interpreted, and Dynamic". Currently, Java is used in internet programming, mobile devices, games, e-business so

20. Encapsulation in Java

 

Encapsulation

  • Encapsulation is defined as the wrapping up of data under a single unit.
  • It is the mechanism that binds together code and the data it manipulates.
  • It is a protective shield that prevents the data from being accessed by the code outside this shield.
  • The variables or data of a class is hidden from any other class and can be accessed only through any member function of its own class in which it is declared.
  • This is achieved by declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables.
class Encapsulated {
    private String name;
    private int age;
    private double marks;

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public double getMarks() {
        return marks;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setMarks(double marks) {
        this.marks = marks;
    }
}

class Encapsulation {
    public static void main(String[] args) {
        Encapsulated obj = new Encapsulated();
        obj.setName("Deepak");
        obj.setAge(20);
        obj.setMarks(92.6);
        System.out.println("Name is: " + obj.getName());
        System.out.println("Age is: " + obj.getAge());
        System.out.println("Marks is: " + obj.getMarks());
    }
}

Output :

Name is: Deepak
Age is: 20
Marks is: 92.6

Difference between Encapsulation and Abstraction

  • The major difference between abstraction and encapsulation is that abstraction hides the code complexity while encapsulation hides the internal working from the outside world.
  • Abstraction is used to hide the unnecessary information or data from the user but shows the essential data that is useful for the user.
  • Encapsulation is used to bind up the data into a single unit called class. It prevents to access data members from the outside of the class.
AbstractionEncapsulation
It focuses on the external lookout.It focuses on internal working.
It solves an issue at the design or interface level.It solves an issue at implementation level.
It can be implemented by using abstract class and interface.It can be implemented by using the access modifiers (private, public and protected).
It is the process of gaining information.It is the process of containing the information.
The objects that help to perform abstraction are encapsulated.The objects that results in encapsulation need not be abstracted.

Previous

Next

Comments