thevan
thevan

Reputation: 10364

Error "an explicit conversion exists (are you missing a cast )" comes when creating an object:

I am interested in learning OOPs concepts. While trying a simple program using Inheritance. I have noticed this error. I can't understand why this error occur? I have given that simple c# code below:

class Animal
{
    public void Body()
    {
        Console.WriteLine("Animal");
    }
}
class Dog : Animal
{
    public void Activity()
    {
        Console.WriteLine("Dog Activity");
    }
}

class Pomeranian : Dog
{
    static void Main(string[] args)
    {
        //Dog D = new Dog();
        Dog D = new Pomeranian();    -----> No error occur at this line
        Pomeranian P = new Dog();    -----> Error occur at this line
        D.Body();
        D.Activity();
        Console.ReadLine();
    }             
}

Any one please tell me what is actually happening there...

Upvotes: 4

Views: 12523

Answers (4)

mason
mason

Reputation: 32728

A Pomeranian is a Dog, but a Dog is not necessarily a Pomeranian. That's true in real life, and true when your object oriented classes properly inherit from each other. In real life, you do a DNA test on a dog to verify that it's a Pomeranian. In the .NET Framework, we have some functions and operators that assist us with this.

Let's say you know for sure that your instance of a Dog is actually a Pomeranian. You can perform an explicit cast.

Dog dog = new Pomeranian();
Pomeranian pomeranian = (Pomeranian)dog; //explicit cast. This works. Would throw an InvalidCastException if the dog isn't a Pomeranian

You want to be positive that the instance is actually of the type you're casting to, to avoid exceptions. One way to do that is making use of GetType() and typeof().

Dog dog = new Pomeranian();

if(dog.GetType() == typeof(Pomeranian))
{
    Pomeranian p = (Pomeranian)dog; //explicit cast won't throw exception because we verified what we're doing is valid.
}

Another way is to use the as operator.

Dog dog = new Pomeranian();
Pomeranian pomeranian = dog as Pomeranian; //If the dog isn't actually a Pomeranian, the pomeranian would be null.

Using the as operator is basically equivalent to this...

Dog dog = new Pomeranian();
Pomeranian pomeranian = null;

if(dog.GetType() == typeof(Pomeranian))
{
    pomeranian = (Pomeranian)dog;
}

Obviously, when all your code is the same block it's easy to tell the underlying type by eyeballing it. But when you start using generic collections and other stuff like that and you're passing them between classes, sometimes you don't know what the type is and that's when checking the type becomes important before casting.

See Casting and Type Conversions on MSDN.

Upvotes: 2

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73502

You have to understand the concept Every Dog is an Animal, but not all Animals are Dogs.

Program is a terrible name, let's get rid of that and make it Pomeranian: now everthing will be clear.

Pomeranian P = new Dog();//This is not valid because not all dogs are Pomeranian.

but you can do the following

Dog d = new Pomeranian();//You know why this works :)

I hope this helps.

Upvotes: 9

Syed Shoaib Abidi
Syed Shoaib Abidi

Reputation: 2366

You cannot create an instance of parent from the type child . It violates the concept of inheritance .

Upvotes: 2

Daniel A. White
Daniel A. White

Reputation: 191058

You can't get a Program from a Dog since Program subclasses Dog.

But with polymorphism, a Program is a Dog, but not the other way around.

Upvotes: 1

Related Questions