Different Types of Classes in Java



In Java, a class is a datatype which defines properties (variables) and behaviors (methods) of an object. Defining an object does not consume memory; only its object or instance does.

Depending on the requirement, we will create various types of classes in Java. In this article, we are going to discuss them.

Types of classes in Java

The Java class is classified into different types based on its methods, as shown in the list given below:

  • Concrete class
  • Abstract class
  • Final class
  • POJO class
  • Static class
  • Inner Class
  • Wrapper Class
  • Singleton Class

Concrete class

Any normal class which does not have any abstract method or a class that has an implementation of all the methods of its parent class or interface and its own methods is a concrete class.

Example

Following Java program is an example of Concrete class.

// Concrete Class
public class Concrete { 
   static int product(int a, int b) {
      return a * b;
   }
   public static void main(String args[]) {
      int p = product(2, 3);
      System.out.println("Product: " + p);
   }
}

Output

Product: 6

Abstract class

A class declared with abstract keyword and have zero or more abstract methods is known as abstract class. They are incomplete classes, therefore, to use we strictly need to extend the abstract classes to a concrete class.

Example

Here is an example of Abstract class in Java:

//abstract parent class
abstract class Animal { 
   //abstract method
   public abstract void sound(); 
}
//Dog class extends Animal class
public class Dog extends Animal { 
   public void sound() {
      System.out.println("Woof");
   }
   public static void main(String args[]) {
      Animal a = new Dog();
      a.sound();
   }
}

Output

Woof

Final class

A class declared with the final keyword is a final class and it cannot be extended by another class, for example, java.lang.System class.

Example

Let's see a Java program of final class:

final class BaseClass {
   void Display() {
      System.out.print("This is Display() method of BaseClass.");
   }
}
//Compile-time error - can't inherit final class
class DerivedClass extends BaseClass { 
   void Display() {
      System.out.print("This is Display() method of DerivedClass.");
   }
}
public class FinalClassDemo {
   public static void main(String[] arg) {
      DerivedClass d = new DerivedClass();
      d.Display();
   }
}

In the above example, DerivedClass extends BaseClass(final). We can't extend a final class, so compiler will throw an error.

cannot inherit from final BaseClass
Compile-time error - can't inherit final class

POJO class

A class that contains only private variables and setter and getter methods to use those variables is called POJO class. Its full form is Plain Old Java Object. It is a fully encapsulated class.

Example

An example of POJO class in Java is given below:

class POJO {
  private int value=100;
  public int getValue() {
      return value;
   }
   public void setValue(int value) {
      this.value = value;
   }
}
public class Test {
   public static void main(String args[]){
      POJO p = new POJO();
      System.out.println(p.getValue());
   }
}

Output

100

Static class

Static classes are nested classes means a class declared within another class as a static member. They are defined using the static keyword.

Example

Following is an Java program that demonstrates static class.

class staticclasses {
   // Static variable
   static int s;
   // Static method
   static void met(int a, int b) { 
      System.out.println("Static method to calculate sum");
      s = a + b;
      // Print two numbers
      System.out.println(a + " + " + b);
   }
   // Static class
   static class MyNestedClass { 
      // Static block
      static { 
         System.out.println("Static block inside a static class");
      }
      public void disp() {
         int c = 10;
         int d = 20;
         // Calling static method
         met(c, d); 
         // Print the result in the static variable
         System.out.println("Sum of two numbers: " + s);
      }
   }
}
public class Test {
   public static void main(String args[]) {
      // Object for static class
      staticclasses.MyNestedClass mnc = new staticclasses.MyNestedClass(); 
      // Accessing methods inside the static class
      mnc.disp(); 
   }
}

Output

Static block inside a static class
Static method to calculate sum
10 + 20
Sum of two numbers: 30

Inner Class

A class declared within another class or method is called an inner class.

Example

In this example, we will write a Java program that illustrates inner class.

public class OuterClass {
   public static void main(String[] args) {
      System.out.println("Outer");
   }
   class InnerClass {
      public void inner_print() {
         System.out.println("Inner");
      }
   }
}

Output

Outer

Wrapper Class

A wrapper class helps us to use primitive data types as objects. It is a part of the java.lang package.

Example

The following example demonstrates the use of a wrapper class in Java:

 
// Using Wrapper Class 
public class Example { 
   public static void main(String[] args) { 
      int num = 50; 
      // Converting int into Integer 
      Integer obj = num; 
      System.out.println("Wrapper object: " + obj); 
   } 
} 

Output

 Wrapper object: 50 

Singleton Class

A singleton class is a class that allows only one instance of itself to be created and provides a global point of access to that instance. It is used when exactly one object is needed to coordinate actions across the system.

Example

The following Java program is an example of a singleton class:

 
// Singleton Class Example 
class Singleton { 
   // Private static variable 
   private static Singleton single_instance = null; 
   public String str;
   // Private constructor
   private Singleton() { 
      str = "Welcome to Tutorialspoint"; 
   }
   // Static method to create instance of Singleton class 
   public static Singleton getInstance() { 
      if (single_instance == null) 
      single_instance = new Singleton(); 
      return single_instance; 
   }
}
public class SingletonExample { 
   public static void main(String args[]) { 
      // Instantiating Singleton class with variable x 
      Singleton obj = Singleton.getInstance(); 
      System.out.println(obj.str); 
   } 
} 

Output

 
Welcome to Tutorialspoint 
Updated on: 2025-05-21T16:09:47+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements