Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Java Classes: Overview and Implementation

In this article we will learn about the java classes and their implementation in java programming.

The standard definition of a class is “A template for creating objects which defines their states and behaviours”. Sometime a class is termed as a blueprint from which objects are created. State and behaviour of a class can be termed as its fields and methods respectively.

Once we start coding in java programming, we need to create some classes in which we write our code. A class is the basic building block of any application. In java one file may contain more than one java class, and there are some rules associated with a java source file.

  • In a java source code file there may be only one public class but it can have multiple non-public classes.
  • In case there is any public class present in source code file, name of the file should be the class name.
  • If java source file do not have any public class then java file can have different name than the class.

Declaring Classes

Below is the basic template of a class that you normally see in applications:

  class MyTemplateClass
{
 // field, constructor, and 
 // method declarations
}
Area between the braces, known as the class body, contains basic
building elements of a class like, declarations for the fields that provide the
state of the class and its objects, and methods to implement the behavior of
the class and its objects.

In general, class declaration should apply the following points hierarchy :

  1. The first thing comes while declaring a class is modifiers such as public, private, and protected.
  2. The class name, with the initial letter capitalized by convention.
  3. The name of the parent class i.e superclass if exist/applicatble, preceded by the keyword extends. A class can only extend (subclass) one parent, multiple inheritance is not allowed in java.
  4. A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
  5. The class body, surrounded by braces, {}.

You can provide more information about the class, such as the name of its superclass, whether it implements any interfaces, and so on, at the start of the class declaration. For example:

  class MyTemplateClass extends MySuperClass implements myInterface {
    // field, constructor, and
    // method declarations
}

Means that MyClass is a subclass of MySuperClass and that it implements the YourInterface interface.

Declaring Member Variables

In java we can categorize the variables in 3 different types:

  • Member variables in a class—these are called fields.These member variable can be static, non-static, final etc.
  • Variables in a method or block of code—these are called local variables.
  • Variables in method declarations—these are called parameters.

Member variables in a class i.e. Class variables also known as Static fields which share its identity/value among all the created objects of the class. When you declare a field to be static, only a single instance of the associated variable is created, which iscommon to all the objects of that class. Hence when one object changes the value of a class variable, it affects all objects of the class. We can access a class variable by using the name of the class. Static variables can be accessed even though no objects of that class exist. It is declared using static keyword.

Declaring Class Methods

As we have discussed, class behaviour can be defined in the methods of the class. Sometime methods are also known as functions. Methods can be of type instance method or static method.

Static methods/Class methods, similar to Class variables can be invoked without having an instance of the class. Class methods are often used to provide global functions for Java programs. For example, methods in the java.lang.Math package are class methods. You cannot call non-static methods from inside a static method.

The Object Class is the super class for all classes in Java. Some of the object class methods areEquals, toString(), wait() etc.

Creating Object of Class

When you create a new object, you use the new operator to instantiate the object. The new operator returns the location of the object which you assign o a reference type. Each time you create an object, a new set of instance variables comes into existence which defines the characteristics of that object. If you want to create an object of the class and have the reference variable associated with this object, you must also allocate memory for the object by using the new operator.

To Create Object ofa class <new>Keyword can be used.

<Class_Name>   ClassObjectReference = new <Class_Name>();

Accessibility of Class Members

In case of non-static methods/variables, we call a method of an object by naming the object followed by a period (dot), followed by the name of the method and its argument list, like this:

objectName.methodName(arg1, arg2, arg3). 

In case of static method/variable, we don’t require the creation of class instances. They can be directly called using the class name itself, like this ClassName.member.

Using the this Keyword in Class

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using ‘this’.

The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.

For example, the ThisDemo class was written like as follows:

  public class ThisDemo {
    public int x = 0;
    public int y = 0;
        
    //constructor
    public ThisDemo(int a, int b) {
        x = a;
        y = b;
    }
}

But it could have been written like this:

  public class ThisDemo {
    public int x = 0;
    public int y = 0;
        
    //constructor
    public ThisDemo(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

Each argument to the constructor shadows one of the object's fields — inside the constructor x is a local copy of the constructor's first argument. To refer to the ThisDemo field x, the constructor must use this.x.

Access Modifiers

Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control:

  • At the top level—public, or package-private (no explicit modifier).
  • At the member level—public, private, protected, or package-private (no explicit modifier).

A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package. A package can be termed as collection or group of related classes.

At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers: private and protected. The private modifier specifies that the member can only be accessed in its own class. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

Type Abstract Class

An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be sub-classed.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

abstract void moveTo(double deltaX, double deltaY); 

If a class includes abstract methods, then the class itself must be declared abstract, as in:

public abstract class AbstractDemo {
     // declare fields
     // declare nonabstract methods, abstract keyword 
     abstract void draw();
  }

When an abstract class is sub classed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.

Listing 1. Example of Class – object creation.

  package articles; 
  public class ClassDemo 
  {
       public static void main(String[] args){
          ClassArticle objClassArticle = new ClassArticle ();
           objClassArticle. showPrintInfo (); 
       }
  }
  class ClassArticle
  {
        public void showPrintInfo() {
            System.out.println("Calling from main Class.");        
   }
  }

Output Listing 1: Calling from main Class

Explanation Listing 1: Above listing shows a basic skeleton of a class ‘ClassArticle’ , As you will notice this class in not declared as public , this is because single java file can have only one public java class. So default access modifier will be applied to this class. In ClassDemo, we are creating objects from class ‘ClassArticle’ using new keyword, and accessing its method using (.) operator.

Conclusion:

In this tutorial we learned the basic concepts of classes in java, and got the idea how to create classes, their states and behaviours in form of variables and methods. Finally, we looked into a practical sample of using classes.



Computer system engineer who has 4 year of experience in the field our web-development ,FPGA, and java programming.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login