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 :: random value between 10-20 
Java :: java array quick sort 
Java :: get value from Spring application.properties 
Java :: syntax of the switch statement in Java 
Java :: how to start array index from 1 in java 
Java :: streams in java 
Java :: get ocurrences in array java 
Java :: constructor of class that extends another class 
Java :: multiple inheritance in java 
Java :: islowercase java 
Java :: stream reduce 
Java :: how to get a set from a map in java 
Java :: menu alert dialog in android 
Java :: new date api in java 8 
Java :: java hashmap 
Java :: matrix dimensions 
Java :: Add Elements in java map 
Java :: instantiation in java 
Java :: java hashmap set value 
Java :: Java How to use NavigableMap? 
Java :: how to choose an element in a array list Java 
Java :: h2 database spring boot create table application.properties 
Java :: android pick up photo from local device 
Java :: strictfp java example 
Java :: sum of no 
Java :: no main attribute in java android 
Java :: java 8 lambda delete from list 
Java :: class c { public static void main(string[] args){ system.out.println("hello"+args[0]);}} output 
Java :: how to cut a certion part from a string in java 
Java :: Java @FunctionalInterface annotation 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =