Interface is another way to achieve abstraction.
Interface is 100% abstract class which is used to group together all similar methods with empty body.
Note: A class extends to another class. An interface extends another interface. But, a class also always implements single or multiple interfaces with the keyword “implements” and all its methods.
In below example Vehicle interface gets implement by the Car class.
Example:
OUTPUT:
Car
Multiple Inheritance is not supported in the case of a class because of ambiguity. But, it is supported in the case of interfaces because the same method coming from different interfaces in a child class using multiple inheritance will always have a single definition.
Example:
OUTPUT:
Test User
A functional interface is an interface that contains only one abstract method.
Interface will have only one functionality to perform. A functional interface can have any number of default and static methods with an implementation.
Runnable is a good example of functional interface.
Function interface plays an important role for writing lambda expression which helps us to write short and clean code.
@FunctionalInterface
annotation allows us to declare a functional interface having only one abstract method.
Java 8 introduces a new technique where it allows us to write implementation of default method in interfaces.
Single interface can have any number of default method implementation.
It provides us the backward compatibility to add some functionality for existing interfaces without changing existing logic of classes implementation the same interface.
For example, if we have an interface and same is implementing by multiple classes. After adding a default method or methods will not force us to update the existing classes implementing the same interface.
Similar to default method interface can also have default static method implementation.
Below is the example for defining default and static methods in an interface.
OUTPUT:
It is an empty interface having no fields and methods inside it. Implementation of these interfaces will give a special definition to a class. Serializable and Clonnable interface are the example of marker interface. Cloneable interface belongs to java.lang.Cloneable and and serializable interface belongs to java.io.Serializable.
Syntax:
Public interface {
// having no fields and methods
}
Cloning is similar to making a copy of an object. Below is the Employee class example where it has two fields with name and age stored in emp1 object. The clone() method will help us to make a copy of emp1 to emp2 with all field values.
Example :
OUTPUT:
Note:Accessing clone() method without implementing cloneable interface will throw CloneNotSupportedException exception
Serialization in java can be achieved by implementing Serializable interface.
Serialization allows us to serialize the object or write the state of object as it is into byte streams. The reverse of the serialization is deserialization where byte stream converts back to object with the same state what we serialized.
For serialization java uses writeObject() and for deserialization it is readObject().
Declaring a variable as transient inside a class will not allow to serialize or write the object into byte stream.
Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.
📝 Blogs
javadoubts.com © All rights reserved