Java: Type of Classes


Pre-Requisite: Java: Class
In a previous post, you have got an understanding of java class declaration and creation.

Types of classes

Java supports lots of types of classes:

  • Concrete Class
  • Abstract Class
  • POJO Class
  • Static Class
  • Nested Class/Inner Class
  • Final Class
  • Anonymous Class
  • Lambda Expression

Here we will discuss these classes in detail.

Concrete Class

A concrete class is a normal class that is not declared with Non-access modifiers as abstract, final, etc. This class can have an implementation of a parent, interfaces or own class methods.

Example: Concrete Class

//Example Concrete Class
public class CalculatorTest {
    static int add(int a , int b)
    {
    	return a+b;
    }
    static int substract(int a , int b)
    {
    	return a-b;
    }
    static int multiply(int a , int b)
    {
    	return a*b;
    }
    static int division(int a , int b)
    {
    	return a/b;
    }
	public static void main(String[] args) {
      System.out.println("4+5 ="+add(4,5));
      System.out.println("4-5 ="+substract(4,5));
      System.out.println("4*5 ="+multiply(4,5));
      System.out.println("4/5 ="+division(4,5));
	}
}

See Also: Java: Concrete Class Examples

Abstract Class

A class that is declared with the keyword abstract is known as an abstract class in Java. It can have abstract and concrete methods (method with the body). An abstract class can not be instantiated but need to extend to implement abstract methods.

Example: Abstract Class

args)
{
Vehicle v=new Car();
v.engine();
v=new Bike();
v.engine();
v=new Truck();
v.engine();
v=new Bus();
v.engine();
}
}

See Also: Java: Abstract Class Examples

POJO Class

A class with only private variables and public getter and setter methods is called as POJO(Plain Old Java Object) class. These getter and setter methods use private variables. This class is completely encapsulated.

Example: Pojo Class

//POJO Class
public class Animal {
	//private  variables
    private String name;
    private String breed;
    private int age;
    private String color;
    //Getter and setter methods
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getBreed() {
		return breed;
	}
	public void setBreed(String breed) {
		this.breed = breed;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
}

See Also: Java: Pojo Class Examples

Static Class

A static class is a nested class declared as a static member of the class.

Example: Static Class

import java.util.Scanner;

public class StaticClasses {
	static int s;// static variable

	// static method
	static void printSum(int a, int b) {
		s = a + b;
		System.out.println(a + "+" + b + "=" + s);
	}

	static class NestedStaticClass//static class
	{
		static//static block
		{
			System.out.println("Inside Nested Class Static Block");
		}

		public void display()
		{
			Scanner scanner=new Scanner(System.in);
			System.out.println("Enter value of a:");
			int a= scanner.nextInt();
			System.out.println("Enter value of b:");
			int b= scanner.nextInt();

			printSum(a,b);
			System.out.println("Sum of numbers a+b:" +s);

		}
	}
}

public class StaticClassTest {

	public static void main(String[] args) {
		StaticClasses.NestedStaticClass nss=new StaticClasses.NestedStaticClass();
		//call method of nested class method
		nss.display();
	}
}

See Also: Java: Static Keyword & Examples

Nested Class/Inner Class

A Class declared inside of another class is called Nested Class/Inner Class.

Example: Nested Class/Inner Class

public class OuterClass {

	//nested/Inner Class: Class inside the class
	class NestedClass
	{
		public void innerMethod()
		{
			System.out.println("Inner Class Print");
		}
	}

	public static void main(String[] args) {
		System.out.println("Outer Class Print");

	}

}

See Also: Java: Nested/Inner Class Examples

Final Class

A Class declared with the final keyword is known as Final Class. This class can not be extended by another class. For Example java.lang.System, java.lang.String

Example: Final Class

public final class FinalClass {
 public void display()
 {
	 System.out.println("Display final class method.");
 }
}

//show compiler error :
//"The type BaseClass can not subclass the final class FinalClass"
class BaseClass extends FinalClass{
	public void display()
	 {
		 System.out.println("Display base class method.");
	 }
}

See Also: Java: Final Keyword & Examples

Anonymous Inner Class

Anonymous Class is an inner class without a name and for which only a single object is created. Such class is useful when you need to create an instance of an object such as overloading methods of a class or interface, without having to actually subclass a class.

Anonymous inner classes are mostly used in writing implementation classes for listener interfaces in graphics programming.

Example: Anonymous Class

//Using Anonymous Inner class Thread that extends a Class
class MyThread
{
    public static void main(String[] args)
    {
        //Here we are using Anonymous Inner class
        //that extends a class i.e. Here a Thread class
        Thread t = new Thread()
        {
            public void run()
            {
                System.out.println("Child Thread");
            }
        };
        t.start();
        System.out.println("Main Thread");
    }
}

See Also: Java: Anonymous Class Examples

Lambda Expression

Lambda expressions added in Java 8. It useful to create instances of functional interfaces (An interface with single abstract method). For Example: java.lang.Runnable is having one abstract method as run().

Example: Lambda Expression

public class LamdaTest {
	public static void main(String[] args) {
		new Thread(() -> System.out.println("This is Lamda test")).start();
	}
}

See Also: Java: Lamda Expression Examples

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s