Types of Classes in Java with Examples

Types of Classes in Java | We have already known that classes are considered as a blueprint for creating objects. They are very important to construct object-oriented programming.  These classes encapsulate State and Behavior.

Behavior is encapsulated into methods (also known as functions) and state (properties) is encapsulated in data called member variables.

A class is a functional unit of Java program that provides not only methods to perform some tasks but also data fields on which methods operate.

In this tutorial, we will familiarize different types of classes in Java.

Types of classes in Java

1. Concrete Class:

A class whose object can be created and whose all methods have body is called concrete class. Java object class is a concrete class. A concrete class can have both static and non-static members.

It can extend its superclass, an abstract class, or implement an interface if it implements all their methods. It has no abstract methods.

Let’s take a program where we will create a concrete class and will instantiate it.

Program source code 1:

public class Concrete { // Concrete class
int a;
int b;
 a = 50;
 b = 50;
void display()
 int sum = a + b;
 System.out.println("Sum of two numbers: " +sum );
public static void main(String[] args) {
Concrete c = new Concrete();
      Sum of two numbers: 100

2. Static Class:

In Java, static is a keyword that can be applied with variables, methods, inner classes, and blocks. We cannot declare a class with static keyword but inner class can be declared as static.

When an inner class is defined with a static modifier inside the body of another class, it is known as static nested class in Java. Let’s take an example program based on it.

Program source code 2:

public class A 
// Static nested class starts here.
 static class B // Inner class with the static modifier
  public void m1()
	System.out.println("Static nested class method");
 } // Static nested class ends here.
public static void main(String[] args) 
// Since class B is static nested class, not a regular inner class, Therefore, outer class object is not required.
// You can directly create an object of static nested class like this.	
    B b = new B();
      Static nested class method

3. Abstract Class:

An abstract class is a class which is declared with abstract keyword. It is just like a normal class but has two differences.

1. We cannot create an object of this class. Only objects of its non-abstract (or concrete) sub-classes can be created.

2. It can have zero or more abstract methods that are not allowed in a non-abstract class (concrete class).

An abstract class is sometimes also known as base class or superclass. It contains undefined and unimplemented abstract method bodies that are implemented and defined by derived classes.

Program source code 3:

public abstract class Hello 
// Declaration of instance method. 
 public void msg1() 
abstract public void msg2(); 
public class Test extends Hello 
// Overriding abstract method. 
  public void msg2() 
public static void main(String[] args)
// Creating object of subclass Test. 
  Test obj = new Test(); 

4. Final Class:

When a class is declared with final keyword, it is called final class in Java. Final class means Restricting Inheritance!. It does not allow itself to be inherited by another class.

In other words, Java classes declared as a final cannot be extended (inherited). If you do not want to be a subclass, declare it final. A lot of classes in Java API are final.

For example, String class is the most common predefined final class object in Java.

Program source code 4:

final class Hello 
// Declaration of instance method. 
 public void msg() 
public class DerivedClass extends Hello 
public void msg() 
public class Test {
public static void main(String[] args) 
 DerivedClass d = new DerivedClass();
      Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The type DerivedClass cannot subclass the final class Hello

	at anonymousObject.DerivedClass.<init>(DerivedClass.java:3)
	at anonymousObject.Test.main(Test.java:6)

5. Inner class:

A class declared inside another class is known as nested class. The class which is a member of another class can be either static or non-static.

When a member class is declared with static, it is known as static nested class. The member class which is non-static is known as inner class in java.

An inner class cannot have any kind of static member. So, the members of an inner class can be:

  • Instance variables
  • Instance methods
  • Constructors
  • Initializer block
  • Inner class

Based on declaration and behaviors, there are basically four types of inner classes in Java. They are as follows:

