Introduction_Part_10

Introduction_Part_10 - Generics A generic type is a type...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Last Updated: January 5, 2012 CSE 2011 Prof. J. Elder Generics A generic type is a type that is not defined at compilation time. A generic type becomes fully defined only at run time. This allows us to define a class in terms of a set of formal type parameters , that can be used to abstract certain variables. Only when instantiating the object, do we specify the actual type parameters to be used. when instantiated as a variable.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Last Updated: January 5, 2012 CSE 2011 Prof. J. Elder Generics Example /** Creates a coupling between two objects */ public class Couple <A, B> { A obj1 ; B obj2 ; public void set (A o1, B o2) { obj1 = o1; obj2 = o2; } } Camera myCam1 = new DigitalCamera("Nikon","D90”,12000000); Camera myCam2 = new AutoDigitalCamera("Olympus","E30",12000000); Couple<Camera,Camera> stereoCamera = new Couple<Camera,Camera>(); stereoCamera.set(myCam1, myCam2);
Background image of page 2
Last Updated: January 5, 2012 CSE 2011 Prof. J. Elder Generics Example Note that two things are happening here:
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

Introduction_Part_10 - Generics A generic type is a type...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online