Public class Employee implements java.io. Adds a data type to a class: This situation is where the term tagging. ![]() For example, when an interface extends EventListener, the JVM knows that this particular interface is going to be used in an event delegation scenario. Polymorphism is a powerful mechanism in OOP to separate the interface and implementation so as to allow the programmer to program at the interface in the design. Suppose that we have the following Employee class, which implements the Serializable interface − Example other interfaces in the Java API, you can use a tagging interface to create a common parent among a group of interfaces. To demonstrate how serialization works in Java, I am going to use the Employee class that we discussed early on in the book. In other words, you can say that interfaces can. It is used to achieve abstraction and multiple inheritance in Java. There can be only abstract methods in the Java interface, not method body. The interface in Java is a mechanism to achieve abstraction. It has static constants and abstract methods. The next line of inputs are Double which corresponds to, For Rectangle(Option 1) get the. An interface in Java is a blueprint of a class. The return value is Object, so you will need to cast it to its appropriate data type. The first line of the input is an integer corresponds to the shape. Generics enforce type correctness at compile time and, most importantly, enable implementing generic algorithms without causing any extra overhead to our applications. This method retrieves the next Object out of the stream and deserializes it. Java Generics is a powerful addition to the Java language because it makes the programmer’s job easier and less error-prone. Public final Object readObject() throws IOException, ClassNotFoundException ![]() Similarly, the ObjectInputStream class contains the following method for deserializing an object − The purpose of nested classes is to group classes that belong together, which makes your code more readable and maintainable. The above method serializes an Object and sends it to the output stream. In Java, it is also possible to nest classes (a class within a class). Public final void writeObject(Object x) throws IOException The ObjectOutputStream class contains many write methods for writing various data types, but one method in particular stands out − Most impressive is that the entire process is JVM independent, meaning an object can be serialized on one platform and deserialized on an entirely different platform.Ĭlasses ObjectInputStream and ObjectOutputStream are high-level streams that contain the methods for serializing and deserializing an object. ![]() Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.Īfter a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |