Class in Java
In Java, two main types of classes exist:
- Normal classes: Normal classes are the most common type of class in Java. They possess fields, methods, and constructors. They allow developers to define and encapsulate data and behavior within objects.
- Abstract classes: Abstract classes cannot be directly instantiated. Instead, they serve as a blueprint for other classes. Abstract classes can contain abstract methods, which are declared but not implemented within the abstract class itself. Subclasses that inherit from an abstract class must provide implementations for these abstract methods.
Interface in Java
Similarly, two main types of interfaces are present in Java:
- Normal interfaces: Normal interfaces are a collection of abstract methods. They define a contract that a class can implement, thereby inheriting the abstract methods defined within the interface. By implementing an interface, a class can define its own behavior and fulfill the requirements specified by the interface.
- Marker interfaces: Marker interfaces do not include any methods. They serve as a special type of interface that signifies a particular property or behavior of a class. By implementing a marker interface, a class indicates that it possesses the characteristic associated with that interface. Marker interfaces are primarily used for identification or tagging purposes.
By utilizing these class and interface types, developers can create flexible and reusable code structures in Java.