Types of classes in Java
Concrete class
Any normal class which does not have any abstract method or a class that has an implementation of all the methods of its parent class or interface and its own methods is a concrete class.
Example
public class Concrete { // Concrete Class static int product(int a, int b) { return a * b; } public static void main(String args[]) { int p = product(2, 3); System.out.println("Product: " + p); } }
Output
Product: 6
Abstract class
A class declared with abstract keyword and have zero or more abstract methods are known as abstract class. The abstract classes are incomplete classes, therefore, to use we strictly need to extend the abstract classes to a concrete class.
Example
abstract class Animal { //abstract parent class public abstract void sound(); //abstract method } public class Dog extends Animal { //Dog class extends Animal class public void sound() { System.out.println("Woof"); } public static void main(String args[]) { Animal a = new Dog(); a.sound(); } }
Output
Woof
Final class
A class declared with the final keyword is a final class and it cannot be extended by another class, for example, java.lang.System class.
Example
final class BaseClass { void Display() { System.out.print("This is Display() method of BaseClass."); } } class DerivedClass extends BaseClass { //Compile-time error - can't inherit final class void Display() { System.out.print("This is Display() method of DerivedClass."); } } public class FinalClassDemo { public static void main(String[] arg) { DerivedClass d = new DerivedClass(); d.Display(); } }
In the above example, DerivedClass extends BaseClass(final), we can't extend a final class, so compiler will throw an error. The above program doesn't execute.
Output
cannot inherit from final BaseClass Compile-time error - can't inherit final class
POJO class
A class that contains only private variables and setter and getter methods to use those variables is called POJO (Plain Old Java Object) class. It is a fully encapsulated class.
Example
class POJO { private int value=100; 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
100
Static class
Static classes are nested classes means a class declared within another class as a static member is called a static class.
Example
import java.util.Scanner; class staticclasses { static int s; // static variable static void met(int a, int b) { // static method System.out.println("static method to calculate sum"); s = a + b; System.out.println(a + "+" + b); // print two numbers } static class MyNestedClass { // static class static { // static block System.out.println("static block inside a static class"); } public void disp() { int c, d; Scanner sc = new Scanner(System.in); System.out.println("Enter two values"); c = sc.nextInt(); d = sc.nextInt(); met(c, d); // calling static method System.out.println("Sum of two numbers-" + s); // print the result in static variable } } } public class Test { public static void main(String args[]) { staticclasses.MyNestedClass mnc = new staticclasses.MyNestedClass(); // object for static class mnc.disp(); // accessing methods inside a static class } }
Output
static block inside a static class Enter two values 10 20 static method to calculate sum 10+20 Sum of two numbers-30
Inner Class
A class declared within another class or method is called an inner class.
Example
public class OuterClass { public static void main(String[] args) { System.out.println("Outer"); } class InnerClass { public void inner_print() { System.out.println("Inner"); } } }
Output
Outer