Java Types of Classes

After learning "what is class in Java", let us go to see how many Java Types of Classes exist.

Java supports three types of classes.

1. Concrete classes
2. Abstract classes
3. Interfaces

1. Concrete Classes

Java comes with two types of classes – Concrete classes and Abstract classes. What we call, generally a class in Java, is known infact as concrete class. Other way, to say, a Java class which is not abstract, is a concrete class. A concrete class can be final also.

a) public class Employee
b) public final class Employee

"public" is known as an access specifier. A class can have only two access specifiers – public or default. In the second statement, final adds extra meaning and purpose (known as functionality) to Employee class. final is known as access modifier. We discuss separately about access specifiers and access modifiers and their differences and comparison.

Following code gives a simple example on concrete classes, one of Java Types of Classes.
import java.lang.*;
public class Demo
{                             // OPEN BRACE OF THE CLASS
  public static void main(String args[])
  {
    System.out.println("Hello World");
    System.out.println("Bless me I am Learning Java");
  }
}                             // CLOSE BRACE OF THE CLASS

Java Types of ClassesOutput screenshot of Concrete classes on Java Types of Classes

import java.lang.*;

In the above statement import is a keyword of Java equivalent to include of C/C++. java.lang is known as a package. A package contains classes and interfaces. String and System classes we are using in the above code belong to this package only. We discuss keywords Java supports shortly.

public class Demo

The basic construct of Java programming is a Class. All the code you write must be put within the class declaration only – between open brace and close brace. Here, public means any other class can access Demo class code without any restrictions.

public static void main(String args[])

static is another access modifier which allows JVM to call the main() method without the help of an object. void means the main() does not return a value. The main() takes a parameter of String array by name args used later to access command-line arguments.

System.out.println(“Hello World”);
System.out.println(“Bless me I am Learning Java”);

System.out.println() is equivalent to printf() of C-lang which prints the messages at command prompt.

Follow the links to know more about the words used in the code.

1. Java Example Compilation Execution
2. Keywords Java
3.Access Specifiers Permissions and Restrictions
4. Access Specifier vs Access Modifier Java

2. Abstract Classes

An abstract class differs from concrete class in that a few method of abstract class misses the body part. Any method without body is known as abstract method. Another rule is is a class contains abstract methods, the class should be declared as abstract class. That is, abstract class can contain abstract methods and also we cannot create objects with abstract class.

Following code gives a simple example on abstract classes, one of Java Types of Classes.
abstract class Employee
{
  public abstract void calculate(int basicPay, int houseRent);   // observe abstract method declaration
}
public class Officer extends Employee
{
  public void calculate(int basicPay, int houseRent)              // abstract method is given body here in subclass
  {
    System.out.println("Officer total salary Rs." + (basicPay + houseRent));
  }
  
  public static void main(String args[])
  {
    Officer manager = new Officer();
    manager.calculate(7500, 2000);
  }
}

Java Types of ClassesOutput screenshot of Abstract classes on Java Types of Classes

Observe, abstract keyword is used with both class and method. Abstract method calculate() does not have body in super class Employee and is overridden by subclass Officer with body.

More on abstract classes is given in Abstract Classes

3. Interfaces

Coming to interfaces, an interface is not completely new. It is after all a type of abstract class where all methods should be abstract (know, abstract class can contain a mixture of concrete and abstract methods). It means, interface contains only abstract methods. Basically Java does not support multiple inheritance. But supports partially through interfaces. To inherit concrete and abstract classes, use extends keyword and with interfaces use implements keyword. Like abstract classes, with interfaces also you cannot create objects and all the abstract methods should be overridden by the subclass.

Following code gives a simple example on interfaces, one of Java Types of Classes.
interface Employee                                      // 1st interface
{
  public abstract void calculate(int basicPay, int houseRent);   
}

interface Officer                                       // 2nd interface
{
  public abstract void getData(int id, String name);   
}

public class Engineer implements Employee, Officer      // observe implements and writing two interfaces
{                                                       // an example of multiple inheritance
  public void calculate(int basicPay, int houseRent)    // start overriding all abstract methods in subclass Engineer
  {
    System.out.println("Engineer Salary is Rs." + (basicPay + houseRent));  
  }
  public void getData(int id, String name)          
  {
    System.out.println("Engineer ID: " + id + " and Name: " + name);
  }
  public static void main(String args[])
  {
    Engineer eng1 = new Engineer();
    eng1.calculate(9999, 1111);
    eng1.getData(100, "S N Rao");
  }
}

Java Types of ClassesOutput screenshot of Interfaces on Java Types of Classes

Observe, after implements two interfaces came. It is not possible with abstract classes. After extends, there should be only abstract class.

More examples with indepth knowledge is given in Java Interfaces Multiple Inheritance

1 thought on “Java Types of Classes”

Leave a Comment

Your email address will not be published.