How to Create a Class in Java

How to Create a Class in Java

Classes are a fundamental part of object-oriented programming. A class is often considered to be an object factory or prototype. Classes are thereafter instantiated to create objects.  For example, if we take a real-life example, a car can be considered an object, and the class can be an automobile. The car will have a certain colour and weight that will be considered the attributes of the car. The methods associated with the car can be accelerate and brake. The class declaration contains the following components as listed below:

  1. Modifiers: Modifiers determine the scope of the object. Classes can either be public, private, or protected depending upon the functionality.
  2. Class Keyword – When defining a new class, the declaration must start with the keyword ‘class’.
  3. Class Name – The name of the class, must start with a capital letter followed by an alphanumeric combination. While it is not a mandate to start a class name with capital letters, this is a convention followed by all developers.
  4. Super Class – If the class you are creating is a subclass, it must have the name of the superclass in its declaration. It should be preceded by the keyword ‘extends’. Multiple inheritance is not allowed in Java. Therefore, a class can only extend one superclass at a time.
  5. Interfaces – A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. More than one interface can be implemented by a class.
  6. Body – The body of the class is enclosed within ‘{ }’ and contains the actual functionality of the class.

Sample Code to Create a New Class

The following code creates a class called Main.

public class Main
{
int x = 5;
}

Sample Code to Create a new Object

public class Main
{
int x = 5;
public static void main (String args [])
{
Main obj = new Main ();
System.out.println (obj.x);
}
}

 

Sample Code to create Multiple Objects of a Class

public class Main
{
int x = 5;
public static void main (String args [])
{
Main obj1 = new Main ();
Main obj2 = new Main ();
System.out.println (obj1.x);
System.out.println (obj2.x);
}
}

 

Using Multiple Classes

Objects created in one class can be accessed in another class. This leads to better organization of classes. The functionality of the classes can be segregated in a way such that one class has all the methods and attributes while the other class will contain the main method.

Example:

public class Main
{
int x = 5;
}

class Second
{
public static void main (String args [])
{
Main obj = new Main ();
System.out.println (obj.x);
}
}

 

OUTPUT

5

Java Class Attributes

Attributes of a class are also called the fields of the class. These are variables that can be used throughout the scope of the class.

Example

public class Main
{
in x = 5;
int y = 3;
}

 

Accessing Attributes

The attributes or fields of a class can be accessed using the dot syntax (.). In the following example, we will modify a class attribute by setting the class field to 40.

public class Main
{
int x;

public static void main (String args [])
{
Main obj = new Main ();
obj.x = 40;
System.out.println (obj.x);
}
}

 

OUTPUT

40

 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.