Casting is the conversion of one type to another type. Typically, casting is used to convert an object reference to either a subtype (for example, casting an Animal reference to a Horse), but casting can also be used on primitive types to convert a larger type to a smaller type, such as from a long to an int.
example:
int i = 5;
short s = (short)i;
------------
We create a superclass called Shape, with a number of subclasses (e.g.
Circle, Square, Triangle, etc).
These classes can be treated as types; you can create instances and store their references in variables, e.g.:
Shape shape = new Shape(); Circle circle = new Circle();
|
However, that's not the end of the story when you use inheritance. You could also do something like this:
Shape shape = new Circle();
0 comments:
Post a Comment