简体   繁体   中英

Properties of child class not visible

In C# 4.0 I have a parent class and a child class, similar to this:

public abstract class Parent()
{
   public Parent() { }

   public void CommonMethod() { /* do something */ }
}

public class Child : Parent
{
   public string PropertyA { get; set; }
   public string PropertyA { get; set; }

   public Child() : base()

}

If I instantiate the following:

Parent obj = new Child();

..I cannot get access to PropertyA and PropertyB . I realise that obj is of type Parent which does no have those members, but how can I gain access to them in a 'clean' way? I could do the following:

Child obj = new Child();

..which would give me access, but I frequently see objects being made that are typed to the parent rather than the child. Why is this so common? Am I going about this the wrong way?

EDIT: I should say that the properties in Child() are not common to all classes that derive from Parent().

If those properties are common for all derived types then you should declare them in the base class.

If they are common for some derived types then put them into an interface and implement that interface from your derived classes.

If they are only specific to Child class then you don't have much choice, either declare your instance as Child not Parent or check the type and cast it appropriately.

I frequently see objects being made that are typed to the parent rather than the child. Why is this so common?

If you don't need it, don't use it. Apparently your Child properties are important; then declare your obj as Child , not Parent .

Inversely, you use the Parent declaration if you actually only care about Parent members.

How can I gain access to them in a 'clean' way?

By casting:

Parent obj = new Child();
((Child)obj).PropertyA = "foo";

But not every Parent has to be a Child .

make declaration of those propertied in your parent class as shown below :-

public abstract class Parent()
{
   public Parent() { }
   public string PropertyA { get; set; }
   public string PropertyA { get; set; }
}

UPDATE:-

I would suggest to transfer all your abstract class code to an interface and declare the properties there and use the interface in child wherever you require and as suggested by other people here if main focus is on child then make instance of child instead of parent .

Interface would be as below :-

public interface IParent
{
   // interface members
       string PropertyA { get; set; }
       string PropertyA { get; set; }
}

you need to change calling your method like below
Parent obj = new Child(); -> is yours

need to be like this `

Parent obj=new Parent();  or Child obj=new Child();

and in your parent class change class like this:

 public string PropertyA { get; set; }


public string PropertyB { get; set; }

in your base class need has with same method.

and derived your class need to be like as parent class this is the second method.

You defined an object from Parent , but new it from Child . So whenever you call any method or properties for obj , the program refer to Parent class to find them, but it can't find those properties in Parent , because you didn't override them.

public abstract class Parent()
{
  public Parent() { }

  public void CommonMethod() { /* do something */ }

  abstract public PropertyA {get; set;}
  abstract public PropertyB {get; set;}
}

public class Child : Parent
{
   override public string PropertyA { get; set; }
   override public string PropertyA { get; set; }

   public Child() : base()

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM