+ 1

Encapsution - Java

I need to know exactly what is encapsulation. Detailed Explanation Please.

6th Mar 2019, 11:47 AM
Adhit Narayanan
Adhit Narayanan - avatar
8 odpowiedzi
+ 11
Encapsulation is basically hiding something from the main code. This is done using accessibility modifiers.
6th Mar 2019, 12:13 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 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.
8th Mar 2019, 7:03 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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)
6th Mar 2019, 12:16 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 2
Can you give me an example using program?
6th Mar 2019, 12:14 PM
Adhit Narayanan
Adhit Narayanan - avatar
6th Mar 2019, 12:34 PM
Seniru
Seniru - avatar
+ 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.
6th Mar 2019, 9:27 PM
M_N
+ 1
Thanks
6th Mar 2019, 12:25 PM
Adhit Narayanan
Adhit Narayanan - avatar
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; }
7th Mar 2019, 4:31 PM
Harsh Shah