I got next question at Java interview:
Could you name some generic class examples from real life? Not from real PROGRAMMING life, but in common. E.g. you are explaining generics to person, who doesn't know anything about programming.
Same thing about the most easy design pattern - singleton.
I have know any idea, about first question (about generics), I tried to name different hand tools (like fretsaw), which work with only one type of material, but it is not generics really.
Please, avoid collection-like suggestions, it is too naive and obvious.
A Generic class simply means that the items or functions in that class can be generalized with the parameter(example T) to specify that we can add any type as a parameter in place of T like Integer, Character, String, Double or any other user-defined type.
In a nutshell, generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs.
Generic is a class which allows the user to define classes and methods with the placeholder. Generics were added to version 2.0 of the C# language. The basic idea behind using Generic is to allow type (Integer, String, … etc and user-defined types) to be a parameter to methods, classes, and interfaces.
Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.
I want to open a pet shop, I don't care what different type of pets they are but dammit I want a pet shop. I want kittens, tortoises and spiders. I want to be able to count them up, find how many animals I've got and order them as such. I don't want to have to cast the fact my tortoise is a tortoise, I know it's a tortoise.
There is only one shop bank account, I will never have more than one, where ever someone orders from me, either online or by post, there is only ever one shop bank account. There can never be another no matter where my shop is accessed from.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With