Encapsulation In Java

Encapsulation In Java

Pillars Of Object Oriented Programming

There are 4 pillars of OOP

  1. Encapsulation
  2. Abstraction
  3. Polymorphism
  4. Inheritance

General Overview of Class & Objects

  1. The class is considered a blueprint for the object. Example: building layout for the building Object

  2. Objects are physical Objects. Or when we actually allocate memory to it.

Note: Every class will consist of two things (not compulsary to have both)

  1. Behaviour/ activity--- called as method
  2. To store data --- called as variables
     class HelloWorld {
    public static void main(String[] args) {

        classes object=new classes();
        System.out.println(object.a);

    }
}

class classes{
int a=20;
}

We use . to access the variables inside the class

Private Variables

  1. In the above example we can access the variables of the class outside of the class, which can be a security issue
  2. To avoid this we can use private keyword examples: Screenshot 2022-12-03 134928.jpg

If we try to access it from outside we will get an error

The solution we use to methods getter and setter

example:

class HelloWorld {
    public static void main(String[] args) {

        classes object=new classes();
        object.setter(20);
        System.out.println(object.getter());

    }
}


class classes{

 private int a;

 public  int  getter(){
   return a;

 }

 public void setter(int x){
    a=x;
 }

}

In this way we achieve encapsulation.

Encapsulation

  1. In object-oriented computer programming (OOP) languages, the notion of encapsulation (or OOP Encapsulation) refers to the bundling of data, along with the methods that operate on that data, into a single unit.
  2. Containers are just one example of encapsulation in coding where data and methods are bundled together into a single package.
  3. A key benefits to hiding information about attributes and methods using encapsulation in programming is that it prevents other developers from writing scripts or APIs that use your code.

Shasowing Problem

class HelloWorld {
    public static void main(String[] args) {

        classes object=new classes();
        object.setter(20);
        System.out.println(object.getter());

    }
}


class classes{

 private int a;

 public  int  getter(){
   return a;

 }

 public void setter(int x){
    a=a;//here jvm will get confused if it is local variable or it is instance variable
 }

}

The solution is to use this keyword The other way of making a setter is called a constructor

Constructor

  1. Constructor is a specialized setter method. It is called the moment we create an Object Some Important Behaviour Of Constructors
  2. The first line of the constructor always has a call to super()
  3. If no constructor is provided by the programmer then JVM will insert a default constructor
  4. The default constructor needs to be non-parameterized
  5. If we make a parameterized call to the default constructor we will get a compile time error
  6. Default constructor is inserted by JVM only if we do not have any constructor given be the programmer
  7. There is no return value of a constructor

example

  1. Constructor By Programmer example:
class HelloWorld {
    public static void main(String[] args) {

        classes object=new classes(20);

        System.out.println(object.getter());


    }
}


class classes{

 private int a;

 classes(int number){
    this.a=number;

 }

public int getter(){
    return this.a;
}
}
  1. Default Constructor
 class HelloWorld {
    public static void main(String[] args) {

        classes object=new classes();

        System.out.println(object.getter());


    }
}


class classes{

 private int a;


public int getter(){
    return this.a;
}
}

Note: this() is used to call the class constructor and super() is used to call the parent constructor.Both this() and super() cannot exist together