Getters And Setters

Lesson 28
Author : Afrixi
Last Updated : November, 2017


Table of Content

Code

Copypublic class Movie{
     private String title;
     private String rating;

     public Movie(String title, String rating){
          this.title = title;
          this.setRating(rating);
     }

     public String getRating(){
          return this.rating;
     }
     public void setRating(String rating){
          if (rating == "G" || rating == "PG" || rating == "PG-13" || rating == "R" || rating == "NR"){
               this.rating = rating;
          } else {
               this.rating = "NR";
          }
     }
}