+ 1
Encapsution - Java
I need to know exactly what is encapsulation. Detailed Explanation Please.
8 Answers
+ 11
Encapsulation is basically hiding something from the main code. This is done using accessibility modifiers.
+ 12
Encapsulation
A fundamental object-oriented concept is 'encapsulation' , the bundling of data that represents the state of an object together with the code responsible for manipulating that data. In a Java program, the state of an object is represented by the value of its 'instance variables': data fields that represent the attributes of a type of object. You interact with a Java object (you send it messages) by invoking the object's 'instance methods': executable code that manipulates the object's instance variables.
To create an object in Java, you need a class. A 'class' encapsulates the instance variables and methods that define an object. The act of creating an object is sometimes called 'instantiation' , and objects themselves are sometimes called 'class instances'. A class serves as a blueprint from which you can instantiate objects that contain the instance variables and methods defined by the class.
+ 10
public class Hello{
public int w = 9;
private int u = 1;
}
Now in the main method,
System.out.println(Hello.w) works but not System.out.println(Hello.z)
+ 2
Can you give me an example using program?
+ 2
Take this lesson also!
https://www.sololearn.com/learn/Java/2162/
+ 2
In Java everything is an object. Every object has it's own Class.
If you need connection between your vars/methods from different Classes - use public.
If not - use private.
+ 1
Thanks
0
In encapsulation data members are bind with data function of the respective class.
We can declare data member as Private and use in member function.
Declaring data member as private is so that data member cannot be changed.
Example: (IN C++)
class A
{
private:
int a;
Public:
int getdata()
{
return a;
}
void setdata(int b)
{
a=b;
}
};
main()
{
int num;
A obj;
obj . setdata (50);
num=obj.getdata();
cout<<num;
}