What is the Difference Between Class and Interface?
🆚 Go to Comparative Table 🆚The main difference between a class and an interface in Java is that a class describes the behavior of an object, while an interface contains the behaviors assigned and defined by the class. Here are some key differences between classes and interfaces:
- Keyword: A class is created using the keyword
class
, while an interface is created using the keywordinterface
. - Objects: Classes can have objects created from them, while interfaces cannot.
- Method Bodies: Classes can contain method bodies, while interfaces can only contain method signatures (i.e., abstract methods).
- Inheritance: Classes can inherit from other classes using the
extends
keyword, while interfaces can be inherited by classes using theimplements
keyword. - Variables: Classes can have different types of variables (instance, static, final), while interface variables are always public, static, and final.
- Constructors: Classes can have constructor methods, while interfaces cannot.
In summary, classes are used to create objects and contain method bodies, while interfaces are used to define the capabilities that a class must implement. An interface is a blueprint of a class, specifying the methods and properties that a class implementing the interface must have.
Comparative Table: Class vs Interface
Here is a table comparing the differences between a class and an interface:
Parameter | Class | Interface |
---|---|---|
Keyword used | class | interface |
Objects can be created | Yes | No |
Inheritance | A class can extend another class | A class can implement an interface |
Inherited by | A class can be extended by another class | A class can implement multiple interfaces |
Constructors | Yes, a class can contain constructors | No, an interface cannot contain constructors |
Abstract methods | No, a class cannot contain abstract methods | Yes, an interface can contain abstract methods |
Access specifiers | Variables and methods in a class can be declared using any access specifier (public, private, protected, default) | All variables and methods in an interface are declared as public |
Variables | Variables in a class can be static, final, or neither | All variables in an interface are static and final |
In summary, a class is a blueprint for creating objects and can contain constructors, non-abstract methods, and variables with various access specifiers. An interface, on the other hand, is a contract that a class agrees to implement, containing abstract methods and public variables. Classes can extend other classes and implement interfaces, while interfaces can extend other interfaces and be implemented by classes.
- Abstract Class vs Interface
- Objects vs Classes
- Package vs Interface in Java
- Inheritance vs Interface in Java
- Classes vs Structures
- Class vs ID
- Abstract Class vs Concrete Class
- Abstract Class vs Inheritance
- Class vs Instance Variables
- Class Diagram vs Object Diagram
- Superclass vs Subclass
- Phylum vs Class
- Implements vs Extends
- Class vs Structure in C#
- ER Diagram vs Class Diagram
- Virtual vs Abstract
- Encapsulation vs Abstraction
- Polymorphism vs Inheritance in OOP
- C vs C++