contador javascript Saltar al contenido
Contact : alejandrasalcedo0288@gmail.com

Difference between class and interface in Java


Both Class and Interface are used to create new reference types. A class is a collection of fields and methods that operate in fields. An interface has totally abstract methods, that is, methods with anyone. An interface is synthetically similar to the class, but there is an important difference between class and the interface that is a class that can be instantiated, but an interface can never be instantiated. So let's learn more differences between a class and an interface with the help of a comparison chart shown below.

Comparative graph

Basis for comparison Class Interface
BASICAn instance of a class is created to create objects.You can never create an instance of an interface, as the methods cannot perform any action when invoking.
KeywordclassInterface
Access specifierClass members can be private, public or protected.The members of an interface are always public.
MethodsThe methods of a class are defined to perform a specific action.The methods in an interface are purely abstract.
Implement / ExtendA class can implement any interface number and can extend only one class.An interface can extend several interfaces but cannot implement any interface.
BuilderA class can have constructors to initialize the variables.An interface can never have a constructor, since there is almost no variable to initialize.

Class definition

A class is the most important aspect of classless Java programming that a Java program cannot implement. A class creates reference types and these reference types are used to create objects. A class has a signature and a body. The signature of a class has the name of the class and the information that indicates whether the class has inherited another class. The body of a class has fields and methods that operate in those fields. A class is created using a kind of keyword. Let's see how to declare a class.

 clase nombre_clase {/ * campos ... mtodos * /} 

When an instance of a class is created, each object created contains a copy of the fields and methods with them. Fields and members declared within a class can be aesthetic or non-aesthetic. The value of the static members is constant for each object, while the non-static members are initialized by each object differently according to their requirement.

Members of a class have access specifiers that decide the visibility and accessibility of the members to the user or subclasses. The access specifiers are public, private and protected. A class can be inherited by another class using the access specifier that decides the visibility of the members of a superclass (inherited class) in a subclass (hereditary class). In this way, the class complies with the concept of concealment and encapsulation of data in object-oriented programming.

Interface definition

An interface is also among the reference types defined in Java. The importance of an interface is that in Java, a class can only inherit a single class. To avoid this restriction, Java designers introduced an interface concept. An interface is synthetically similar to a class, but it lacks field declaration and the methods within an interface have no implementation. An interface is declared using a interface of keyword. Let's look at the declaration of an interface.

 interfaz nombre_interfaz {tipo var_name = valor; tipo method1 (lista de parmetros); tipo method2 (lista de parmetros); . . } 

An interface does not define any method declared within it because it lacks fields to operate. Just because any method within an interface does not perform any action, an interface can never be instantiated. If an interface has any field member, it must be initialized at the time of its declaration. An interface never contains any constructor, since it lacks field members to start. Therefore, an interface only defines what a class should do instead of how it should do it.

An interface created once created can be implemented by any number of classes using implements of keywords. But classes that implement an interface must define all methods within an interface. An interface can also inherit another interface using the keyword extend . If a class implements an interface that extends to another interface. Then, a class must define the methods of both interfaces that appear in a string. The methods within an interface must always be public, since the classes that implement them must access them.

Key differences between class and interface in Java

  1. A class can be instantiated by creating its objects. An interface is never created as an instance, since the methods declared within an interface are abstract and do not perform any action, so the creation of instances of an interface is not used.
  2. A class is declared using a keyword class. In the same way, an interface is created using a keyword interface.
  3. Class members can have the access specifier as public, private, protected. But the members of an interface are always public, since the classes that implement them must access them.
  4. The methods within a class are defined to perform an action on the fields declared in the class. Since the interface lacks the declaration of fields, the methods within an interface are purely abstract.
  5. A class can implement any number of interfaces but can extend only one sper class. An interface can extend any number of interfaces but cannot implement any interface.
  6. A class has constructors defined within it to obtain the initialized variable. But, an interface has no constructor since there are no fields to initialize. The fields of an interface are initialized only at the time of their declaration.

Conclusion:

Both classes and interfaces have their own importance. A class is used when we need to define how the task is done. An interface is used when we need to know what task needs to be done.