1/34
Module 3: Generics & Serialization
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
It is a method of allowing abstraction over types.
Generics
This make classes more dynamic in terms of handling types.
Generics
Instead of allowing a specific type, a _____ object accepts different type as its parameter.
generic
Using generics, we can achieve the following:
Abstraction over Type
Enforce Compile Time Type Safety
Strengthens Relationship
Declaration of Programmers Intent
Removes the need of casting
a class will not be bounded by a specific type
Abstraction Over Type
moves checking of ClassCastException to compilation time
Enforce Compile Time Type Safety
In Generics, we move the checking of ClassCastException to ____ time
compilation time
code states how class must be used instead of focusing on type
Declaration of Programmers Intent
Limitation of Generics
Instantiate a generic type
Use primitive for generic type
Infer compatibility with classes
To apply generics for a class, _____ brackets are provided after the classname.
angle
public class Sample<?>{ /* class code */ }
What values can be put in the “?” ?
T - Type
E - Element
K - Key
N - Number
V - Value
We can limit the type of data to be accepted even when generics is applied using?
Generic Bounds
There 2 different bound forms in generics.
Upperbound
Lowerbound
uses extends keyword. It is the limit that states that the accepted type must be a subtype of the given class.
Upperbound
uses super keyword. It is the limit that states that the accepted type must be a supertype of the given class.
Lowerbound
All data travels in the form of?
bytes
Aall data travels in the form of bytes. Since object is also a form of data, it is needed to converted to a byte form before it can be passed through a certain network. Java now handles this in a process which they call as ?
Serialization
It is a process in which current state of Object will be saved in stream of bytes.
Serialization
TRUE OR FALSE
As byte stream create is platform neutral hence once objects created in one system can be deserialized in other platform.
TRUE
Translate the Object state to Byte Streams. This Byte stream can be used for different purpose.
Write to disk
Store in memory
Send byte stream to other platform over network
Save in database as Binary Large Object (BLOB)
An interface that is implemented when serialization is desired.
Serializable Interface
This interface means that no methods must be defined to conform with it.
Marker Interface
The Serializable interface can be found from _____ package.
Java IO
NOTES:
If a class implements Serializable then all of its future subclasses are also Serializable automatically
If the superclass did not implement Serializable, the following will happen on deserialization:
The default constructor will be invoked from the superclass.
All fields will initialized with the default values.
NOTES:
If a class implements Serializable then all of its future subclasses are also Serializable automatically
If the superclass did not implement Serializable, the following will happen on deserialization:
The default constructor will be invoked from the superclass.
All fields will initialized with the default values.
It is a version number associated to each serializable class by serialization runtime.
serialVersionUID
This version number is used during deserialization process to verify that the sender and receiver of a serialized object have loaded class for that object which is compatible with respect to serialization.
serialVersionUID
TRUE OR FALSE
Defining a serialVersionUID field in serializable class is mandatory.
FALSE
Defining a serialVersionUID field in serializable class is not mandatory
If a serializable class have explicit serialVersionUID then this field should be of type ____ and must be ____ and ___. It is advised that ____ access modifier must be used.
long
static
final
private
NOTES:
If there is no serialVersionUID field defined explicitly then serialization runtime will calculate default value for that class.
NOTES:
If there is no serialVersionUID field defined explicitly then serialization runtime will calculate default value for that class.
TRUE OR FALSE
It is advisable to define serialVersionUID
TRUE
If there is a difference between serialVersionUID of loaded receiver class and corresponding sender class then ______ will be thrown.
InvalidClassException
To exclude certain fields from Serialization, the _____ modifier can be used.
transient
This exludes the modified field from the process. When the object is deserialized, the instance will receive the default value for the field.
transient
Two ways to exclude certain fields from Serialization
Transient
Static
NOTES:
Another way to exclude is to use static modifier. This also excludes the field from Serialization. However, this is due to a different cause. When a variable is declared as static, the variable is a standalone value. This value is not a part of the instance but rather a part of the class itself. Therefore, serialization will not include it.
NOTES:
Another way to exclude is to use static modifier. This also excludes the field from Serialization. However, this is due to a different cause. When a variable is declared as static, the variable is a standalone value. This value is not a part of the instance but rather a part of the class itself. Therefore, serialization will not include it.