Java provides several types of classes to support different programming needs. Here’s a breakdown of the major types:
1. Concrete Class (Regular Class)
A concrete class is a class that has a full implementation and can be instantiated. It contains both variables and methods (instance and static).
Example:
class Car {
String model;
void drive() {
System.out.println(model + " is driving");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.model = "Tesla";
car.drive();
}
}
✅ Used for: General object creation and behavior implementation.
2. Abstract Class
An abstract class cannot be instantiated and may contain abstract methods (methods without a body). It serves as a blueprint for subclasses.
Example:
abstract class Animal {
abstract void makeSound(); // Abstract method (no implementation)
void sleep() { // Concrete method
System.out.println("Sleeping...");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Bark!");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.makeSound(); // Bark!
myDog.sleep(); // Sleeping...
}
}
✅ Used for: Defining common behavior across multiple subclasses.
3. Interface
An interface defines a contract for classes to implement.
- It only contains abstract methods (before Java 8) or default/static methods (since Java 8).
- All methods in an interface are public and abstract by default.
Example:
interface Vehicle {
void start(); // Abstract method
}
class Bike implements Vehicle {
@Override
public void start() {
System.out.println("Bike is starting...");
}
}
public class Main {
public static void main(String[] args) {
Vehicle myBike = new Bike();
myBike.start();
}
}
✅ Used for: Achieving multiple inheritance and defining contracts.
4. Final Class
A final class cannot be extended (inherited). It is used to prevent further modifications.
Example:
final class MathUtils {
static int square(int x) {
return x * x;
}
}
// class AdvancedMath extends MathUtils {} // ❌ Error: Cannot inherit from final class
public class Main {
public static void main(String[] args) {
System.out.println(MathUtils.square(5)); // Output: 25
}
}
✅ Used for: Security and ensuring a class’s implementation cannot be changed.
5. Static Class (Nested)
Java does not allow top-level static classes, but you can have static nested classes inside another class.
Example:
class OuterClass {
static class StaticNestedClass {
void show() {
System.out.println("Inside static nested class");
}
}
}
public class Main {
public static void main(String[] args) {
OuterClass.StaticNestedClass obj = new OuterClass.StaticNestedClass();
obj.show();
}
}
✅ Used for: Grouping related classes inside a class.
6. Inner Class (Non-Static Nested Class)
An inner class is defined inside another class and has access to its outer class’s members.
Example:
class Outer {
class Inner {
void show() {
System.out.println("Inside inner class");
}
}
}
public class Main {
public static void main(String[] args) {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.show();
}
}
✅ Used for: Encapsulation and logically grouping classes.
7. Anonymous Class
An anonymous class is a class without a name, usually used for short-lived implementations of interfaces or abstract classes.
Example:
interface Greeting {
void sayHello();
}
public class Main {
public static void main(String[] args) {
Greeting obj = new Greeting() { // Anonymous class
public void sayHello() {
System.out.println("Hello from an anonymous class!");
}
};
obj.sayHello();
}
}
✅ Used for: One-time implementations.
8. Enum Class
An enum class represents a fixed set of constants.
Example:
enum Day {
MONDAY, TUESDAY, WEDNESDAY
}
public class Main {
public static void main(String[] args) {
Day today = Day.MONDAY;
System.out.println(today); // Output: MONDAY
}
}
✅ Used for: Defining constant values.
Summary Table
Class Type | Can Be Instantiated? | Can Be Extended? | Purpose |
---|---|---|---|
Concrete Class | ✅ Yes | ✅ Yes | General object creation |
Abstract Class | ❌ No | ✅ Yes | Defining common behavior |
Interface | ❌ No | ✅ Yes (implements) | Defining contracts |
Final Class | ✅ Yes | ❌ No | Preventing inheritance |
Static Nested Class | ✅ Yes | ✅ Yes | Grouping inside a class |
Inner Class | ✅ Yes | ✅ Yes | Access to outer class members |
Anonymous Class | ✅ Yes | ❌ No | Short-lived implementations |
Enum Class | ✅ Yes | ❌ No | Fixed set of constants |