Create array of object with heritage

good morning,

I have several objects that inherit from my father's class "books" and I need to put them all in an array.

creating an array probe of the parent class and it worked but want to access methods in inherited classes is not possible.

thank.

Upvotes: 0

Views: 337

Answers (1)

Jon Hanna
Jon Hanna

Reputation: 113242

  1. As much as possible, avoid having to access the methods in the derived classes anyway. A good design would pass such an array of "books" to methods that dealt with "books", and didn't care that they were actually "textbooks" or that some of their methods were overridden.

  2. If unavoidable, you can do:

    Textbook tb = myBook as Textbook; if(tb != null) tb.TextbookOnlyOperation();

Upvotes: 1

Related Questions