Get Upto 50% Offer | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

  • Home
  • Blog
  • Explain different types of ...
Post By Admin Last Updated At 2020-12-21
Explain different types of Java Classes

A class in Java programming is an important part of coding. There are different types of Java classes which is an object-oriented programming language. A class in Java is an object builder or a template. The various types of Java classes have a different purpose to serve for an application developer or user.

In this article, we are going to discuss various types of Java Classes and objects in this programming language and their usage.

Introduction to Java Classes

A class within Java language is a kind of template useful in building and designing objects, methods, and various object data types. It is also defined as a collection of similar objects within the coding language. Java Classes are known as categories and the objects are the instances or items within them having state and behavior.

Types of classes in Java

Java Classes include the following important components;

Modifiers – The modifiers mention the default access for the class.

ClassName – This denotes the name of the class.

KeyWords – The “KeyWords” refers to suggesting keywords in case the class extends.

The Class Body exists with the curly braces {}.

Here is the overview of different types of Java Classes.

Concrete class: This is a normal class that includes the concrete or strong implementation of all the methods.

POJO class: POJO refers to -“Plain Old Java Object” includes only private member variables and it uses many getter-setter methods to access these variables.

Abstract class: This class is declared with an abstract method having one or more abstract methods.

Final class: This is a class where value remains the same throughout and cannot be inherited.

Static class: A static class is the Java Class that includes only static members.

Inner class: This is a class that is a member of another class.

Main Class: A main () class in Java is a method that is the beginning point of the program execution. Moreover, it can be placed in any class of the specific application. Besides, if there are many files and classes within a complex application, then we can use the Main class and place it in the Main method.

class Main{

     public static void main(String[] args){

         System.out.println(“Hello, World!”);

     }  

}

 We will see in detail the different types of Java Classes in this article further.

Get more insights on Java Classes in a practical way through Java Online Course through the expert’s voice at OnlineITGuru.

Types of Java Classes

Let’s move further in this post regarding - various types of Java Classes. Here we will discuss the various types of Java Classes in detail along with examples.

Concrete Class

A concrete class within ‘Java Classes” is like any normal class within a Java program. This class doesn’t have any abstract methods within it. Hence, all the methods in the concrete class are fully applied. Moreover, a concrete class can receive from another class, even an abstract class or implement an interface. Further, we can represent this Java class and build its objects.

The example to define the Concrete Class is as follows:-

public class Main { // Example of Concrete Class

   static int total(int valx, int valy) {

   return valx + valy;

   }

   public static void main(String args[]) {

   int sum = total(10, 20);

      System.out.println("Total of two integers: " + sum);

   }

}

Output

Two integers total – 30

Abstract Class

The abstract class in Java classes is that which is incomplete or whose application is not completed. Besides, an abstract class cannot be represented, and therefore, it needs an extension by the other Java Classes. Moreover, it implements its various methods to form a concrete class.

Furthermore, this Java class is declared by using the keyword ‘abstract’ that also may include static and final methods as well as constructors.

Example

The following example explains the use of Abstract class within the Java Classes.

interface X

{

int product(int x, int y);

}

abstract class Product implements X

{

// the method calc. product of 2 numbers

public int product(int x, int y)

{

return x * y;

}

}

// the concrete class implements

Class Main method to extend Product

{

// main method

public static void main(String args[])

{

Main ob = new Main();

int p = ob.product(40, 10);

// now print product

System.out.println(“Product: “ + p);

}

}

Output

Product 400

||{"title":"Master in Java", "subTitle":"Java Certification Training by ITGURU's", "btnTitle":"View Details","url":"https://onlineitguru.com/core-java-online-training-placement.html","boxType":"demo","videoId":"Vi7pdpk5Vq0"}||

POJO Class

The Plain Old Java Object-POJO class includes only private members and uses Getter & Setter methods that members use. It includes a pure data structure where the fields within this class may override some objects or equals. Moreover, this class doesn’t have its own behavior. Some of the properties that a POJO class includes are as follows.

The use of public getter and setter methods is important while writing a POJO class.

It should not include pre-defined annotations.

This class should not extend the pre-mentioned classes.

Moreover, all the variables or instances within it should be private only.

Furthermore, it doesn’t include a no-argument constructor.

Example: The following example coding defines the POJO class in practical.

class POJO {

  private int value=250;

  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

The result value will be – 250

Final Class

The class within Java Classes that is declared with a “final-keyword” is a Final class. This class cannot be extended to any other class. Therefore, it cannot be inherited or sub-classified. Once a class is stated final in Java then it becomes an immutable one. To make it an immutable one, we must declare it as a final in terms of class or keyword.

Example: The example of the Final class is declared in the following code.

final class BaseClass {

   void Display() {

   System.out.print("Displaymethod relates to BaseClass");

   }

}

class DerivedClass extends BaseClass { //Compile-time error - cannot inherit final class

   void Display() {

   System.out.print("DerivedClass display method.");

   }

}

public class FinalClassDemo {

   public static void main(String[] arg) {

   DerivedClass d = new DerivedClass();

   d.Display();

   }

}

The example states that a DerivedClass extends BaseClass(final) and we cannot extend a final class anymore. Therefore, the compiler will throw an error in the result. Finally, the above program doesn't execute.

The Output will be as follows

1)      Can’t inherit from final BaseClass

