Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an interface a class?

Tags:

java

interface

Is an interface a special kind of class or can you say that an interface isn't a class at all?

like image 220
smallB Avatar asked Jul 30 '12 11:07

smallB


People also ask

Is interface same as class?

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.

Is interface a class or method?

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.

Do interfaces count as classes?

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”.

Is class and interface same in Java?

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.


1 Answers

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.

like image 95
Mark Byers Avatar answered Sep 17 '22 02:09

Mark Byers