Thursday, August 8, 2024

Encapsulation

 Encapsulation :

Encapsulation is the concept of wrapping data (variables) and methods (functions) into a single unit, typically a class.

It is a fundamental principle of object-oriented programming (OOP) that helps in hiding the internal state of an object and only exposing the necessary functionalities.

 

               //ex:   simple class is example for encapsulation

-//  data hiding can be done using private k/w

ex:

package AccesspecifiersInheritance;

class A1

{

               //  combining data and Methods in single unit is called Encapsulation

               //ex:   simple class is example for encapsulation

               //  data hiding can be done using private k/w

               int a=10;

               private int b=20;//  can be accessed only in class - A1 but not outside class

               void M1()

               {

                              System.out.println("Calling M1");

               }

}

package Inheritance;

public class Encapsulation

{

               public static void main(String[] args)

               {

                              //create obj for class- A1

                              A1 a1ref = new A1();

                             

                              // access private variable

                              a1ref.b //  using private var-We can perform data hiding

               }

}

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...