  1. Normal or Regular inner class
  2. Method local inner class
  3. Anonymous inner class
  4. Static nested class

6. Public Class:

When a class is declared with a public access control modifier, it is called public class. A public class is visible and accessible from anywhere. The instance of public class can be created from any other class.

When a class is declared as public, any program in any package can use the code or some of the code. But we do not put public with class, it is non-public (default) class.

Let’s take an example where we have declared a public class named Book.

package bookLibrary;
public class Book {
String isbn;
String title;
int noofPages;
int width;
int height; }

The class Book is a member of bookLibrary package and has five fields (variables). Since the class Book is declared as public, therefore, it can be instantiated from any other class.

In Java core libraries, the majority of classes are public classes. For example, the declaration of java.lang.Runtime class in Java is as follows:

public class Runtime

The extension of public class is java. and it must be saved in a file with the same name as class name. The class Book must be saved in a Book.java file inside an bookLibrary directory.

A java source file can contain only one public class but it can contain multiple classes that are not public.

7. Private class:

An outer class (top-level class) cannot be declared with a private access modifier but an inner class can be declared as private. An inner class is a member of outer class.

By mistake, if you will try to use any other modifiers with top-level class, you will get a compile-time error: “Modifier private not allowed here“.

8. Singleton Class:

A singleton class in Java is a class that allows only one instance to be created. All its variables and methods will belong to just one instance. Singleton class concept is useful when we need to create only one object of class.

A good example of a singleton is a data class that contains all the data to be used across the entire application. There is no need to create more than one object.

The main disadvantage of a singleton class is that it cannot be reusable. This is because we can create only one object of class.

All the above classes are created by programmers in a Java program as per requirements. Now let’s see a brief overview of predefined classes library in Java.

Predefined Classes in Java

The library of predefined class in Java is called Java class library or application programming interface (API). In Java, classes are organized into groups called packages.

Each package has a specific name such as java.lang, java.util package.  These two packages each contain several classes. So, let’s see one by one.

1. Object Class:

Object class is the root of Java class hierarchy. It is the superclass of all other classes. It means that every class inherits methods that are defined in the Object class. Object provides wait(), notify(), and notifyAll() methods for multithreading.

2. Math Class:

Math class is present in java.lang package. All the methods present in this class are static. So, we do not need to create an object of class to use methods. Math class’s methods are used for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.

For example:

double root;
root = Math.sqrt(10); // It will find square root of 10.

3. String Class:

String class is present in java.lang package. It represents character strings. It is a powerful concept that provides a lot of methods to work on string. With the help of these methods, we can perform operations on string such as concatenating, converting, comparing, replacing strings, trimming, etc.

Since strings are constant, therefore, their values cannot be changed once they are created. For example: String str = “abc”;.

4. System Class:

System class is present in java.lang package. All the methods and variables of system class are class methods and variables. They are defined with a static keyword that makes unique them.

We can use directly system class methods and variables in a program. We access them using class name instead of creating an object of class.

5. Random Class:

Random class is present in java.util package. It is used to generate a list of random numbers. Random numbers are used in creating simulations or models of real-world situations, with programs.

6. Scanner Class:

Scanner class is present in java.util package. It is used to read input from a keyboard or text file. When scanner class receives input from the keyboard then it breaks input into several parts which are called tokens. These tokens are retrieved from from the scanner object using methods such as next(), nextByte(), nextInt(), etc.

7. Wrapper Class:

A class whose object warps or contains primitive data type is called wrapper class. It is present in java.lang package. It is used to convert primitive data type into object form. When we will create an object to a wrapper class, it contains a field where we can store a primitive data type.

That is, we can wrap a primitive value into a wrapper class object. The list of wrapper classes defined in java.lang package is Character, Byte, Short, Integer, Long, Float, Double, and Boolean.

These are the few important types of classes defined in java.lang and java.util packages. There are several classes available in several java packages that are not possible to remember or keep in mind. We have explained above a brief introduction of some important predefined classes in Java.

Hope that this tutorial has covered the basic points of different types of classes in Java with example programs. I hope that you will have understood this topic.

Thanks for reading!!!
Next ⇒ Data types in Java⇐ PrevNext ⇒