excluding inherited members

  • Thread starter Thread starter eggie5
  • Start date Start date
E

eggie5

Hi,

I have a class Dog which derives from Animal. Suppose my instance of
Dog is bulldog. bulldog has all the members of Animal plus Dog. How
can copy just the members from Dog to a new Dog called boxer? Just get
the Dog members from bulldog and leave out any inherited members from
Animal.

Dog bulldog=new Dog(); <-has inhereited members from Animal

Dob boxer = bulldog - inherited Animal members

How can I do this?
 
eggie5 said:
I have a class Dog which derives from Animal. Suppose my instance of
Dog is bulldog. bulldog has all the members of Animal plus Dog. How
can copy just the members from Dog to a new Dog called boxer? Just get
the Dog members from bulldog and leave out any inherited members from
Animal.

You can't. If you don't want instances of Dog to be able to be treated
as instances of Animal, you shouldn't make Dog derive from Animal in
the first place.

Look up "Liskov's Substitution Principle" for more on this.
 
You can't. If you don't want instances of Dog to be able to be treated
as instances of Animal, you shouldn't make Dog derive from Animal in
the first place.

Look up "Liskov's Substitution Principle" for more on this.

Well, If I can exclude the base class from my derived class from an
outside perspective, I think I can recompile the base class (Animal),
to provide a Value property.

So I would be able to call Dog.Value to get the Dog object without any
of the base class members.

The only question is... how do I implement Animal.Value?

public T Value
{
get
{
//TODO: How do I get the class that is inheriting this?
}
}
 
eggie5 said:
Well, If I can exclude the base class from my derived class from an
outside perspective

You can't. The point of it being derived from the base class is that it
inherits everything from the base class.
I think I can recompile the base class (Animal), to provide a Value
property.

So I would be able to call Dog.Value to get the Dog object without any
of the base class members.

No. If Dog derives from Animal, then every Dog inherits members from
Animal. There's no way round that, and if you don't *want* members to
be inherited, you shouldn't use derivation.
 
yuo can do that but you must do some changes.
Add an interface for dog, cal it IDog, make an object inherited from the
interface but not from the animal.
then, get the type of the instance of dog and animal intersect the
properties (GetType and get proerties of the object).
Fill the properties values on the new dog instance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top