Types of Classes in Java
In Java, the class is a blueprint from which we can create an individual object. Java provides a keyword named class by which we can declare a class. Inside the class, we define class members and functions. It is not possible to create Java programs without class. We can also refer a class as a user-defined data type because an object-oriented paradigm allows us to model real-world objects. In this section, we will focus on the types of classes in Java.
Types of Classes
There are seven types of classes in Java:
In Java, static is a keyword that manage objects in the memory. The static object belongs to the class instead of the instance of the class.
We can make a class static if and only if it is a nested class. We can also say that static classes are known as nested classes. It means that a class that is declared as static within another class is known as a static class. Nested static class does not require reference to the outer class. The purpose of a static class is to provide the outline of its inherited class.
The properties of the static class are:
Let's understand the concept of static class through a program.
In the outer class of the above program, we have declared a variable str as static because we are accessing that variable from a static context. If we declare that variable as non-static, the compiler shows an error because a nested static class cannot access non-static members of the outer class.
The second thing to be noticed in the above program is that for creating the object of the nested class we need not to create an instance of the outer class. If the nested class is not a static class, we need to create an instance of the outer class.
The word final means that cannot be changed. The final class in Java can be declared using the final keyword. Once we declare a class as final, the values remain the same throughout the program. The purpose of the final class is to make the class immutable like the String class. It is only a way to make the class immutable. Remember that the final class cannot be extended. It also prevents the class from being sub-classed.
Let's understand the concept of the final class through a program.
/FinalClassExample.java:11: error: cannot inherit from final A class B extends A
An abstract class is a that is declared with the keyword abstract. The class may or may not contain abstract methods. We cannot create an instance of an abstract class but it can be a subclass. These classes are incomplete, so to complete the abstract class we should extend the abstract classes to a concrete class. When we declare a subclass as abstract then it is necessary to provide the implementation of abstract methods. Therefore, the subclass must also be declared abstract. We can achieve data hiding by using the abstract class. An example of an abstract class is AbstarctMap class that is a part of the Collections framework.
Let's understand the concept of abstract class through a program.
Sum of a and b is: 70
These are the regular Java classes. A derived class that provides the basic implementations for all of the methods that are not already implemented in the base class is known as a concrete class. In other words, it is regular Java classes in which all the methods of an abstract class are implemented. We can create an object of the concrete class directly. Remember that concrete class and abstract class are not the same. A concrete class may extend its parent class. It is used for specific requirements.
Let's understand the concept of the concrete class through a program.
Product of a and b is: 48
A class that has only an object at a time is known as a singleton class. Still, if we are trying to create an instance a second time, that newly created instance points to the first instance. If we made any alteration inside the class through any instance, the modification affects the variable of the single instance, also. It is usually used to control access while dealing with the database connection and socket programming. If we want to create a singleton class, do the following:
In Java, POJO stands for Plain Old Java Object. A Java class that contains only private variables, setter and getter is known as POJO class. It is used to define Java objects that increase the reusability and readability of a Java program. The class provides encapsulation. It is widely used in Java because it is easy to understand these classes. POJO class has the following properties:
Let's understand the concept of POJO class through a Java program.
The price of an article is 89764.34 Rs.
Java allows us to define a class within a class and such classes are known as nested classes. It is used to group the classes logically and to achieve encapsulation. The outer class members (including private) can be accessed by the inner class. The general syntax for declaring the nested class is as follows:
The nested classes are of two types:
1. Static Nested class: A class that is static and nested is called a static nested class. It interacts with the instance member of its outer class. We can create an object of the static nested class by using the following syntax:
2. Non-static Nested Class: Non-static nested classes are called inner classes.
The general syntax for declaring the static nested class and inner class is as follows:
Let's understand the concept of inner class through a Java program.
Types of Inner Classes
Java provides the two types of inner classes are as follows:
Local Inner Class
It is a type of inner class that is defined inside a block. Here block denotes a method body (a group of statements enclosed between a pair of braces). Due to defining inside a block it is also known as method local inner class. These are the non-static classes because they can access the instance members of the block. We can define the local inner classes in the body of a method. These classes must be instantiated in the block in which they are defined.
When we compile a Java program (a program that contains an inner class), the compiler generates the two class files namely Outer.class and Outer$1Inner.class. One for the outer class and the other for the inner class that contains a reference to the outer class.
Let's understand the concept of a local inner class through a Java program.
Divisor = 4 Remainder = 0 We are inside the inner class Quotient = 5
Anonymous Inner Class
It is a type of inner class that is the same as local classes but the only difference is that the class has not a class name and a single object is created of the class. It makes the code more concise. It is used if we want to use the local class once. We can create anonymous classes in the following two ways:
Looking at the above syntax, we see that it is the same as the invocation of constructor except that the class has a definition contained in the block.
Score is 321
Java also provides another type of Java class is known as wrapper class. Let's discuss it in detail.
In Java, the term wrapper class represents a collection of Java classes that objectify the primitive type of Java. It means that for each primitive type there is a corresponding wrapper class. The wrapper classes are used to perform the conversion from a primitive type to an object and vice-versa. The following figure demonstrates the wrapper class hierarchy.
The following table represents the primitive type and corresponding wrapper class.
Let's understand the wrapper class through a Java program.
Byte object byteobj: 0 Integer object intobj: 23 Character object charobj: m