Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
hello all,

C#
Class a{}
Class b:clsss a()
{}
Class c:class b()
{}

So can you explain
C#
C objc=new a()

Thanks to all
Posted
Updated 29-Aug-11 21:18pm
v2
Comments
Uday P.Singh 30-Aug-11 3:19am    
what is your question?
[no name] 30-Aug-11 3:25am    
how it happens how can we create a object of c and initialize with a

Derived class can be converted to Base class, not otherwise.
Your statement is wrong.
It can be a obj=new c()
 
Share this answer
 
Comments
RaisKazi 30-Aug-11 3:40am    
My 5. Agree.
Uday P.Singh 30-Aug-11 3:43am    
correct my 5!
Sorting your example out so it would compile gives:
C#
class A
  {
  }
class B : A
  {
  }
class C : B
  {
  }
...
  C objc = new A();
It will still not compile: you will get an error "Cannot implicitly convert type 'MyClass.A' to 'MyClass.C'. An explicit conversion exists (are you missing a cast?)"
Because C is derived from B, and B is derived from A, each item of type C contains everything that an item of type B does, and B contains everything that class A does. What that means is that an item of class B can hold an instance of Class B or anything derived from class b. It cannot hold an instance of class A, because class A does not know anything about any additions made to create class B. If the compiler allowed you to set an instance of calss A into a class C reference variable, your program would fail at run time when you tried to use any of the class C features, because the object that the variable was referring to did not contain them.

It's a bit like saying "A textbook (class B) is a type of book (class A), and a Physics textbook (class C) is a type of textbook (class B), so if I buy a book (class A), then it will contain information on Physics, rather than poetry"
 
Share this answer
 
Comments
RaisKazi 30-Aug-11 3:38am    
My 5+. This is the most perfect Answer for me. OP's code will definately throw error - "Cannot implicitly convert type 'a' to 'c'. An explicit conversion exists (are you missing a cast?)", Op should learn Multilevel Inheritance.
Uday P.Singh 30-Aug-11 3:43am    
correct my 5!
C objc=new a()
This code will not complile.

Class A is the parent of C due to inheritance hierarchy - C is a child of B and B is a child of A.
Thus A objC = new C(); will complile without any issues.

Think of it this way, subclasses can provide more than base classes, so you can easily typecast a derived object into the base class but not the other way round.
 
Share this answer
 
v5
Comments
[no name] 30-Aug-11 3:30am    
thanks
Abhinav S 30-Aug-11 3:39am    
I've given you the solution the other way round.
My solution is not correct.

Your code will not compile.

A objC = new C() will compile.
RaisKazi 30-Aug-11 3:39am    
No Abhinav, This code will trhow an error - "Cannot implicitly convert type 'a' to 'c'. An explicit conversion exists (are you missing a cast?)"
Abhinav S 30-Aug-11 3:41am    
I got this the other way round initially.
I've modified my answer.
RaisKazi 30-Aug-11 3:43am    
Yes now Agree. My 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900