Topics Covered

The following topics will be covered on this section:

  • How to declare a class
  • How to declare a class variable
  • How to define a class constructor
  • How to call a class constructor
  • How to define a class method
  • How to use class method
  • Class Access Control
  • Method Access control
  • How to create a java object

Class Declaration

From our previous tutorial we have shown already on how to declare a class. Basically a class is declared as below:

The keyword public from the above declaration is what we call access control. There are 4 possible access control that we can use:

  • public – the class when declared public will be accessible on all classes that import the package
  • protected – the class is accessible to all class on the same package and its subclass
  • no modifier – class is accessible to all class on the same package
  • private – you might not want to declare your class private not unless the class is an inner class because if you declare your class as private, it will not be accessible at all. This is helpful if the inner class implementation is only intended on  the parent class to be used.

After the keyword public we have implicitly call another keyword which is class. This is a reserved word which is intended only in declaring a class. After the class keyword, the class name to identity the class should come. There is general convention accepted by Java Community in naming a class, start with uppercase letter. By convention we mean that the code will still compile and run without following it but will make it unreadable. After the class name declaration, the class body comes next. Anything inside the opening and close bracket are called class body. This where the code implementation comes in.
The class body can consists variable declaration and method declaration. These member variables are the one that perform the actual implementation of the class.

Now that we have discussed the basic of a class, let’s tackle more on a advance class declaration.

There are two additional keyword we have added from the basic class declaration which are the following:

  • extends
  • implements

The extends keyword means that ClassName is a subclass of AnotherClass which means to say that the class ClassName inherits all the behavior of the parent class AnotherClass. As a general rule, a class can only have one parent class.

Meanwhile the implements keyword means that ClassName class implements the interface MyInterface. Basically an interface is like a blue print of a class, it doesn’t have any concrete implementation. It’s only sole purpose in life is to be implemented. A class can implement multiple interfaces. So if we want to add another interface on our class we just declare it after the keyword implements and separate the interfaces by comma.

Make a note that once we make use of the implement  keyword, we forced our class to provide a concrete implementation of all the methods that the interface provides.

Class Variable Declaration

Basically a class variable are those variables that is accessible on the entire class. Depending on the access modifier these variables can also be access outside the class.

 

From the above  example code snippet of the class DeskFan, we have declared quite a few variables which is the powerOn, currentGearSpeed, isOscillating and voltage. Please go over on our tutorial on the variable declaration to learn the naming convention in declaring a variable if you still not familiar with it.

There are two data types we have used from the above example:

  • int data type – for variable currentGearSpeed and voltage
  • boolean data type – for variable powerOn and isOscillating

As we have pointed already that there is a convention in naming a variable which is using camel case. You can choose to ignore it however it is highly discourage.

Notice the keywords public, private and protected before the data type declaration. Similar to class, variables has access modifiers too. So even we can declare the class as public we can still limit the visibility of the variables by using access modifiers. Why we have to go into trouble to do that? When coding a class, we might be using a lot of variables and method declaration. Exposing all of these variables would not make any sense at all. Let’s say we want to design a class that accepts a employee id variable and this class provides methods and variables that return all the employee information. Let’s say we have another class that calls this class that we have to get the employee information, it would not make sense for the calling class to see variables regarding database connections, or maybe data formatting. So basically what we gonna do is to make only those methods that gives detailed employee information as public and we hide the rest of the methods and variables by declaring it as private.

Method Declaration

On this section we will be showing on how to declare a class method. Let’s create a simple method.

 

Similar to class declaration and variable declaration, a method has access modifiers too. Why we have to consider the access modifiers? The same rationale as what we have given on variable declaration, this is necessary to hide the methods that we don’t want to exposed. So from the above example, we have used protected as we don’t want to exposed this method outside our package. After declaring the method as protected, we have put the return type which is in this case an Integer object. Depending on the desired implementation, we can put any object type as return object. Moving back to the example, we used Integer object type thus the calculateArea method returns an Integer value to whatever calls this method. This declaration forcefully make our method to absolutely return a value thus a keyword return on the end part of our method body. If you don’t want your method not to return any value, just put void as return type.

So far we have discussed the protected and Integer keywords on our method declaration. What comes next after the return type is the calculateArea. This is the method name which we will be using to call this code block whenever needed. So what are those variables after method name   calculateArea(int width, int length) ?  Basically the width and length are called method arguments. Any method can have zero method argument or can have multiple arguments. To declare a method argument, specify first the variable type and then proceed with the variable name. If you have multiple argument, separate those with comma.

Method Overloading

One of the most advance topic in handling java methods is the concept of method overloading. The idea behind this is to reuse existing method name to accommodate additional method parameters. Let’s say for example we have a class that handles calculation of areas of polygons and with that functionality we have declared a method calculateArea. Under normal circumstances we would be creating different method name to handle calculation of area of a