GiJeet said:
let's use something I can actually visualize. If I declare a type
Chicken but I refer to a type Eagle I can use what? The methods and
properties of a Chicken or an Eagle?
What is bird?
Chicken bird = new Eagle();
G
A chicken is hardly and eagle, or vice versa...
This makes more sense:
interface ICanFly {}
interface ICanHunt {}
class Bird : ICanFly {}
class Chicken : Bird {}
class Eagle : Bird, ICanHunt {}
Now you can create an eagle object and store the reference in different
kinds of reference variables:
ICanFly eagleFly = new Eagle();
ICanHunt eagleHunt = new Eagle();
Bird eagleBird = new Eagle();
Bird chickenBird = new Chicken();
Chicken chicken = new Chicken();
Eagle eagle = new Eagle();
The eagleFly reference can be used for anything that the ICanFly
interface defines.
The eagleHunt reference can be used for anything that the ICanHunt
interface defines.
The eagleBird reference can be used for anything that the Bird class
defines.
The chickenBird reference can be used for anything that the Bird class
defines.
The chicken reference can be used for anything that the Chicken class
defines.
The eagle reference can be used for anything that the Eagle class defines.
Casting the reference (Eagle)eagleFly means that you can use it for
anything that the Eagle class defines.
Casting the reference (ICanFly)eagle means that you can only use it for
what the ICanFly interface defines.
You can cast any of the references eagleFly, eagleHunt, eagleBird or
eagle to the type ICanHunt, but not the references chickenBird or chicken.