There are three main features of OOPS:

  1. Encapsulation
  2. Inheritance
  3. Polymorphism

 
Wrapping of data and methods into a single unit is known as Encapsulation. It provides the security that keeps data and methods safe from unwanted changes. Class is the example of encapsulation which binds fields and members into a single unit.
To get encapsulation using

  • Declare the variables of a class as private.
  • Provides public getter and setter methods to change and sight the variables values.

 
Example

public class Addition{
private int sum;
public void settotal(int a, int b){
sum = a + b;
}
public void gettotal()
{
System.out.println("Addition of two numbers:" +sum );
}
public static void main(String []args){
Addition add = new Addition ();
add.settotal(10, 20);
add.gettotal();
}
}

Output
Addition of two numbers:30
Learn more about Cross-Platform Mobile Technology for Android and iOS using Java in this insightful blog now!

Leave a Reply

Your email address will not be published. Required fields are marked *