.. a little more on casting, because my original statement was unclear...
Class inheritance of objects is core to object oriented programming... for example, you might have a class definition that describes a "Fruit Object"... attributes may be "sweetness" and "color", lets just say. Things you can do to a fruit is "package it" and "ship it". Then you might have an "Apple Object" that inherits the Fruit class. The apple object may have attributes like "roundness" and "tree height"... and things you can do like "eat it" or "cut it". Maybe you cannot "peel it", but other objects that inherit the fruit class can. why do it like this? For efficiency and reuse of code. Very similar to how database schemas are designed for normalization (not repeating the same data over and over again, but relating it to another table that has that data stored in one row, instead of millions, and then reference that one row)... digressing... complex topic.
anyway, lets say your program is passed an Apple object that you want to ship... well, any fruit object can be shipped, all you have to do is Cast the apple UP to the fruit object. The programming interpreter (compiler, runtime engine, whatever) checks to see if the Apple inherits Fruit, and if it does, it can act on the object as a Fruit and perform the "ship it" routine.
Conversely, if you get some data that is a Fruit, but you don;t know what type of fruit, you can Cast it DOWN to the Apple and the Cast function will return a success or failure, indicating if the Fruit you have is of the class Apple.
You can technically use Cast on primitive variables (basic things like numbers and strings - which you can think of as a class, incorrectly... they are primitives, but can be viewed as a simple class of a single element. So you can try to Cast an Integer to a Float, and it might work... if the language is smart enough to get what you are really trying to do... but we don't call this Casting... we call this Converting.
Class inheritance of objects is core to object oriented programming... for example, you might have a class definition that describes a "Fruit Object"... attributes may be "sweetness" and "color", lets just say. Things you can do to a fruit is "package it" and "ship it". Then you might have an "Apple Object" that inherits the Fruit class. The apple object may have attributes like "roundness" and "tree height"... and things you can do like "eat it" or "cut it". Maybe you cannot "peel it", but other objects that inherit the fruit class can. why do it like this? For efficiency and reuse of code. Very similar to how database schemas are designed for normalization (not repeating the same data over and over again, but relating it to another table that has that data stored in one row, instead of millions, and then reference that one row)... digressing... complex topic.
anyway, lets say your program is passed an Apple object that you want to ship... well, any fruit object can be shipped, all you have to do is Cast the apple UP to the fruit object. The programming interpreter (compiler, runtime engine, whatever) checks to see if the Apple inherits Fruit, and if it does, it can act on the object as a Fruit and perform the "ship it" routine.
Conversely, if you get some data that is a Fruit, but you don;t know what type of fruit, you can Cast it DOWN to the Apple and the Cast function will return a success or failure, indicating if the Fruit you have is of the class Apple.
You can technically use Cast on primitive variables (basic things like numbers and strings - which you can think of as a class, incorrectly... they are primitives, but can be viewed as a simple class of a single element. So you can try to Cast an Integer to a Float, and it might work... if the language is smart enough to get what you are really trying to do... but we don't call this Casting... we call this Converting.