Java Encapsulation Concept not clear

后端 未结 12 2402
死守一世寂寞
死守一世寂寞 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:00

    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.

    Encapsulation in Java 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.

    Real-time example: cars and owners. All the functions of cars are encapsulated with the owners. Hence, No one else can access it..

    The below is the code for this example.

    public class Main {
    
      public static void main(String[] args) {
        Owner o1=new Car("SONY","Google Maps");
        o1.activate_Sunroof();
        o1.getNavigationSystem();
        o1.getRadioSytem();
     }
    }  
    //Interface designed for exposing car functionalities that an owner can use.
    public interface Owner {
         void getNavigationSystem();
         void getRadioSytem();
         void activate_Sunroof();
    }
    /*
    Car class protects the code and data access from outside world access by implementing Owner interface(i.e, exposing Cars functionalities) and restricting data access via private access modifier.
    */
    public class Car implements Owner {
                    private String radioSystem;
                    private String gps;
    
                    public Car(String radioSystem, String gps) {
                        super();
                        this.radioSystem = radioSystem;
                        this.gps = gps;
                    }
    
                    public String getRadioSystem() {
                        return radioSystem;
                    }
    
                    public void setRadioSystem(String radioSystem) {
                        this.radioSystem = radioSystem;
                    }
    
                    public String getGps() {
                        return gps;
                    }
    
                    public void setGps(String gps) {
                        this.gps = gps;
                    }
    
                    @Override
                    public void getNavigationSystem() {
                        System.out.println("GPS system " + getGps() + " is in use...");
                    }
    
                    @Override
                    public void getRadioSytem() {
                        System.out.println("Radio system " + getRadioSystem() + " activated");
                    }
    
                    @Override
                    public void activate_Sunroof() {
                        System.out.println("Sunroof activated");
                    }
    }
    

提交回复
热议问题