Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

encapsulation in java

public class Person {

    private String name;
    private int age;

    public  void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}
//parent class
public class Test {
    public static void main(String[] args) {
        Person p1 = new Person();
        p1.setName("Sabbir");
        System.out.println(p1.getName());

    }
}
Comment

Java Encapsulation

public class Person {
  private String name; // private = restricted access

  // Getter
  public String getName() {
    return name;
  }

  // Setter
  public void setName(String newName) {
    this.name = newName;
  }
}
Comment

encapsulation java

Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates. Another way to think about encapsulation is, it is a protective shield that prevents the data from being accessed by the code outside this shield. 
Comment

encapsulation in java

1-ENCAPSULATION: We can hide direct access to data by using
private key and we can access private data by using getter and
setter method.
Comment

PREVIOUS NEXT
Code Example
Java :: how to print array in one line in java 
Java :: convert string to boolean java 
Java :: change element in array java 
Java :: mergesort java 
Java :: custom dialog in android 
Java :: java list change element position 
Java :: access array elements java 
Java :: array index out of bound exception in java 
Java :: java stream index 
Java :: convert from integer to character java 
Java :: what is ioexception in java 
Java :: Java Change ArrayList Elements 
Java :: java binary tree 
Java :: each loop in java 
Java :: the java_home environment variable is not defined correctly on mac 
Java :: use custom font java 
Java :: java check if dates are the same day 
Java :: how to remove an element from an arraylist java 
Java :: try catch in java 
Java :: java convert int to string 
Java :: java mahout get clusters centers 
Java :: update a key in the firebase realtime database java 
Java :: reading 2d array in java 
Java :: method object class in android 
Java :: exponet 
Java :: How to define lambda expression in Java? 
Java :: trier un tableau de string java 
Java :: pack in swing 
Java :: unique numbers in array java 
Java :: data validation dialog box android 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =