Java Generic

 

                           

                                    Java Generic 

Basic Examples

 Generic Class Example: NEW

In this example, Box is a generic class that can hold a value of any type. The <T> in Box<T> is a type parameter, and it represents the type that will be specified when creating an instance of the Box class. Here's how you can use it:



 Generic Class Example with supper class: NEW

In this example:

We have a generic class called StorageBox<T>, which extends a non-generic superclass Box<T>. T is the type parameter that is inherited from the superclass.

The StorageBox class has an additional field called storageLocation to store information about the storage location of the item.

The constructor of StorageBox takes both the value and the storage location as parameters and calls the superclass constructor using super(value) to set the value.

Here's how you can use this StorageBox class:


As you can see, the StorageBox class can be parameterized with different types (Integer and String in this case), and it inherits the generic type parameter T from its superclass Box<T>.



Generic Method Example: NEW

In this example, Utils is a class with a generic method called getFirstElement. This method takes a List<T> as an argument and returns the first element of the list. Here's how you can use it:

The <T> in Utils.<T>getFirstElement(...) specifies the generic type for the method, and it's inferred based on the type of the list passed as an argument.

These examples demonstrate the use of generic classes and methods in Java, which allow you to create reusable and type-safe components that can work with different data types.



Small use case related example:


In this example, Storage is a generic class that has a type parameter T with an upper bound extends Product. This upper bound constraint specifies that T must be a subclass of (or the same class as) the Product class. Here's how you can use this Storage class:






Nice Must Read Examples

===========================================================================================

Type1:  class generic

       We can define class which can be vary like this <T>

    Eg1:


       when the extract implementation relevant class can be provided 


   Eg2
   




Type 2:  (V.V) sub class to super class generic

  VV => { Reason for mentioning the extended class like (BIR extends BaseIndexerRunner) is only for identifying the supper class, otherwise methods or variables inside the "BIR" can not be identified to write the logic inside generic class(BaseIndexer) since we do not know even super class ,So when compiler know the super class we can use its methods inside the generic class(BaseIndexer) like new BIR().method1()  (if we do not use then no need of mentioning the extended class) }
 

V.V. Example project Code with kafka consumer :  https://github.com/jpssasadara/java-Generic-pick-EX

Type 3 method generic




Comments

Popular posts from this blog

Hibernate (Java) -- by jps sasadara

Observer Design Pattern & RxJava & @Async

JAVA uml Based cording <<< by jps sasadara >>>