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. The difference is that the inputs to formal parameters are values, while the inputs to type parameters are types.
- From Oracle about Java generics | [reply] |
In a nutshell, generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods.
Technically, a constructor in one class could create an object of another class, but unless the other class was a child class of the constructing class, it would not have access to the methods of the constructor's class.
This would make more sense for a "contains" relationship between the classes. In that case, the constructor method could have a parameter specifying the class of a contained object. It could then call the constructor of that class to create the object and save a reference to that object in a field of it's own object-under-construction.
Code in the container class, when operating on the contained object, would, auto-magically, invoke the methods of the contained object's class. However, code in the contained object's class would not have access to the containing class's methods.
| [reply] |