Novice Info : Java Classes Demystified

In my previous post, I have explained about the differences between a Java Class and an Interface, in this post we are going to dwell deep into Java Classes. I will be covering the following concepts in this post.

  • What is a Class
  • What happens when you create an object of a Class
  • What are different types of classes in java
  • Generics in Classes

What is a Class :
“A class is the blueprint from which individual objects are created.” – as per the java tutorials in Sun oracle documentation.
Simply put, a class is a digital skeleton of an external world object, which can store the state, and has a definite behavior. Using this skeleton we can create many objects in the Virtual environment (JVM), all of these objects will have the same behavior, with a state of their own.
Difference between, class and Class : In java, “class” with a small “c” letter is a keyword that is used to define a skeleton/Blueprint. A “Class” with a capital “C” letter is a generic type, that is the name of the class, which is used to create objects.

   1: public class Car {

   2:  private String brand;
   3:  private String color;
   4:  public Car(String brand, String color){
   5:   this.brand = brand;
   6:   this.color = color;
   7:  }
   8:  public void drive(){
   9:   …
  10:  }
  11:  public void applyBreaks(){
  12:   …
  13:  }
  14:}

In the above Car Class, we have defined a couple of variables called “Brand”, “Color”. They store the state of the Car Objects. All the Car objects have a definite behavior that is defined as the methods “drive()”, “applyBreaks()”. The Car class is a logical structure of the real car entity.

The Objects created from the Car class (Skeleton) will have a state initialized at the time of creation of an Object for this class, using the constructor of the class (as shown in the code snippet below). Every Class defined with the “class” keyword, will have a Has-A relationship with the “Class” object in “java.lang” package.

Note : Class objects will created by the Classloader of the JVM, when an object of that type is instantiated.

What happens when you create an object of a Class

When an Object is created for any class, the JVM will create an Object for the Class(Car) type using the Classloader and then it will call the constructor to create a state for the object, and returns the reference of the object created for the Car type.

A Constructor is a way to create a new Object for any type. If there is no Constructor defined for the class, then the Compiler will add a default no-argument constructor into it. The first line of the Constructor has to be a “super()” or “this()” constructor. If there is no line like this, then the compiler will add the “super()” constructor by default. If a Constructor is defined, then the compiler will not add a no-arg constructor.

   1: /* Example 1 */

   2: public class Vehicle{
   3:    // Default Constructor with no arguments is added by default.
   4: }
   5: 
   6: 
   7: /* Example 2 */
   8: public class Vehicle{
   9:   public Vehicle(int wheels){
  10:      // Compiler will add a default super constructor
  11:     ….
  12:    }
  13:   /* No Arg Constructor need to be added if you need to when a
  14:   parameterized constructor is defined */
  15:   public Vehicle(){
  16:    this(4); /* super construtor will not be added as we have called this constructor */

  17:    }

  18: }

 

 What are different types of classes in java

  • Abstract Class
  • Inner Class
  • Local Class and Anonymous Class
  • Static Class

This section is to give an introduction to various types of classes and not a comprehensive set of the features, of these classes.

Abstract Class : An Abstract Class is a class, where they will have at least one abstract methods. (Abstract methods are the that has no definition / body). These classes cannot be instantiated (Objects for these classes cannot be created).

   1: public abstract class Vehicle{

   2:  public abstract void drive();
   3:  public void shiftGeat(){}; // This method is having the
   4: }

Inner Class : An Inner Class is a class defined with in a class.

   1: public class Vehicle{

   2:   …
   3:   public class Engine {
   4:     …
   5:   }
   6:     …
   7: }

Local Class and Anonymous Class:

Local classes are a special type of Inner classes, that are defined in the body of a method in a class. Similarly, Anonymous classes are a a type of Local Classes defined inside a method without a class name.

   1: public interface Event{

   2:   public abstract void action();
   3:   }
   4: 
   5:   public class Vehicle{
   6:   public void break(){
   7:      public class ABS extends Thread { // Local Class
   8:        ….
   9:      }
  10:     ABS abs = new ABS();
  11:     abs.start();
  12:   }
  13:  public void applyBreaks(){
  15:      Event e = new Event(){ // Anonymous Class
  16:      public void action(){
  17:      ABS abs = new ABS();
  18:      abs.start();
  19:      }       
  20:   };
  21:   e.action();
  22:  }
  23: }

 Generics in Classes

Generics in Classes, helps to identify the Cast errors at the compile time, instead of capturing it late at runtime. Generics again is a vast concept, which I feel might need its own blog, hence limiting the discussion about it to the basics.

Generics allows to pass Type parameters to the classes, and are defined to restrict the attempt of using it to ineligible objects.

   1: public Class Box<T> {

   2:    Map<T,Integer> tBox;
   3:    public Box(T t){
   4:      this.tBox = new HashMap<T,Integer>();
   5:    }
   6:    public void add(T t,int quantity){
   7:      tBox.put(t,quantity);
   8:    }
   9:    public Map<T,Integer> get(){
  10:     return this.tBox;
  11:    }
  12: }
  13: 
  14: public class TestBox{
  15:    public static void main(String[] aa){
  16:    Box<Chocolates> chocolateBox;
  17:    Box<Fruits> fruitBox;
  18:    choclateBox= new Box<Chocolates>();
  19:    fruitBox = new Box<Fruits>();
  20:    chocolateBox.add(Dairymilk,5);
  21:    chocolateBox.add(Cadbury,2);
  22:    chocolateBox.add(Apples,1); // Will throw a compile time error.
  23:    fruitBox.add(Apples,5);
  24:    fruitBox.add(Oranges,3);
  25:    chocolateBox = fruitBox.get(); // Will throw a compile tiime error.
  26:
  27: }

As shown in the above example, Box class ensures strict type checking with the objects created and hence will not result in any runtime issues, unless it is used in Raw type.

Hope, this is useful and you have got some information about the Java Classes. Please share your thoughts and suggestions in the comments section.

Advertisements

About Sharath

I am a passionate technologist, I have varied interests in Development, Testing and Maintenance phases of PDLC / SDLC. In my leisure I love to play with my daughter. I love eating out, hanging out with friends and reading blogs/articles.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s