Java Classes and Interfaces

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.

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 Reply

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