Java Classes

A class in Java is a set of objects which shares common characteristics/ behavior and common properties/ attributes. It is a user-defined blueprint or prototype from which objects are created. For example, Student is a class while a particular student named Ravi is an object.

Properties of Java Classes

  1. Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are created.
  2. Class does not occupy memory.
  3. Class is a group of variables of different data types and a group of methods.
  4. A Class in Java can contain:
    • Data member
    • Method
    • Constructor
    • Nested Class
    • Interface

Class Declaration in Java

access_modifier class <class_name>
{  
    data member;  
    method;  
    constructor;
    nested class;
    interface;
}

Example of Java Class

Java




// Java Program for class example
 
class Student {
    // data member (also instance variable)
    int id;
    // data member (also instance variable)
    String name;
 
    public static void main(String args[])
    {
        // creating an object of
        // Student
        Student s1 = new Student();
        System.out.println(s1.id);
        System.out.println(s1.name);
    }
}


Java




// Java program to Illustrate Creation of Object
// Using new Instance
 
// Main class
class GFG {
 
    // Declaring and initializing string
    String name = "w3wiki";
 
    // Main driver method
    public static void main(String[] args)
    {
        // Try block to check for exceptions
        try {
            Class cls = Class.forName("GFG");
            // Creating object of main class
            // using instance method
            GFG obj = (GFG)cls.newInstance();
            // Print and display
            System.out.println(obj.name);
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        catch (InstantiationException e) {
 
            e.printStackTrace();
        }
        catch (IllegalAccessException e) {
 
            e.printStackTrace();
        }
    }
}


Java




// Java program to Illustrate Creation of Object
// Using clone() method
// Main class
// Implementing Cloneable interface
class GFG implements Cloneable {
 
    // Method 1
    @Override
    protected Object clone()
        throws CloneNotSupportedException
    {
        // Super() keyword refers to parent class
        return super.clone();
    }
    String name = "w3wiki";
    // Method 2
    // main driver method
    public static void main(String[] args)
    {
        GFG obj1 = new GFG();
        // Try block to check for exceptions
        try {
            GFG obj2 = (GFG)obj1.clone();
            System.out.println(obj2.name);
        }
        catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}


Output 1

0
null

Output 2

w3wiki

Output 3

w3wiki

Components of Java Classes

 In general, class declarations can include these components, in order: 

  1. Modifiers: A class can be public or has default access (Refer this for details).
  2. Class keyword: class keyword is used to create a class.
  3. Class name: The name should begin with an initial letter (capitalized by convention).
  4. Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
  5. Interfaces(if any): 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.
  6. Body: The class body is surrounded by braces, { }.

Constructors are used for initializing new objects. Fields are variables that provide the state of the class and its objects, and methods are used to implement the behavior of the class and its objects.
There are various types of classes that are used in real-time applications such as nested classes, anonymous classes, and lambda expressions.

Classes and Objects in Java

In Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. The class represents a group of objects having similar properties and behavior. For example, the animal type Dog is a class while a particular dog named Tommy is an object of the Dog class.

In this article, we will discuss Java objects and classes and how to implement them in our program.

Similar Reads

Java Classes

A class in Java is a set of objects which shares common characteristics/ behavior and common properties/ attributes. It is a user-defined blueprint or prototype from which objects are created. For example, Student is a class while a particular student named Ravi is an object....

Java Objects

...

Ways to Create an Object of a Class

...

Anonymous Objects in Java

...

Difference between Java Class and Objects

An object in Java is a basic unit of Object-Oriented Programming and represents real-life entities. Objects are the instances of a class that are created to use the attributes and methods of a class.  A typical Java program creates many objects, which as you know, interact by invoking methods. An object consists of :...