The differences are: 1. The class defines a set of attributes and methods, and the interface only contains abstract methods; 2. The class is a subclass in the inheritance relationship, and the interface is the parent class in the inheritance relationship; 3. The class can Be instantiated, interfaces cannot be instantiated; 4. Classes are used to define specific objects, and interfaces are used to define a set of behaviors; 5. Interfaces can contain default methods and static methods, and classes cannot have static methods or default methods; 6. Classes The members are package-level private, and the members of the interface are public; 7. The class must explicitly call the constructor of its parent class; 8. The class provides encapsulation, but the interface does not.
# Operating system for this tutorial: Windows 10 system, Dell G3 computer.
The difference between Java interfaces and classes
Definition:
Class: A class is a data type in Java that defines a set of properties (member variables) and methods (member functions). A class is a blueprint or template for objects, used to create objects with the same properties and methods.
Interface: An interface is a completely abstract class that only contains abstract methods and no specific implementation. Interfaces can contain constants, abstract methods, default methods, static methods, etc.
Inheritance:
Class: A class is a subclass in an inheritance relationship. It can inherit the properties and methods of the parent class.
Interface: The interface is the parent class in the inheritance relationship, which can be implemented by other classes. A class can implement multiple interfaces.
Implementation:
Class: Class can be instantiated to create objects.
Interface: The interface cannot be instantiated.
Purpose:
Class: Class is used to define specific objects that have specific properties and behaviors.
Interface: Interface is used to define a set of behaviors that can be implemented by any class. This makes interfaces very useful in terms of polymorphism and code reuse.
Default methods and static methods:
Interface: Interfaces can contain default methods and static methods. Default methods are abstract methods for which a default implementation has been provided, whereas static methods are methods that belong to the interface rather than to the class that implements it.
Class: A class cannot have static methods or default methods.
Default access modifier:
Class: The members (properties and methods) of a class are package-level private by default (that is, only other classes in the same package can access).
Interface: Members of the interface are public by default, regardless of whether they are in the same package.
Constructor of subclass:
Class: The constructor of a subclass must explicitly call the constructor of its parent class (using super()).
Interface: A class that implements an interface does not need to explicitly call the interface's constructor.
Access modifiers and encapsulation:
Class: Class provides encapsulation by setting data members to private and using getter and setter methods to access and change the data. This helps hide implementation details and ensure data integrity.
Interface: The interface does not provide encapsulation, and all members are public. This necessitates that classes implementing the interface must provide a complete implementation without hiding any details.
Classes are concrete, stateful entities; interfaces are abstract, stateless contracts. A class can inherit another class; an interface can be implemented by another class. Classes can have constructors; interfaces do not have constructors.
The above is the detailed content of What are the differences between java interfaces and classes. For more information, please follow other related articles on the PHP Chinese website!