What is a Class in Java with Example

A Class can be defined as a template / blueprint for creating objects which defines its state and behavior. Class can have three major components such as variables,methods and constructors. We can create a class with all of these components or some of these or even none of these, but a class with no components is of no use. Lets take a look into the exo-skeleton of declaring a class first

Class in Java syntax:

 <<Access Specifier>> class <<Class Name>> extends
 <<Super Class Name>> implements <<Interface Name>>{}
  • Access specifier : Allowable Access specifier for a class are public and default. Access specifiers defines the range of a class where it can be accessed. If a class is public then it can be accessed from anywhere. If you have not added anything in front of a class then it is called as default access specifier. Default access falls between protected and private access, allowing only classes in the same package to access.
  • class keyword : The access specifier is followed by class keyword which symbolizes that this entity is a class.
  • Class Name : The class keyword is followed by a class name, which follows the rules of identifiers
  • Extending & Implement : A class can extends only 1 other class, whereas it can implement any number of interfaces.

Rules for declaring a class in Java

  1. A class can have only public or default access specifier, no other access specifier ( protected, private) can be applied to a class.
  2. default access can be seen only by classes within the same package
  3. Only abstract, static, final non-access modifiers can be applied to a class.
  4. An abstract class in Java can not be instantiated. It can have both abstract methods and concrete methods
  5. A final class in Java can not be sub-classed but object can be created for it.
  6. It must have the class keyword, and class must be followed by a legal identifier.
  7. All classes will be by default extending Java Object class( java.lang.Object ), in addition to it all the classes can extends one other class ( only one class ).
  8. It can implement any number of java interfaces each interface has to be separated by a comma.
  9. All the variables, methods, and constructors should be defined within the java class only.
  10. Each .java source file may contain only one public class. and can have any number of other visible classes.
  11. The source file name must match the public class name and it must have a .java suffix.

Variables in a class : 

A class can have the following variable types inside it.

  • Local Variables : Local Variables are the variables which are defined within the methods or constructors. Those type of variables will be initialized with in the method and will get destroyed once the execution of the method is completed.
public class Class_Example 
{
    public Class_Example() 
    {
        String value = "Test";// Local Variable inside a Constructor
    }

    public void disp() 
    {
        String name = "JavaInterviewPoint";// Local variable inside method
        System.out.println("Welcome " + name + "!!!");
    }
}
  • Instance Variables : Instance Variables are the one which is defined outside of methods or constructors. These variables get initialized when the class is instantiated and can be called only with the help of objects. These variables can be accessed within any method or constructor.
public class Class_Example 
{
    String name = "JavaInterviewPoint";//Defining a Instance Variable
    public Class_Example() 
    {
        //Using the Instance variable inside a constructor
        System.out.println("Welcome " + name + "!!!");
    }

    public void disp() 
    {
        //Using the Instance variable inside a method
        System.out.println("Welcome " + name + "!!!");
    }
}
  • Class Variables : Class Variables is almost similar to Instance variable except it has a static keyword in the front indicating the variable belongs to the java class and not to any instance. A class variable can be called directly with the class name like <<Class Name>>.<<Variable Name>>
public class Class_Example 
{
    static String name = "JavaInterviewPoint";//Defining a Class Variable
    public Class_Example() 
    {
        //Using the Class variable inside a constructor
        System.out.println("Welcome " + Class_Example.name + "!!!");
    }

    public void disp() 
    {
        //Using the Instance variable inside a method
        System.out.println("Welcome " + Class_Example.name + "!!!");
    }
}

Java class Constructor :

This is the second most important topic which comes into mind when we speak about a class. Every class will be having a constructor either we have to define it explicitly or the compiler will create a default constructor for you. A class can have any number of constructors. Every time when a new instance is created for the class the constructor will be called, the main rule here is that constructor name for a java class should be the same name of the class name and should not have any return type.

public class Class_Example 
{
    static String name = "JavaInterviewPoint";//Defining a Class Variable
    //Default Constructor
    public Class_Example() 
    {
        
    }
    //Parameterized Constructor
    public Class_Example(String name) 
    {
        this.name=name;
    }
}

Example of Class in Java

Lets create a real world example class “BMWCar” putting all the above learnt concepts.

We have a “Vehicle” interface which consist of two methods numberOfWheels() and speedOfVehicle() both will be declared here and the body will be given by BMWCar class.

“Car” is our super class here consisting of a simple carColor() method, the Car class will be extended by our BMWCar class.

Finally “BMWCar” class extends the “Car” class and implements “Vehicle” interface. In the main method we will be creating the object for the BMWCar and will be calling the individual methods.

public interface Vehicle 
{
    public void numberOfWheels();
    public void speedOfVehcile();
}
class car
{
    public void carColor()
    {
        System.out.println("Color of the car is \"Blue\"");
    }
}
public class BMWCar extends car implements Vehicle 
{
    int wheels = 4;//instance variable
    
    public BMWCar()
    {
        System.out.println("Default Constructor called");
    }
    @Override
    public void numberOfWheels() 
    {
        System.out.println("BMW Car has \""+wheels+"\"  wheels");
    }
    @Override
    public void speedOfVehcile() 
    {
        int speed = 50;//local variable
        System.out.println("BMW car is driving at \""+speed+"\" kmph");
    }
    public static void main(String args[])
    {
        //creating object for the child class
        BMWCar bmwCar = new BMWCar();
        //Calling parent class method
        bmwCar.carColor();
        //Calling child class methods
        bmwCar.numberOfWheels();
        bmwCar.speedOfVehcile();
    }
}

Output :

Default Constructor called
Color of the car is "Blue"
BMW Car has "4"  wheels
BMW car is driving at "50" kmph

Leave a Reply

Your email address will not be published. Required fields are marked *