CodingBison

A Java class is a collection of variables (called Properties) and functions (called methods). Properties store the data that is of interest to the class and methods define the function/logic that handles the data. Thus, with a class, you get the complete package; Java objects are based on Java classes. You might say that a class is a blueprint of an object. So, what is an object? Well, an object is an instance of a class. If you find it confusing, then let us look at sample example to understand a class better. We have defined a class called "Movie". The properties (name, yearOfRelease) of a movie are defined with in a class.

 class Movie {
     // Properties of the Movie Class.
     // Variables.
     String name;
     int    yearOfRelease;

     // Methods.
     void setName(String movieName) {
         name = movieName;
     }

     String getName() {
         return name;
     }	

     void setYearOfRelease(int movieYearOfRelease) {
         yearOfRelease = movieYearOfRelease;
     }

     int getYearOfRelease() {
         return yearOfRelease;
     }	
 }

If you notice the "Movie" class, it does not have a main method, however a main method is required in order to start a Java program. So, let us write a main method that basically makes use of the class "Movie".

 class Movie {
     String name;
     int  yearOfRelease;

     void setName(String movieName) {
         name = movieName;
     }

     String getName() {
         return name;
     }	

     void setYearOfRelease(int movieYearOfRelease) {
         yearOfRelease = movieYearOfRelease;
     }

     int getYearOfRelease() {
         return yearOfRelease;
     }	
 }

 /* Name this source file as "TestMovieClass.java". */
 public class TestMovieClass {
     public static void main(String[] args) {
         Movie obj1 = new Movie();
         Movie obj2 = new Movie();
         String movieName;
         int    movieYearOfRelease;

         obj1.setName("Quantum of solace");
         obj1.setYearOfRelease(2008);

         obj2.setName("Skyfall");
         obj2.setYearOfRelease(2012);

         movieName = obj1.getName();
         movieYearOfRelease = obj1.getYearOfRelease();

         System.out.println(" Obj1 Movie Name: " + movieName +
                        ", Year of Release: " + movieYearOfRelease);

         movieName = obj2.getName();
         movieYearOfRelease = obj2.getYearOfRelease();

         System.out.println(" Obj2 Movie Name: " + movieName +
                        ", Year of Release: " + movieYearOfRelease);
     }
 }

We have done quite a few things in the above program, let us look into the details.



Figure: "Movie" Class

Compile/Run this program and here is the output:

  Obj1 Movie Name: Quantum of solace, Year of Release: 2008
  Obj2 Movie Name: Skyfall, Year of Release: 2012

We will see more about classes and objects in the coming sections.

Access specifiers

In order to restrict the scope of the elements, Java provides three types of access specifiers. Public, private, protected ( All of these are Java Keywords). In the above example, the scope of the class is defined as public, which means it can be accessed from any where in the application. Sometimes, your software design might need to be more choosy! For such cases Private and Protected would be come valuable. In addition to the above three, There is an other access specifier called as "Default access specifier". This specifier gets applied when none of the above 3 are specified. We will see more about all of these in the coming sections: Access specifiers.





comments powered by Disqus