Video 40(ch9)

Yash
0



 

Program:-

class MyEmployee{
    private int id;
    private String name;
    public void setName(String a){
        name = a;
    }
    public String getName(){
        return name;
    }
    public void setId(int b){
        id = b;
    }
    public int getId(){
        return id;
    }
}
public class AccessModifierV40 {
    public static void main(String[] args) {
    MyEmployee harry = new MyEmployee();
    //harry.id = 45;
    //harry.name = "harry";-----> throws an error due to private access modifier.
        //Quick quiz: use getter and setter from the method.
        harry.setName("harry");
        System.out.println(harry.getName());
        harry.setId(45);
        System.out.println(harry.getId());
    }
}

Post a Comment

0 Comments
Post a Comment (0)
To Top