Interfaces provide specific meaning to objects in Java

I have doubts about interfaces in JAVA:

When a class implements an interface, does anything happen to it if it doesn't implement its methods? Simple interface injection, does it provide any meaningful change for the class?

For example, I have two classes: Test1

andTest2

public class Test1 implements Serializable { 
}


public class Test2 {          
}

      

Except for what Test1

implements Serializable

, the classes Test1

and are Test2

identical. In this case, would there be a difference between the functionality / properties of the objects Test1

and Test2

? Is it possible to split class objects Test1

into bytes (just because the class Test1

implements Serializable)?

If so, does it mean that the interface implementation provides some additional value for objects of this class?

+3


source to share


4 answers


Straight from the documentation Serializable

-

The serializability of a class is activated by a class that implements the java.io.Serializable interface. Classes that do not implement this interface will not have their state, serialized or deserialized. The serialization interface has no methods or fields and only serves to define serialization semantics.

Link

Q1> In this case, will there be any difference between the functionality / properties of Test1 and Test2 objects ?

Serializable

is a marker interface, which means there is no method or field.

Q2> Is it possible to split objects of class Test1 into bytes (just because class Test1 implements Serializable ?
Yes.



Q3> If yes, does it mean that the interface implementation provides some additional value for objects of this class ?

Object serialization creates a stream of information about Java classes for the objects that are persisted. For serializable objects, sufficient information is retained to restore those objects if a different (but compatible) version of the implementation of the class is present.

This class can additionally define the following methods:

  • WriteObject method to control what information is saved or add additional information to the stream

  • ReadObject method to read the information written by the corresponding writeObject method or update the state of the object after it is restored

  • The writeReplace method that allows the class to assign a replacement to the object to be written to the stream

Link

+2


source


It depends on the definition of the interface you choose to implement. In the example, you took one of the classes implemented with a serializable interface; it is a marker interface that you didn't need to overload with any of its methods. But there is a difference between the two classes, while objects of class Test1 can be converted to a byte stream for external storage, such a function is not available for objects of Test2. Also, there is a serialId class member that has Test1, but Test2 does not.



This was a specific case; but lets say that the interface you implemented had abstract methods, then Test1 either had to be an abstract class, or it had to concretely define those abstract methods of the interface. In such a scenario, the Test1 and Test2 classes varied greatly from each other. FYI. The body of an interface can contain abstract methods, default methods, and static methods.

+2


source


In Java 8+ it is possible for an interface to provide a default method. For example 13.5.6-1. Adding a default method (which means the interface can add method implementations),

interface Painter {
    default void draw() {
        System.out.println("Here a picture...");
    }
}

      

However, Serializable

being a marker (and, according to Wikipedia, in part), the mere presence of such an interface indicates specific behavior on the part of the implementing class. You can compare it with Externalizable

.

+1


source


An interface in its most basic form is a collection of empty methods that define some basic functionality. A class that implements an interface will need to include all of its methods.

However, the interface Serializable

doesn't have any methods. It simply states that "this object can be serialized" and sometimes they use other special methods ( readObject()

, writeObject()

etc.) to process it.

Here's a quote from the Javadocs :

The serializability of a class is activated by a class that implements the java.io.Serializable interface. Classes that do not implement this interface will not have their state, serialized or deserialized. The serialization interface has no methods or fields and only serves to define serialization semantics.

Therefore, a class can only be serialized if it implements an interface Serializable

.

+1


source







All Articles