Is an interface a special kind of class or can you say that an interface isn't a class at all?
A class is a blueprint from which we can create objects that share the same configuration - properties and methods. An interface is a group of related properties and methods that describe an object, but neither provides implementation nor initialisation for them.
An interface in Java is a blueprint of a class. A Java interface contains static constants and abstract methods. The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not the method body.
Interface is not a class, its just interface. It has got some methods with no body inside it, just the method signature (they are basically abstract methods!), and may have some final and static variables. And, a typical class “implements” interfaces, not “extends”.
An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
An interface isn't a class, but you could say that both interfaces and classes are types.
From the Java specification:
In the Java programming language, every variable and every expression has a type that can be determined at compile-time. The type may be a primitive type or a reference type. Reference types include class types and interface types.
Notice though there is a special class called Class<T>
that can represent both classes and interfaces:
Instances of the class Class represent classes and interfaces in a running Java application.
The fact that an interface is represented by a Class
instance where isInterface
is true
could give you the impression that an interface is just a special type of class. However this is not the case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With