-2

am new to c# and am trying to explore the the inheritance concept and faced the below issue; am getting

error at childclass c = new parentclass();

namespace Inheritance
{
class parentclass
{

}

class childclass : parentclass
{ 

}



public class Program
{
    public void Main(string[] args)
    {
        parentclass a = new parentclass();
        parentclass b = new childclass();
        childclass c = new parentclass();
    }
}
}
Karthik
  • 1
  • 1
  • Always include the error messages in the question. And in this case the error message should be quite clear. – Sami Kuhmonen May 01 '17 at 07:42
  • 1
    Possible duplicate of [Why can't reference to child Class object refer to the parent Class object?](http://stackoverflow.com/questions/2145767/why-cant-reference-to-child-class-object-refer-to-the-parent-class-object) – Mohd Ismail Siddiqui May 01 '17 at 07:45
  • 1
    All apples are fruits, but not all fruits are apples. – Abion47 May 01 '17 at 07:52

2 Answers2

2

Try to rename your parent to Car, and your child to Ford. A Ford is a Car, but a Car is not necessarily a Ford!

Look at the colon when inheriting as an "is a" operator...

0

This is the basic OOPS concept that child never hold reference of Parent class object. Only Parent can holds the reference of Child i.e. providing the concept of Generalization and many more other rules.

Child class is super-set for parent class. All human beings are living things but all living things cant be human being.