Java Classes and Interfaces

After learning "What is a class in Java?" and "What is Java Application?", let us know the types of classes Java supports. Java supports basically three types of classes. These types are discussed in this tutorial Java Classes and Interfaces.

1. General classes (known as concrete classes)
2. Abstract classes
3. Interfaces

The basic difference is concrete classes contain all methods with body, abstract class contain some methods without body and some with body and interfaces contain all methods without body. The methods without body are known as abstract methods. One more striking difference of interfaces from the other two types is that interfaces support multiple inheritance. That is, one class can extend any number of interfaces.

A class (or a concrete class) or an abstract class can be inherited by other classes with extends keyword and interfaces must be inherited with implements keyword.

Example on Java Classes and Interfaces is given involving 2 concrete classes and 2 interfaces.
interface Test1                                               // 1st interface   
  public abstract void display();
interface Test2                                               // 2nd interface
  public abstract void show();
class Demo1                                                   // 1st concrete class
  public void calculate1()
    System.out.println("\nFrom calculate1() method");
public class Demo2 extends Demo1 implements Test1, Test2      // 2nd concrete class
  public void display()                                       // override the abstract method of 1st interface
    System.out.println("From display() method");
  public void show()                                          // override the abstract method of 2nd interface
    System.out.println("From show() method");
  public void calculate2()                                      
    System.out.println("From calculat2() method");
  public static void main(String args[])
    Demo2 d1 = new Demo2();                                  // create object
    d1.calculate1();                                         // call each method

Java Classes and InterfacesOutput Screenshot on Java Classes and Interfaces

Test1 and Test2 are interfaces and Demo1 and Demo2 are concrete classes. Demo2 extends Demo1 and implements two interfaces. Being subclass, Demo2 overrides all the abstract methods of both interfaces.

When an interface is implemented by a concrete class, all the interface abstract methods should be overridden by the concrete class; else compilation error.

Also after extends there must be only one class of either concrete class or abstract class and after implements there must be interfaces only of any number.

Dig more into the subject. Start learning classes, abstract classes and interfaces with the following links of this same Web site.

A. Concrete classes
1. Java Example Compilation Execution
2. Java Local Instance Variables
3. Using Variables from Methods
4. Data Binding Data Hiding Encapsulation

B. Abstract classes
1. Abstract Classes

C. Interfaces
1. Java Interfaces Multiple Inheritance

After practicing these topics, go slowly into other topics of this site. All topics are explained with meaning, purpose, program, output screenshots and explanation of the code.

Leave a Comment

Your email address will not be published.