Diferencia entre clase e interfaz (con tabla)

Both Class and Interface defines what an object looks like and also the behavior of an object. If we study and understand both the structures thoroughly, it will be easier to deal with the programming. They are the two basic structures in an object-oriented programming language.
Also, better understanding will enhance our coding quality and the developer’s experience.

Class vs Interface

The main difference between a Class and an Interface is that a class can contain both abstract and concrete methods. Whereas, an interface can contain only abstract methods. A class may reach partial abstraction but an interface always achieves full abstraction.

Class is a basic structure in a program from which objects are created. The class itself belongs to certain packages that contain an identical group of bundles packed together. The declaration of a class generally contains keywords, modifiers, and a class name.
A class can be further classified into two categories:
i. Built-in-classes
ii. User-defined classes

The idea of the interface was introduced for the implementation of multiple classes at a time. It contains the method declaration of a program. Not only abstract methods, but an interface can also contain static methods. They are always declared as public by default.

Comparison Table Between Class and Interface

Parámetros de comparaciónClaseInterfaz
DefiniciónA class basically describes and defines the behavior of an objectInterface basically contains the attributes and behavior defined by a class
KeywordThe keyword “class” is used to declare a classThe keyword “interface” is used to declare an interface
MétodosA class is allowed to have both abstract and concrete methods. It means the method can have a bodyAn interface can have only abstract methods. It means the method can not have a body
Access specifierThe members in a class can be either public, protected, or defaultAll the members of an interface, by default, are public
HerenciaA class does not support multiple inheritances. It uses the keyword extends to inherit another classAn interface does support multiple inheritances. It can however inherit only an interface

What is Class?

A class is a blueprint or a plan, from which objects are created. It is a group of objects having similar properties. It is a logical entity and can not be physical. A class name conventionally starts with a capital letter.

It is the basic concept of object-oriented programming. Determines the behavior of an object. It can have any number of methods and can access its values.

A class can contain the following type of variables:
A local variable, instance variable, and class variable.

The syntax of a class is as follows:

class {
field;
method;
}

To create an object under a class, we need to use the keyword known as ‘new’, along with a constructor. A constructor has the same name as that of a class. Its function is similar to that of a method.

For example, if the class name is Program, the constructor is Program().
To access the members of the class, we need to use the name of the object along with the class.

What is Interface?

The interface is defined as a group of similar methods but with empty bodies.

When we implement an interface, it makes a class more proper. This in turn makes the behavior of the class more formal.
It is a contract enforced by the compiler which connects the class to the outside world.

Before a class gets compiled successfully, the methods defined by the interface must appear in the source code. To compile a class, we need to add the public keyword at the beginning. There can be no private variables and methods.

It comprises abstract methods as well as static constants. An interface can not contain any method body. It is generally used for achieving multiple inheritances in a program.

Default and static methods can be introduced in an interface. Also, we can have private methods in an interface. It is used for achieving abstraction.

Main Differences Between Class and Interface

  1. In a class, the keyword named class is used to declare it. In an interface, a keyword named interface is used to declare it.
  2. The members of a class don’t need to be public. It can be public, private, as well as protected. Whereas the members of an interface are public by default.
  3. A class can have a method body. It contains concrete methods. The interface can not have a method with bodies. They contain abstract methods.
  4. A class is allowed to extend only a single class but as many interfaces are required. An interface can not implement interfaces itself but is allowed to extend any number of classes. This in turn is an advantage of multiple class implementation.
  5. Classes in a program might have constructors. They are a block of coding structures in a program, quite similar to methods. The interface does not have any constructor.

Conclusión

To frame a program, Class and Interface are the two most basic elements. They have their distinct functions to serve. However, they do have quite a few similarities when compares syntactically. The keyword “class” is used to create a class and the keyword “interface” is used to create an interface. We need to keep in mind their proper formats in order to execute the required program.

A class can have a method body whereas an interface can not. Coming to methods, classes do contain both static and final methods which can be public or private. In the case of interface, it can not have either of the two methods, which is always public. This is basically to provide the accessibility to the class that is to be implemented.

Another important point is that the implementation of a class also requires the validation of all methods present in the interface. Thus without a doubt, class and interface are solely dependent on each other.

Referencia

  1. https://ieeexplore.ieee.org/abstract/document/6141290/
  2. https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0009813
x
2D vs 3D