Java Classes and Their Uses

By ongraph
April 30, 2021 | 38 Views

Latest Industry insights to keep you updated on the latest happenings.

What is Class in Java?

Class is a design or a set of instructions to create a particular type of object. It is a primary concept of Object-Oriented Programming (OOP) which revolves around real-life entities. Class in Java decides how an object will behave and what the object will contain. 

In simpler words, Everything in Java is in the terms of classes and objects, along with its attributes and methods. For e.g.: in real life, a bicycle is an object. The bicycle has attributes, such as weight & color, and methods, such as drive & brake. Therefore, a class in OOP is a prototype that explains the variables & methods to the Java Objects. 

Types of Classes in Java:

  1. Static Class

In Java, static determines a keyword that is used with variables, classes, blocks & methods. When we put the static keyword before any of the above mentioned, it determines that the particular member belongs to a type itself. In other words, an instance of a static object is created and shared across all the instances of the class.

public class StaticClassExample  

{  

private static String str = “Javatpoint”;  

//nested class which is a Static class  

public static class StaticDemo  

{  

//non-static method of Static class  

public void show()   

{  

System.out.println(str);   

}  

}  

public static void main(String args[])  

{  

StaticClassExample.StaticDemo obj = new StaticClassExample.StaticDemo();  

obj.show();  

}  

}  

** In the above example we have str as static because we are using that variable from a static context.

  1. Final Class

The word final indicates something that cannot be changed. The final class in Java is declared by the final keyword. As we declare a class as final, the values in the program remain the same throughout. Purpose of the class to be declared as final is to make the class  immutable like the String class.

//base class declared as final  

final class A  

{  

void printmsg()   

{  

System.out.print(“Base class method is executed.”);  

}  

}  

//derived class  

//extending a final class which is not possible   

//it shows the error cannot inherit final class at compile time   

class B extends A  

{   

void printmsg()   

{  

System.out.print(“Derived class method is executed.”);  

}  

}  

//main class  

public class FinalClassExample  

{  

public static void main(String[] arg)   

{  

B obj = new B();  

obj.printmsg();  

}  

}  

  1. Abstract Class

An abstract class is declared with the keyword abstract. The abstract class might contain or not contain abstract methods. These classes are incomplete and to make it complete, an abstract class must be extended to concrete class. Once a subclass is declared as abstract then it becomes necessary to offer the implementation of abstract methods also. Hence, the subclass must also be stated as abstract.

//abstract class  

abstract class MathematicalOperations   

{   

int a=30, b=40;  

//abstract method  

public abstract void add();   

}  

public class Operation extends MathematicalOperations    

{   

//definition of abstract method       

public void add()   

{  

System.out.println(“Sum of a and b is: “a+b);  

}  

public static void main(String args[])   

{  

MathematicalOperations obj = new Operation();  

obj.add();  

}  

}  

  1. Concrete Class

The regular Java classes are known as concrete classes in which all the methods of an abstract class are implemented. Concrete class helps to create an object directly. You need to remember that concrete class and abstract class are not the same. A concrete class may extend its parent class and is used for specific requirements.

//Concrete Class  

public class ConcreteClassExample   

{   

//method of the concreted class  

static int product(int a, int b)   

{  

return a * b;  

}  

public static void main(String args[])   

{  

//method calling      

int p = product(6, 8);  

System.out.println(“Product of a and b is: ” + p);  

}  

}  

  1. Singleton Class

A class with an object at a time is known as a singleton class. Though if you want to create an instance a second time, the new instance points to the first instance. If we modify inside the class through any instance, the change affects the single instance also. 

If we need to create a singleton class, follow this:

  • Create a private constructor.
  • Create a static method that returns the object of the singleton class

public class Singleton  

{  

private String objectState;  

private static Singleton instance = null;  

private Singleton() throws Exception  

{  

this.objectState = “Javatpoint”;  

}  

public static Singleton getInstance()  

{  

if(instance==null)  

{  

try  

{  

instance=new Singleton();  

}  

catch(Exception e)  

{  

e.printStackTrace();  

}  

}  

return instance;  

}  

public String getObjectState()  

{  

return objectState;  

}  

public void setObjectState(String objectState)  

{  

this.objectState = objectState;  

}  

}  

  1. POJO Class

POJO is Plain Old Java Object, a type of Java class that contains private variables i.e. setter & getter is known as POJO. It helps to define Java objects essential in increasing the reusability and understandability of Java programs. 

Remember these important points:

POJO class has the following properties to remember: 

  1. Predefined classes such as Arrays, HttpServlet, etc cannot be extended.
  2. It does not contain pre-specified comments.
  3. It cannot process predefined interfaces.
  4. It is not needed to add any constructor.
  5. All instance variables should be private.
  6. The getter/ setter methods should be public.

class PojoDemo   

{  

//private variable      

private double price=89764.34;  

//getter method  

public double getPrice()   

{  

return price;  

}  

//setter method  

public void setPrice(int price)   

{  

this.price = price;  

}  

}  

//main class  

public class PojoClassExample  

{  

public static void main(String args[])  

{  

PojoDemo obj = new PojoDemo();  

System.out.println(“The price of an article is “+ obj.getPrice()+” Rs.”);  

}  

}  

  1. Inner Class

Java comes up with an interesting property, you can define a class within a class and this is known as nested classes. It helps to group the classes logically and to achieve encapsulation. Also, the outer class members can be assessed by the inner class.

Below is the syntax to declare a nested class:

class OuterClass  

{    

//code    

class NestedClass  

{    

//code    

}    

}  

Nested Class are of two types:-

  • Static Nested class
  • Non-static Nested Class also known as Inner Class.

Below is the Java program to understand the Inner Class:

public class InnerClassExample  

{  

public static void main(String[] args)   

{  

System.out.println(“This is outer class.”);  

}  

class InnerClass   

{  

public void printinner()   

{  

System.out.println(“This is inner class.”);  

}  

}  

}  

Importance of Java Inner Class

The advantages of inner classes. They are as follows:

  • The inner class can be used only by the outer class, it represents a special type of relationship that allows access to all members and methods of the outer class and also the private class.
  • Inner classes develop a more understandable, readable and maintainable code because they logically group classes and interfaces.
  • Easy to access, as the inner object is completely available inside an outer Code optimization that requires fewer code to write hence avoids having a separate class.

Basically, Inner classes are the best choice for the purpose of logically grouping the classes that are used in one place. It also increases encapsulation. 

Want to know more or have an idea to develop a java based web application? Let’s connect for a free consultation with one of our solution architects today.

RECOMMENDED

Monthly industry insights to keep you updated on latest happenings

Follow us on Twitter
Follow us on Facebook
Follow us on Linkedin