2)      Compile-time error - can't inherit the final class

Static Class

A static class within various Java Classes defines that an object that relates to a class and not to any instance. It is declared as a static class within another class as a member. Hence, this class cannot be accessed through an object but can access directly through a class name.

Example: The following coding will show how a static class will be applicable.

import java.util.Scanner;

class staticclasses {

   static int s; // static variable

   static void met(int x, int y) { // method static

   System.out.println("static method to calculate sum");

   s = x + y;

   System.out.println(x + "+" + y); // printing two numbers

}

   static class My NestedClass { //class - static

   static { // static block

      System.out.println("static block within a static class");

   }

   public void disp() {

      int b, d;

      Scanner sc = new Scanner (System.in);

      System.out.println("Write down two values");

      b = sc.nextInt();

      d = sc.nextInt();

      met(b, d); // call the static method

      System.out.println("Addition of two numbers-" + s); // printing results in a static variable

   }

   }

}

public class Test {

   public static void main(String args[]) {

   staticclasses.MyNestedClass mnc = new staticclasses.MyNestedClass(); // static class object

   mnc.disp(); 

   }

}

Output- The result for the above coding will be as follows.

Static method to calculate addition of two numbers: 15 + 15

Addition of two distinct numbers: 30 

Nested or Inner Class

The nested or inner class within this coding language is useful to increase the encapsulation. This type of class is that exists inside another class.

As any class includes variables and methods as its members, the Nested/Inner class also may have its own members.

Example: We can define a Nested class using the following example code.

class OuterClass{

         class NestedClass{

             //nested class code…

         }

         //outerclass code…

     }  

Furthermore, there are some subtypes of the Inner or Nested Java class. They are;-

  •         Nested Inner class
  •         Method Local Inner class
  •         Anonymous Inner class
  •         Static Nested class

Nested Inner Class

This Java class includes the accessibility to private member variables of the outer class. Moreover, users can also implement access modifiers to this inner nested class of Java.

Method Local Inner Class

This type of inner class method is defined as an outer class method within the inner class.

Anonymous Inner class

The anonymous inner class is stated as an inside outer class within this class but it doesn’t contain any name. These types of classes can be built in two different ways.

Static Nested class

This class also has a static class as its member similar to a class having a static member variable within it.

Example:

public class Main {

   class InnerClass {

   public void inner_print() {

      System.out.println("Inner class");

   }

   }

   public static void main(String[] args) {

   System.out.println("Main in Outer class");

}

}

The output of the above code will be as follows for the inner class.

"Main in Outer class"

Thus, the above are the major types of Java Classes. There are a few other types of special classes also within this programming language. Let us discuss them.

||{"title":"Master in Java", "subTitle":"Java Certification Training by ITGURU's", "btnTitle":"View Details","url":"https://onlineitguru.com/core-java-online-training-placement.html","boxType":"reg"}||

Immutable Class: (Java Classes)

Within the “Java Classes”, the immutable class produces various unchangeable objects. Moreover, this is an object whose contents cannot be modified once the object is built. So, this is an unchangeable Java class without any object change.  

All the covering Java Classes for antique types (For example-Integer, Boolean, Byte, etc.) are immutable or unchangeable. The String class is also an unchangeable class. Furthermore, we can also have some user-defined immutable classes in the Java program.

To become a class immutable, it needs to be stated final along with all its member data. Also, this class should have a constructor based on some parameters stated. Further, the member variables should use only a getter method but not the setter method.

For an immutable or fixed class in this coding language, the above requirements must be fulfilled.

Singleton Class (Java Classes)

A “singleton class” within the Java classes allows a single instance at a time. In this language, a singleton class is a design pattern that ensures only one instance class exists at any point in time. Hence, any class stated as a singleton class includes a single access point globally.

Here, we have to note that similar to a normal class, the termination of the application life cycle (ALC) doesn’t harm a singleton class. When a class is stated as a singleton, then we don’t need to build a new instance every time. Whenever there is a new request made for the object, we don’t build any instance.

Moreover, here in this case the same instance is reusable. Hence, in this way, we can save the large memory space mostly in the case of multi-thread apps and database apps.

Furthermore, we can use this type of Java Class for various activities like caching, logging, etc.

Other than the above there are object class and wrapper class also within this coding language. Let us have a brief on Object class in Java.

Object Class

This class is known as the parent class or the first class within the Java program language. The object class exists at the top of the Java hierarchy.  Similar to all other classes having the Object class as a superclass, all Java objects include Arrays that implement the Object class methods. Moreover, the object class exists within the “java.lang” package. There are Object class methods and constructors also available. The methods include Boolean equals (), protected object class (), int hashcode(), void notify(), void wait(), etc.

Wrapper Class

The wrapper class within Java Classes includes a mechanism that changes historical or ancient data types to objects and vice-versa. Such as int, char, etc. Thus, for every historical data type, there is an equivalent Java wrapper class.

Summing up

Thus, we can end up the discussion here on different types of “Java Classes”. I hope you got an idea of various Java Classes with examples. If you feel that this post is relevant for you, then you can step further to learn more in this regard. So, get trained with experts in a live and practical way from the Java Online Training program through the online platform. This program may help you to get professional knowledge in real-time and with proper guidance. Learn more about various Java frameworks, classes, etc., and their usage in application development.