Java Classes


The basic unit of Java coding is a class. All the code you write should be enclosed within the class. Java classes can be divided into three types – Concrete classes, Abstract classes and Interfaces. Out of these, interface has special focus as Java achieves multiple inheritance through interfaces.

A. Concrete class

Concrete class contains all concrete methods (non-abstract). Following is an example on Concrete class of Java Classes.

public class Demo
{
  public void display()      // a concrete method
  {
    System.out.println("From display() concrete method");
  }
  public static void main(String args[])
  {
    Demo d1 = new Demo();
    d1.display();
  }
}

Java ClassesOutput Screenshot of Concrete class on Java Classes

The concrete class contains, of course, only one concrete method and is called from main() method with an object.

B. Abstract class

Abstract class comes with three variations – all concrete methods (non-abstract) or all abstract methods or a mixture of concrete and abstract methods. An abstract method differs from concrete method in that the abstract method does not have a body. All the abstract methods of the abstract class should be overridden by subclass with body. Moreover, we cannot create objects with abstract class. Following is an example on Abstract class of Java Classes.

abstract class Test                 // observe abstract keyword with class
{
  public abstract void display();   // observe, abstract keyword with method
}
public class Demo extends Test
{
  public void display()             // overrides super class abstract method display() with body
  {
    System.out.println("Subclass overridden display() method");
  }
  public static void main(String args[])
  {
    Demo d1 = new Demo();
    d1.display();
  }
}

Java ClassesOutput Screenshot of Abstract class on Java Classes

display() method is declared as abstract in super class Test and is given body by subclass.

C. Interface

Interface is a special variation of abstract class where all methods should be abstract. That is, interface should not have even one concrete method. All the abstract methods of the interface should be overridden by subclass with body. Moreover, we cannot create objects with interface. Java supports multiple inheritance through interfaces. That is, with concrete classes and abstract classes Java does not support multiple inheritance. One more difference is use implements keyword to inherit interfaces. Following is an example on interfaces of Java Classes.

interface Test                            // 1st interface; observe interface keyword
{
  public abstract void display();         // abstract method without body
}
interface Hello                           // 2nd interface
{
  public abstract void show();            // abstract method
}
public class Demo implements Test, Hello  // observe two inheritances with implements
{
  public void display()                   // override all abstract methods with body
  {
    System.out.println("Subclass overridden display() method");
  }
  public void show() 
  {
    System.out.println("Subclass overridden show() method");
  }
  public static void main(String args[])
  {
    Demo d1 = new Demo();
    d1.display();
    d1.show();
  }
}

Java ClassesOutput Screenshot on Java Classes

display() and show() methods are abstract methods in interfaces and overridden by subclass Demo.

public class Demo implements Test, Hello

Demo uses implements (not extends) keyword to inherit interfaces.

Remember, after extends there should be one class of either concrete or abstract. After implements, there should be interfaces only of any number.

For more Explanation on the above three examples refer the links.

1. Java class example
2. Java abstract classes
3. Java Interfaces
4. Java Constructor

Leave a Comment

Your email address will not be published.