Note the declaration of class:Instead of T, We can use any valid identifier.Ĥ. MyListGeneric myListInteger = new MyListGeneric() MyListGeneric myListString = new MyListGeneric() Now the MyListGeneric class can be used to create a list of Integers or a list of Strings Let’s replace String with T and create a new class. Instead of hard-coding String class as the only type the class can work with, we make the class type a parameter to the class. To store integers, we need to create a new class. MyList can be used to store a list of Strings only. Why do we need Generics? Can you give an example of how Generics make a program more flexible? Generics are used to create Generic Classes and Generic methods which can work with different Types(Classes).Ģ. Java Generics Interview Questions for freshers experienced :.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |