0% found this document useful (0 votes)
2 views18 pages

Functional Interface in Java

The document discusses generics in Java, highlighting their role in creating parameterized types for code reusability and type safety. It also covers default methods and static methods in interfaces, introduced in Java 8, which allow for concrete methods and utility functions without breaking existing implementations. These features enhance backward compatibility and support multiple inheritance of behavior.

Uploaded by

Anushka Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

Functional Interface in Java

The document discusses generics in Java, highlighting their role in creating parameterized types for code reusability and type safety. It also covers default methods and static methods in interfaces, introduced in Java 8, which allow for concrete methods and utility functions without breaking existing implementations. These features enhance backward compatibility and support multiple inheritance of behavior.

Uploaded by

Anushka Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Generics in Java

 Generics means parameterized types.


 They allows us to write code that works with different data types using a single class,
interface or method.
 Instead of creating separate versions for each type, we use type parameters (like <T>)
to make the code reusable and type-safe.
 You define a class, interface, or method with a type parameter in angle brackets (<T>).

Key Benefits:

1. Type Safety: Ensures you get compile-time type checking and avoid runtime
ClassCastExceptions.
2. Reusability: A single class or method can be used with different types.
3. Elimination of Casting: You don't need to cast objects, as they are already of the
specified type.

Generic class
Generic method

Default Methods in Interface

 Default methods are methods in an interface that have a method body.


 They were introduced in Java 8 to allow interfaces to have concrete methods without
affecting classes that already implement the interface.
 Declared using the default keyword.
 They provide a way to add new methods to interfaces without breaking existing code that
implements them.
 Default methods are also known as defender methods or virtual extension methods.

 Uses of Default Methods:


o To add new functionality to an interface without forcing all implementing classes to
override it.
o To provide a default implementation that can be inherited by implementing
classes.
o To enable backward compatibility — old classes implementing the interface will
still work even after new methods are added.
o To support multiple inheritance of behavior (since a class can implement multiple
interfaces with default methods).

Static Methods in Interface

 Static methods are methods in an interface that are declared using the static keyword.
 They were introduced in Java 8 to allow interfaces to have utility/helper methods.
 Static methods belong to the interface itself, not to the objects implementing the interface.
 They can be called using the interface name without creating an object of the implementing class.
 Static methods cannot be overridden by implementing classes.

 Uses of Static Methods:


o To define utility/helper methods inside interfaces, similar to static methods in classes.
o To avoid creating separate utility classes for related helper functions.
o To provide common functionality for all implementing classes without the need for
overriding.
o To keep related functionality encapsulated within the interface.
Programs Of Functional Interface

You might also like