Sobes.tech
Junior

What types of classes exist in Java?

sobes.tech AI

Answer from AI

In Java, the following main types of classes exist:

  1. 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).
  2. 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.");
        }
    }
    
  3. 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;
        }
    }
    
  4. Enums

    • A special type of class designed to represent a fixed set of constants.
    • Declared with the keyword enum.
    • Enum constants are public static final by 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
    }
    
  5. 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.");
        }
    };
    
  6. Nested Class

    • Classes declared within another class.
    • Two types:
      • Static Nested Class: declared with the static keyword. 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, including private. Requires an instance of the outer class to create an instance.
    // 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
            }
        }
    }
    
  7. Record Classes (Java 14 and later)

    • Declared with the record keyword.
    • Designed for data storage. The compiler automatically generates constructor, accessor methods, equals(), hashCode(), and toString().
    • 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()
    }
    
What types of classes exist in Java? — Java - sobes.tech