Java Encapsulation Concept not clear

后端 未结 12 2350
死守一世寂寞
死守一世寂寞 2020-11-30 03:40

This is basic question but still i don\'t understand encapsulation concept . I did\'t understand how can we change the properties of class from other class.because whenever

相关标签:
12条回答
  • 2020-11-30 04:02

    Do you mean to say that the value of empid should be 10 and empname tom instead of 0 and null, if yes then-:

    1)memory to the variables is allocated at run time , and also deallocated after the program is terminated.

    2)Hence if you think that once if you give 10 to empid it should always be 10, it is not so , because empid is just a reference to a memory which is storing "10".

    3)So by deallocation , i mean that empid is no longer pointing to memory area storing 10, after the program terminates

    4)whenever you execute a new program , the empid is now pointing to other memory area , and that memory is allocated the the default value according t the respective datatype,in case of static variable. hence always 0 and null.

    0 讨论(0)
  • 2020-11-30 04:03

    The reason you are getting the output "print employe details:0 null" when running the Employee class is because those variables are not initialized. That is, you do not assign any values to them within the Employee class.

    Whatever you do within the EmployeeTest class will not affect the values of the variables in Employee the next time it is run. Consider each run of a Java program a "clean slate".

    On the point of encapsulation, you really should not be using the static keyword. If you are going for encapsulation check out the other answer to this question, it has a nice code sample for you to use.

    0 讨论(0)
  • 2020-11-30 04:05
    Encapsulation means combining data and code together(class). The main purpose of encapsulation is you would have full control on data by using the code.
    
    class Encap{
    
    private int amount;
    
    public void setAmount(int amount)
    {
    this.amount = amount;
    }
    
    Here, you can set the amount using the setAmount method, but value should be more than 100. So, i have the control on it.
    
    public void setAmount(int amount)
    {
    if(amount>100)
    this.amount = amount;
    }
    
    0 讨论(0)
  • 2020-11-30 04:09

    ENCAPSULATION is mechanism of wrapping methods and variables together as a single unit. for example capsule i.e. mixed of several medicines.

    variables of a class will be hidden from other classes as it will be declared private, and can be accessed only through the methods of their current class

    To achieve encapsulation in Java − * Declare the variables of a class as private. * Provide public setter and getter methods to modify and view the variables values. * The Java Bean class is the example of fully encapsulated class.

    0 讨论(0)
  • 2020-11-30 04:10

    Yeah, this can be a little confusing sometimes. Let's go step by step: First, you need to understand

    • What is encapsulation and why is it used.?

    Encapsulation is one of the four fundamental OOP concepts.Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.

    Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface.

    The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.

    Take a small example:

    public class EncapTest{
    
       private String name;
       private String idNum;
       private int age;
    
       public int getAge(){
          return age;
       }
    
       public String getName(){
          return name;
       }
    
       public String getIdNum(){
          return idNum;
       }
    
       public void setAge( int newAge){
          age = newAge;
       }
    
       public void setName(String newName){
          name = newName;
       }
    
       public void setIdNum( String newId){
          idNum = newId;
       }
    }
    

    The above methods are called Accessors(aka getters and setters). Now you might ask,

    • Why should you use accessors..? There are actually many good reasons to consider using accessors rather than directly exposing fields of a class.Getter and Setters make APIs more stable.

    For instance, consider a field public in a class which is accessed by other classes. Now later on, you want to add any extra logic while getting and setting the variable. This will impact the existing client that uses the API. So any changes to this public field will require change to each class that refers it. On the contrary, with accessor methods, one can easily add some logic like cache some data, lazily initialize it later. Moreover, one can fire a property changed event if the new value is different from the previous value. All this will be seamless to the class that gets value using accessor method.

    There are so many tutorials and explanations as to how and what are they. Google them.

    As for your, current problem:

    1. You have two different classes, each with a main. That is wrong. They will have different properties.
    2. Code change suggested by @Subhrajyoti Majumder is the correct one. Check the answer for solving the problem.

    In the meantime, read up on

    • Encapsulation
    • Accessors

    for a better understanding of the concepts. Hope it helps. :)

    0 讨论(0)
  • 2020-11-30 04:10

    of course, change on one object will not impact on another object. suppose you have a class student and all the children at your school are it's objects. if one leaves the school, this doesn't mean, every other student (object of student class) should leave the school too.

    Encapsulation is the concept of having your class variables as private, so that no one can directly play with your data members from outer world. but you provide the public method, to let the outer world play with your data member, the way you want them to. the nice coding example of encapsulation is given above by Subhrajyoti Majumder.

    (static members are same for all objects of the class. eg: static count variable, to count the number of student class objects. (number of students at school)).

    Edit as you asked for:

    Example:

    public class student{
        public String name;
        public student() {}
     }
    

    and in your main function, outer world can play with your class attributes as:

    student s = new student();
    s.name = "xyz";
    

    let's suppose, you don't want to let the outer world change your name attribute of object. then you should make name 'name' as private, and provide a public method to only view the name (get).

    Example:

    public class student{
        private String name;
        public student() {}
        public String getName(){
          return this.name;
          }
     }
    

    and now in your main method, you can only get the name object, and can't set it to new value, like you could do in first example.

    student s = new student();
    String sname = s.getName();
    

    and if you try:

    s.name = "newname";
    

    compiler will not allow you that. since you don't have permission to access the private members.

    0 讨论(0)
提交回复
热议问题