Thursday, January 23, 2025
HomeProgrammingTypes of Classes in Java

Types of Classes in Java

In Java, classes are the blueprint for creating objects, and they define the properties (fields) and behaviors (methods) of those objects. There are several types of classes in Java, each serving a different purpose. Here are the main types:

1. Normal Classes (Regular Classes)

  • These are the most common classes in Java, defined with the class keyword. A normal class can contain fields, methods, constructors, and other class members.
public class Car {
    String make;
    String model;
    
    public Car(String make, String model) {
        this.make = make;
        this.model = model;
    }
    
    public void drive() {
        System.out.println("Driving a " + make + " " + model);
    }
}

2. Abstract Classes

  • An abstract class cannot be instantiated directly. It can have abstract methods (methods without a body) and concrete methods (methods with a body). Abstract classes are often used to define common behavior that can be shared among subclasses.
public abstract class Animal {
    public abstract void sound();
    
    public void sleep() {
        System.out.println("The animal is sleeping.");
    }
}

3. Interfaces

  • An interface is a reference type, similar to a class, that can contain only abstract methods (until Java 8, where default and static methods were introduced). Classes that implement an interface must provide concrete implementations for all its methods.
public interface Animal {
    void sound();
    void sleep();
}

4. Final Classes

  • A final class is a class that cannot be subclassed (extended). This is useful when you want to prevent inheritance and ensure the class remains unchanged.
public final class MathUtility {
    public static int add(int a, int b) {
        return a + b;
    }
}

5. Inner Classes

  • These are classes defined within another class. There are several types of inner classes:
    • Non-static Inner Classes: Associated with an instance of the outer class.
    • Static Nested Classes: Static classes defined inside another class.
    • Local Classes: Classes defined within a method.
    • Anonymous Classes: Classes without a name, used for instantiating objects in a single expression.
See also  Is There a CSS Parent Selector?

Example of an inner class:

public class OuterClass {
    private String message = "Hello from outer class!";
    
    class InnerClass {
        public void showMessage() {
            System.out.println(message);
        }
    }
}

6. Enum Classes

  • An enum is a special type of class that represents a group of constants. Enums are used to define variables that can have a fixed set of values, like days of the week or types of operations.
public enum Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}

7. Singleton Classes

  • A singleton class ensures that only one instance of the class exists in the Java Virtual Machine (JVM) during the application’s execution. It is typically implemented by making the constructor private and providing a static method to access the instance.
public class Singleton {
    private static Singleton instance;
    
    private Singleton() { }
    
    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

8. Lambda Classes

  • Though not a “class” in the traditional sense, lambda expressions are a way to represent instances of classes with a single method (functional interfaces). They are used to pass behavior as arguments.
public interface Operation {
    int calculate(int a, int b);
}

public class Test {
    public static void main(String[] args) {
        Operation add = (a, b) -> a + b;
        System.out.println(add.calculate(5, 3));  // Output: 8
    }
}

These are the main types of classes you’ll encounter in Java, each with its specific use case.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x