Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real life generic class examples [closed]

I got next question at Java interview:

  1. 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.

  2. 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.

like image 571
bsiamionau Avatar asked Mar 18 '13 12:03

bsiamionau


People also ask

What is generic class with example?

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.

What are generic classes used for?

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.

What is generics in C# Real Time example?

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.

What is generics in Java with simple example?

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.


1 Answers

  1. 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.

  2. 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.

like image 188
david99world Avatar answered Oct 20 '22 03:10

david99world