Java Classes

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.

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.

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.

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 Reply

Your email address will not be published. Required fields are marked *