Is an interface a special kind of class or can you say that an interface isn't a class at all?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
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.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.
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
and a class is defined like this
So an Interface is an Interface and NOT a class
An interface isn't a class, but you could say that both interfaces and classes are types.
From the Java specification:
Notice though there is a special class called
Class<T>
that can represent both classes and interfaces:The fact that an interface is represented by a
Class
instance whereisInterface
istrue
could give you the impression that an interface is just a special type of class. However this is not the case.Yes, an interface is an instance of
java.lang.Class
. If you have aClass
you can interrogate it to see if it is an interface: http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#isInterface()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 thingimplement
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!