☰ See All Chapters |
Java class vs. interface
In this chapter you learn the differences between class and interface.
Class | Interface |
A class contains the implementations. | An interface contains specifications. |
A class can contain the following members:
| An interface can contain the following members:
|
Members of a class can be public, private, protected or default. | All the members of the interface should be public. If no access specifier specified for members then compiler add public access specifier. |
We can create objects for class. class A { } class B { A a = new A(); } | We cannot create objects for interface. |
Class can be extended by class using extends keyword. | Interface can be implemented by class using implements keyword. |
Class does not support multiple inheritance. Class can extend only one class. abstract class A { } class B extends A { } | Interface supports multiple inheritance. Class can implement any number of interfaces. interface A { } interface B { } class C implements A, B { } |
It does not support dynamic polymorphism. | It supports dynamic polymorphism. |
All Chapters