Wednesday, June 15, 2016

Class and Object

Class is the core of Java programming. Why? Because Java is a programming language that support and implement full object-oriented concept. Each Java program is a class. This means, every concept or case of programming that you want to be implemented in Java, it should be wrapped into a class.

What Is Class?


So far, actually we have been using class in every programming we wrote. However, classes we already use is still very minimal, which is only intended to demonstrate the syntax in Java programming. The class is a class that acts as main class (main program), which contains main() method.

Classes can be defined as a blueprint that defines variables (data) and methods (behavior) of an object. Take a look of example object: car. Car has data such as brand, type, color, and so on. Car also has a specific behaviors that can differentiate between one and the other cars, such as the braking system, gear, and so forth. Now let us take the example of other object: human. Human has data or physical characteristics, such as: name, height, weight, fingerprint, hair color, and so on. In addition, human also has behaviors such as: how to walk, how to speak, and so forth. In the program, such objects can be defined as a class.

In programming, class actually not much different from the simple data type explained in previous post. The difference, simple data type used to declare normal variable, while the class is used to declare a variable to be an object.

It is important to note that the class is still abstract. By the time we create a new class, meaning we have defined a new data type. Once defined, the new data type can be used to create an object of that type. In other words, the class is a template for object creation, and the object is a concrete manifestation (instance) of a class. For example, human is class; whereas the object instance or a concrete manifestation of the human class is Larry, Patrick, Lisa, and others.

Defining Class


In Java, a class defined using keyword class. Here is the general form used to define a class:

class ClassName {
   type data1;
   type data2;
   ...
   type dataN;

   type method1(parameter) {
      // code for method1
   }

   type method2(parameter) {
      // code for method2
   }

   type methodN(parameter) {
      // code for methodN
   }
}

Data or variable defined within a class are often referred to the instance variables. The value of these data will be accessible via method.

Simple Class Example


Now we will begin creating a simple class. Here we will create a class Box, which has data: length, width, and height. For now, we do not need to add a method to the class. Method will be explained in the next post.

Consider the following example:

class Box {
   double length;
   double width;
   double height;
}

From the code above, we have defined a new data type named Box. It is important to remember that the class definition will only make a template, instead of creating object. The actual object of the class must be made through the following code:

Box b = new Box(); //Create the object of Box class with the name of b

Here, Box is a class and b is the object or instance of the class Box. Through the object b, we can access and manipulate the data contained in Box class, by using the dot operator (.), As shown in the code below:

b.length = 4;
b.width = 3;
b.height = 2;

The code is used to fill value to the length, width, and height, which is owned by the object b with value 4, 3 and 2. For more details, Take a look at the following full program which will show the use of the class Box above.

class Box {
   double length;
   double width;
   double height;
}

class BoxDemo1 {
   public static void main(String[] args) {

      double volume;
      Box b = new Box();

      //Load value into the data of b object
      b.length = 4;
      b.width = 3;
      b.height = 2;

      //Calculate b volume
      volume = b.length * b.width * b.height;

      //Display the volume to the screen
      System.out.println("box volume = " + volume);
   }
}

The above source code should be saved into a file with the name BoxDemo1.java, not Box.java. The reason is the main() method resides in BoxDemo1 class. At compilation time, the above program will produce two .class files: Box.class and BoxDemo1.class. This happens because in Java, every class will be compiled into its own .class file. If you want to run the program, then you have to execute the file BoxDemo1.class, not Box.class. Here is the result that will be provided by the program.

box volume = 24.0

The program above also can be written in two classes. Every class is written in a single .java file. Thus, in the above case we can make two files .java: Box.java and BoxDemo1.java. The source code should you write to in the two files are as follows:

a. The content of Box.java file
class Box {
   double length;
   double width;
   double height;
}

b. The content of BoxDemo1.java file
class BoxDemo1 {
   public static void main(String[] args) {

      double volume;
      Box b = new Box();

      //Load value into the data of b object
      b.length = 4;
      b.width = 3;
      b.height = 2;

      //Calculate b volume
      volume = b.length * b.width * b.height;

      //Display the volume to the screen
      System.out.println("box volume = " + volume);
   }
}

Please be noted that any object or instance of a class will have its own copy of data. This means, an object with another object, the value of the data can be different. Take a look at the code example below:

class Box {
   double length;
   double width;
   double height;
}

class BoxDemo2 {
   public static void main(String[] args) {

      double volume1, volume2;

      Box b1 = new Box(); //declaring object b1
      Box b2 = new Box(); //declaring object b2

      //Load value into b1 object
      b1.length = 4;
      b1.width = 3;
      b1.height = 2;

      //Load value into b2 object
      b2.length = 6;
      b2.width = 5; 
      b2.height = 4;

      //Calculate volume of b1 object
      volume1 = b1.length * b1.width * b1.height ;

      //Calculate volume of b2 object
      volume2 = b2.length * b2.width * b1.height ;

      //Display the volume of b1 and b2 to the screen
      System.out.println("Volume b1 = " + volume1); 
      System.out.println("Volume b2 = " + volume2);
   }
}

Save the above source code into the file BoxDemo2.java. At run time, the above program will give the following result:

Volume b1 = 24.0
Volume b2 = 120.0

As shown in the above code that object b1 and b2 is an instance or a concrete manifestation of the class Box. The value of the data contained on the two objects can be different so that the value of the produced volumes would be different.

3 comments:

  1. Thank You for Sharing valuable information with us.

    E-Commerce is the fastest-growing platform which is becoming more demandable day by day this is a tremendous form of the vibrant sector that is bringing several opportunities to the new generation to become more skilled and professional. Taking up this great advantage in the E-Commerce sector would need the right path and the right skillset you need to learn the latest technologies and high-demand skills such as building payment gateways and developing an accessible and easy website along with the basics of E-commerce that‘s why Aptech learning official Pro E-Commerce training program in Dwarka, Janakpuri, and Gurgaon which will help to make a successful career in this booming industry. Java Programming Course helps to become an expert in Java web application development. It helps you to bring efficiency in collecting and analyzing the data through Java programming in this course you will learn effective web UI/UX design and Secure web applications by using the advanced technologies of the Java program.

    For more details please visit our website : Aptech Dwarka

    ReplyDelete