Is an interface a special kind of class or can you say that an interface isn\'t a class at all?
No, an interface is not a class in Java.
An interface is a type and all reference types (i.e. non-primitive types) handle quite similarly in Java. Often when people say "class" they are actually referring to a "reference type".
What might be confusing you is that an interface definition is stored in a .class
file, but that's just a technical artifact of Java. In fact all reference type definitions (classes, interfaces, annotations, enums) are stored in .class
files in Java.
Yes, an interface is an instance of java.lang.Class
. If you have a Class
you can interrogate it to see if it is an interface: http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#isInterface()
An interface(is a group of related methods with empty bodies.) is just an interface. Its not a class(A class is the blueprint from which individual objects are created).
notice that you define an interface like this
interface Bicycle {....}
and a class is defined like this
class MyBMX implements Bicycle{...}
So an Interface is an Interface and NOT a class
Interface is just a contract which all implementing classes should follow. An interface is something like a template which cannot make an impact until a class implements it.
The concept of interfaces comes from Abstract Classes, where as abstract classes contains prototypes of method (or abstract methods) and can have few of its methods defined also, while interfaces contains only the prototypes(or signature) of method or abstract methods, whose definition is to be provided by the implementing class. so from the above statement it is clear that interfaces are like 100 percent abstract classes where - none of its method is defined. mentioning it again interfaces are like 100 percent abstract classes but not the classes.
"Interfaces are contracts for what a class can do"
A reason for introducing interface is, we can extend
only single class but interface brought a new thing implement
in java so we can implement thousands of interface.So we can not say that it is a class.
you can get more about this Here!
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.