Skip to main content

Classes and Object in Java



Class - This is a basic and important concept in Java. Classes are the blueprint of variables and methods to describe its behavior and it helps the compiler in creating the object.

Syntax for creating a class

class ClassName{
}

Objects - Objects are the reference for the variables and methods that are in the class. 

Syntax for creating an object

ClassName referenceName = new ClassName();

Types of classes in Java

  • Concrete class
  • Abstract class
  • Inner class
  • Static class
  • Final class
  • POJO class

1. Concrete class

    This type of class does not contain any abstract method and these classes will contain its own method implementation. 

Sample Program
--------------------------------------------------------------------------------------------------------------------
    public class ClassAndObjects {
        int varr = 6;

        void display() {
            System.out.println("Method called...");
        }

        public static void main(String[] args) {
            ClassAndObjects ad = new ClassAndObjects();

            ad.display();
            System.out.println("Variable display "+ad.varr);
        }
    }

--------------------------------------------------------------------------------------------------------------------

Output:
Method called...
Variable display 6

In the above sample program, There is a class "ClassAndObjects" created with variable varr and one method display(). Inside the main method, an object reference was created for this class to access the variable and methods of the class

2. Abstract Class

    In this type, the abstract keyword must be used before the class name and it may or may not contain any abstract methods. abstract methods will have only a declaration and will not have an implementation. The class inherits this abstract class should implement these abstract methods.

Sample Program
--------------------------------------------------------------------------------------------------------------------
abstract class ClassAndObjects {
abstract void display();
abstract void multiTask();
}
--------------------------------------------------------------------------------------------------------------------

3. Inner Class

    Classes are nested in other classes. In this type to access the inner class variables and methods, the inner class object needs to be created by referring to the object reference created for the outer class.

Sample Program
--------------------------------------------------------------------------------------------------------------------
public class ClassAndObjects {
void display() {
System.out.println("This is an outer class");
}
public class innerClass{
void display() {
System.out.println("This is an inner class");
}
}
public static void main(String[] args) {
ClassAndObjects a1 = new ClassAndObjects();
innerClass a2 = a1.new innerClass();
a1.display();
a2.display();
}
}
--------------------------------------------------------------------------------------------------------------------

Output:
This is an outer class
This is an inner class

In the above example, we had 2 classes that are nested. To access the inner class variable and method, the First object should be created for the outer class "ClassAndObjects". Then with the reference of created object reference "a1",  need to create new objects for the inner class.

innerClass a2 = a1.new innerClass();

With reference to this object, we can able to access the variables and methods of the inner class. Or if we need to access only the inner class variable or method we can refer to the outer class while creating objects for the inner class.

ClassAndObjects.innerClass a2 = new ClassAndObjects.innerClass();

4. Static Class

    It is similar to the inner class and here the nested class will be static. To make the class static, add static keyword before the class name. If there is any static block inside the class it will execute at first in the execution. There is no need to call the static block, it will automatically execute at first. 

Sample Program
--------------------------------------------------------------------------------------------------------------------
public class ClassAndObjects {
static String displayText = "This is static variable";

static class innerClass{
static {
System.out.println(displayText);
}
void display() {
System.out.println("This is a method implemented inside the static class");
}
}
public static void main(String[] args) {
ClassAndObjects.innerClass a2 = new ClassAndObjects.innerClass();
a2.display();
}
}
--------------------------------------------------------------------------------------------------------------------

Output:
This is static variable
This is a method implemented inside the static class

5. Final Class

    This type of class cannot be get inherited. This can be achieved by using the final keyword.

Sample Program
--------------------------------------------------------------------------------------------------------------------
final class ClassAndObjects {
void display() {
System.out.println("This is final class");
}
public static void main(String[] args) {
ClassAndObjects a2 = new ClassAndObjects();
a2.display();
}
}
--------------------------------------------------------------------------------------------------------------------

Output:
This is final class

6. POJO(Plain Old Java Object) Class

    This type of class contains only private variables. So, it must contain a getter and setter method and this is fully encapsulated.

Sample Program
--------------------------------------------------------------------------------------------------------------------
class ClassAndObjects {
private String pojo = "This is an example of POJA class";
public String getPojo() {
return pojo;
}

public void setPojo(String pojo) {
this.pojo = pojo;
}
public static void main(String[] args) {
ClassAndObjects a2 = new ClassAndObjects();
System.out.println(a2.getPojo());
}
}
--------------------------------------------------------------------------------------------------------------------

Output:
This is an example of POJA class


Post your questions in the comment to get clarified and support us to improvise us.

Comments