Skip to main content

Encapsulation

Encapsulation 

The process of binding data and corresponding methods into a single unit is nothing but encapsulation.

E.g.-:

class Student{
   data members
        +
    methods
}

If any component follows data hiding and abstraction, such type of component is said to be an encapsulated component.

Encapsulation = data hiding  + abstraction

eg-:

public class Account{
private double balance;

public double getBalance(){
    //validation
    return balance;
}
public void setBalance(double amount){
    //validation
   this.balance=amount;
}
}

The main advantage of encapsulation are -:
  • We can achieve security.
  • Enhancement will become easy
  • it improves maintainability of the application.
The main disadvantage of encapsulation are -:
The main disadvantage of encapsulation is it increases the length of the code and slows down execution.

                                                              

    Tightly Encapsulated Class


A class may be tightly encapsulated, if and only if each and every variable declared as private. Whether class contain carsponding getter and setter method are not, whether these methods are declared as public are not these things that we are not required to check.

E.g.-:

public class Account{
private double amount;
public double getAmount(){
    return amount;
}
}


Ques-: Which of the following class are tightly encapsulated?

A. class A{
          private int x;
      }
B.  class B extends A{
            int y;
      }
C.  class C extends A
         private int d;
      }
D.  class D extends B{
        private int z;
      }

Ans-: A,C.


Note-: If the parent class is not tightly encapsulated, then no child class is tightly encapsulated.
E.g.-:

class A{
         int x;
      }
 class B extends A{
           private int y;
      }
class C extends B
         private int d;
      }

Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Final Variable

Final Instance Variable If the value of a variable changes from object to object, such a variable is called an instance variable . For every object, a separate copy of the instance variable will be created. Instance variables do not require explicit initialization; JVM always provides default values. Example: class Test { int x; // instance variable public static void main(String... args) { Test t = new Test(); System.out.println(t.x); // output: 0 } } If the instance variable is declared as final , then explicit initialization is mandatory. JVM will NOT provide default values. Example: class Test { final int x; } Compile-time Error: variable x might not have been initialized. Rule: A final instance variable must be initialized before constructor completion . Possible places for initialization: 1. At the time of declaration class Test { final int x = 10; } 2. Inside an instance blo...