Encapsulation

  1. Provide public setter and getter methods in class to modify values of variable.
  1. School bag contains books, pens that are binded together and form single unit
  1. Setter methods helps to set the value of variable in class. If your class doesn’t have setter method then you cant write/modify that variable that is variable can be read-only
  2. Getter method helps to get the value of variable in class. If your class doesn’t have getter method then you cant read that variable i.e it can be only write-mode operation only.
  3. Encapsulated code is more flexible and easy to update
  1. Slow the execution
class A 
{
private int x = 10;
}
class B extends A
{
int y = 10;
}
class C extends A
{
private int z = 10;
}
class P 
{
int a = 10;
}
class Q extends P
{
private int b = 10;
}
class R extends Q
{
private int z = 20;
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store