Access Modifier, Getter and Setter Method
Que 1. What do you mean by Access Modifiers?
Answer: Access Modifiers which decide the accessiblity/ visibility of the data members and member method of the class.
Que 2. How many types of Access Modifiers in Java?
Answer: Four types of Access Modiferes in the Java – (a) private (b) public (c) protected (d) default
Que 3. What is the default access modifiers of the class members in Java?
Answer: default.
It means accessible/ visible to the outside the class in the same package.
Que 4. What is private access modifiers ?
Answer: private access modifier means members are not accessed/ visible outside the class. These members are only accessed/ visible inside the class method memebrs.
Que 5. What is public access modifiers ?
Answer: public access modifier means members can accessed/visible outside the class. These members can accessed inside as well as outside the class.
Que 6. What is Getter Function?
Answer: A method member, which returns the value of any one data member, which is called Getter method. Getter method helps in reading the value of private data member.
Que 7. What is Setter Function?
Answer: A method member, which us used to cahnge/update the data member, are called Setter method.
Que 8. Give one example of class with Getter and Setter method.
Answer:
public class Book {
private String title;
private String author;
private String publisher;
private String genre;
private double price;
void display(){
System.out.println("Details of Book");
System.out.println("Title : "+title);
System.out.println("Author : "+author);
System.out.println("Publisher : "+publisher);
System.out.println("Genre : "+genre);
System.out.println("Price : Rs."+price);
}
Book(){
title = "";
author = "";
publisher = "";
genre = "";
price = 0.0;
}
Book(String t, String a, String pub, String g, double pr){
title = t;
author = a;
publisher = pub;
genre = g;
price = pr;
}
double getPrice(){
return price;
}
void setPrice(double pr){
price = pr;
}
}