Classes in Java
A Java class is a set of object which shares common characteristics/ behavior and common properties/ attributes. There are certain points about Java Classes as mentioned below:
- Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are created.
- Class does not occupy memory.
- Class is a group of variables of different data types and a group of methods.
A Class in Java can contain:
- Data member
- Method
- Constructor
- Nested Class
- Interface
Declare Class in Java
access_modifier class<class_name>
{
data member;
method;
constructor;
nested class;
interface;
}
Example:
- Animal
- Student
- Bird
- Vehicle
- Company
Below is the implementation of the above topic:
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
Output 1
0 null
Output 2
GeeksForGeeks
Output 3
GeeksForGeeks
A class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type.
Components of Java Classes
 In general, class declarations can include these components, in order:
- Modifiers: A class can be public or has default access (Refer this for details).
- Class keyword:Â class keyword is used to create a class.
- Class name:Â The name should begin with an initial letter (capitalized by convention).
- 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.
- 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.
- 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.
