Junior
What types of classes exist in Java?
sobes.tech AI
Answer from AI
In Java, the following main types of classes exist:
-
Standard Class
- Can contain fields, methods, constructors.
- Can be inherited by other classes unless declared as
final. - Can implement interfaces.
- Declared with access modifiers (
public,protected,default,private).
-
Abstract Class
- Declared with the keyword
abstract. - Cannot be instantiated directly.
- Can contain abstract methods (without implementation) and regular methods.
- Intended for inheritance; subclasses must provide implementations for abstract methods.
// Abstract class for a shape abstract class Shape { abstract double area(); // Abstract method for calculating area void display() { // Regular method System.out.println("This is a shape."); } } - Declared with the keyword
-
Final Class
- Declared with the keyword
final. - Cannot be inherited by other classes.
- Used to prevent extension of the class, ensure security or immutability of implementation.
// Final class that cannot be inherited final class ImmutableString { private final String value; public ImmutableString(String value) { this.value = value; } public String getValue() { return value; } } - Declared with the keyword
-
Enums
- A special type of class designed to represent a fixed set of constants.
- Declared with the keyword
enum. - Enum constants are
public static finalby default. - Can have fields, methods, and constructors (with a private access modifier).
// Enum for days of the week enum DayOfWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } -
Anonymous Class
- A class without a name, created and instantiated simultaneously.
- Often used to implement interfaces or extend classes on the fly for one-time use.
- Cannot have explicit constructors.
// Creating an anonymous class implementing Runnable Runnable r = new Runnable() { @Override public void run() { System.out.println("Running in an anonymous class."); } }; -
Nested Class
- Classes declared within another class.
- Two types:
- Static Nested Class: declared with the
statickeyword. Does not have direct access to non-static members of the outer class. Instantiated like regular classes. - Inner Class: not declared as
static. Has access to all members of the outer class, includingprivate. Requires an instance of the outer class to create an instance.
- Static Nested Class: declared with the
// Outer class class OuterClass { private String outerField = "Outer"; // Static nested class static class StaticNestedClass { void display() { System.out.println("Inside StaticNestedClass."); // No direct access to outerField } } // Inner class class InnerClass { void display() { System.out.println("Inside InnerClass. outerField: " + outerField); // Access to outerField } } } -
Record Classes (Java 14 and later)
- Declared with the
recordkeyword. - Designed for data storage. The compiler automatically generates constructor, accessor methods,
equals(),hashCode(), andtoString(). - Record fields are implicitly
final.
// Record for representing a point in 2D space record Point(int x, int y) { // The compiler will generate constructor, x(), y(), equals(), hashCode(), toString() } - Declared with the