Skip to main content

Command Palette

Search for a command to run...

Mastering Classes in Core Java: Complete Interview Guide for Freshers

Published
5 min read
N
Java & Spring Boot learner | Writing beginner-friendly technical articles | Exploring backend development

When we start learning Java, the very first concept we encounter is : Class and Object . And as we grow from writing small programs to building real applications, we realize one truth:

Everything in Java revolves around classes. That is why interviewers(especially for fresher roles) repeatedly ask:

  • What is a class?

  • What are the types of classes in Java?

  • What is the difference between abstract class and concrete class?

  • Why do we use nested classes?

  • What is POJO and why is it important in Spring Boot?

  • Why enum is preferred over constants?

  • Explain Singleton pattern in Java

So in this blog, we will explore all important types of classes in Java :

1. What is a Class in Java?

A class in Java is a blueprint or template used to create objects.

A class defines:

  • State → variables/fields

  • Behavior → methods

  • Initialization → constructors

Example of a Class

class Car{
   //state (data)
    String brand;
    int speed;
   // behavior (functionality)
    void drive(){
        System.out.println(brand + "is driving at " + speed + "km/h");
    }
}

Creating an Object

Car carObj = new Car();
carObj.brand = "KIA";
carObj.speed = 120;
carObj.drive();

Output:

KIA is driving at 120 km/h

  • A class is a logical entity, while an object is a real-world instance of a class.

2. Types of Classes in Java

Java provides different types of classes depending on design requirements.

The major ones are:

  1. Concrete class

  2. Abstract class

  3. Nested class

  4. Generic class

  5. POJO class

  6. Enum class

  7. Singleton class

  8. Final class

2.1 Concrete Class:

A concrete class is a normal class where:

  • All methods are fully implemented

  • Objects can be created directly

  • It contains complete business logic

Example:

class Employee{
    void work(){
      System.out.println("Employee is working...");
   }
}

// Object creation
Employee emp = new Employee();
emp.work();

Real-World Use:

In real applications:

  • Service classes

  • Controller classes

  • Utility classes

are mostly concrete.

2.2 Abstract Class

An abstract class represents an incomplete idea.

It is used when:

  • We want a base class

  • We want to enforce rules for subclasses

  • We want partial abstraction

Example:

abstract class Vehicle{
  // Abstract method (no body)
  abstract void run();
  // concrete method (with body)
  void fuel(){
     System.out.println("Vehicle needs fuel to run");
   }
}

Sub class:

class Bike extends Vehicle{
   void run(){
     System.out.println("Bike is running");
  }
}

// Object creation

Vehicle v = new Bike();
v.run();
v.fuel();

Why Abstract Classes are used?

Because they provide:

  • Code reusability

  • Abstraction

  • Common structure

Abstract class can have constructors, and called during child object creation. And it can have non-abstract methods and we cannot create object of abstract class.

3. Nested Classes in Java

Java allows defining a class inside another class. These are called Nested classes

Why Nested Classes?

  • Encapsulation

  • Code Organization

  • Logical grouping

Example:

A Car class may contain an inner Engine class.

Types of Nested Classes

  1. Static Nested Class

  2. Inner Class (Non-static)

  3. Local Inner Class

  4. Anonymous Inner Class

3.1 Static Nested Class

A static nested class belongs to the outer class, not its object.

Example:

class Outer{
  static int value = 100;
   static class Inner{
       void display(){
           System.out.println("Value: " + value);
     }
  }
}

// Object creation
Outer.Inner obj = new Outer.Inner();
obj.display();
  • Static nested class can access only static members of outer class and there is no need for outer class object.

3.2 Non-Static Inner Class

Inner class is associated with an outer object.

Example:

class Outer{
  int number = 50;
  // Inner class
    class Inner{
       void show(){
           System.out.println("Number: " + number);
        }
    }
}

// Object creation
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.show();
  • Inner class can access both static and non-static members.

3.3 Local Inner Class

Defined inside a method. Scope is limited only to that method.

Example:

class Demo{
    void display(){
      class LocalInner{
           void msg(){
               System.out.println("Inside Local Inner Class");
         }
    }
      LocalInner obj = new LocalInner();
      obj.msg();
   }
}
  • It is used for method-level encapsulation

3.4 Anonymous Inner Class

Anonymous class has no name. used when we want one-time implementation.

Example:

Runnable r = new Runnable(){
     public void run(){
            System.out.println("Thread running....");
     }
};

new Thread(r).start();

Real-World use:

  • Event handling

  • Call backs

  • Multithreading

4. Generic Classes (Very Important)

Generics provide:

  • Type safety

  • Code reusability

  • No typecasting

Without Generics:

Object obj = "Hellp";
String s = (String) obj;

Risk: cause ClassCastException

With Generics:

class Box<T>{
   T value;
    Box(T value){
       this.value = value;
    }
    T getValue(){
       return value;
    }
}

// Object creation
Box<Integer> b = new Box<>(10);
System.out.println(b.getValue());

What is Type Erasure?

Generics are removed at runtime, only compile-time feature.

5. POJO Class (Plain Old Java Object)

It is most used in Spring Boot

A POJO Contains:

  • private fields

  • Getters/Setters

  • Default constructor

Example:

public class Student{
   private int id;
   private String name;
    // default constructor
   public Student(){}
   public int getId(){
      return id;
   }
   public void setId(int id){
     this.id=id;
   }
}

Real-World Use:

Spring Boot maps JSON request → POJO object automatically.

6. Enum Class

enum Day{
  MONDAY, TUESDAY, FRIDAY;
}

Day d = Day.MONDAY;

Why Enum is better than Constants?

  • Type-safe

  • Better readability

  • Supports constructors and methods

7. Singleton Class

Singleton ensures only one object exists. Used in :

  • DB Connection

  • Logging

  • Configuration

Eager Singleton:

class Singleton {
    private static final Singleton instance = new Singleton();
    private Singleton();
    public static Singleton getInstance(){
          return instance;
     }
}

Best Singleton Example: Enum

enum SingletonEnum{
  INSTANCE;
}

8. Final Class

A final class cannot be inherited.

final class SecurityManager{
}

Example: String class is final.

Conclusion

Classes are the backbone of Java and the foundation of Object-Oriented Programming. A deep understanding of different types of classes, abstract classes, nested classes, POJO, enum, generics, singleton, and final classes - is not only essential for cracking Core Java interviews but also for writing clean, scalable, and maintainable real-world applications.

Mastering these concepts helps developers design better software architectures, improve code reusability, enforce proper abstraction, and follow industry-standard best practices.

More from this blog

CoreJava

19 posts

I have written and published a comprehensive blog series titled "CoreJava" on Hashnode, based on my learning journey from basics to advanced. The series includes topics like OOP, Collections, Exception Handling, Multithreading, and Java Streams, explained with clear examples and practical insights to help learners build a strong foundation in java.