0

I'm trying to enable a class to inherit all of its data members from other superclasses, but without inheriting unnecessary data members that are only relevant to other classes.

Since I can't inherit from two classes, I figured I'd use interfaces. However, all interface variables are public, static, and final; essentially constants not instance variables.

Any ideas to overcome this problem?

Additional information:

So let's say I need variables x, y, and z to exist in class E.

Variable x will be used by all subclasses of superclass A. Variable y will be used by some subclasses of superclass A. Variable z will also be used by some subclasses of superclass A.

Abstract class B inherits from abstract class A. B contains the variable y. Abstract class C inherits from abstract class A. C contains the variable z. Class D needs variables x and y, it inherits from abstract class B and all is well. Class E needs all the variables x, y, and z. But it cannot inherit from both classes.

How would I get class E to obtain all the variables?

user2860068
  • 1
  • 1
  • 2
  • Show a code example of what you are trying to achieve – Prateek Oct 08 '13 at 19:44
  • 1
    If only *some* of the members of a class would be relevant, that sounds like you've got a design problem there to start with. – Jon Skeet Oct 08 '13 at 19:45
  • 2
    _I'm trying to enable a class to inherit all of its data members from other superclasses, but without inheriting unnecessary data members that are only relevant to other classes._ That would break encapsulation and object orientation. – Sotirios Delimanolis Oct 08 '13 at 19:45
  • Make protected only the relevant data members, and the rest private – Spiff Oct 08 '13 at 19:47
  • _inherit all of its data members from other superclasses, but without inheriting unnecessary data members that are only relevant to other classes_ how can you even imagine this ? how will you separate needed data members from other ones ? – Eugen Halca Oct 08 '13 at 19:48
  • 1
    Interface describes behaviour not state. What you want to do is to redesign your class and separate in other having they own state, then by composition and then you inject to your new classes. – nachokk Oct 08 '13 at 19:55

2 Answers2

0

Depending on what you're actually trying to do you could use multiple interfaces with getters/setters, obviously your implementing class will still need to implement all the data members. You could also keep a reference to one (or more) classes which might be what you actually need.

I'm not sure what you're trying exactly though, but usually (or more like always) if you want to extend a class and you figure you don't need some of the data-members of the class you extend, it means you're doing something wrong.

Alowaniak
  • 571
  • 3
  • 10
0

Prefer composition to inheritance. Instead of trying to have one class that is derived from multiple base classes, have a clase that contains, or is associated with, an obje t of each if those classes.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • See also http://stackoverflow.com/questions/2238642/is-there-anything-composition-cannot-accomplish-that-inheritance-can – Raedwald Oct 09 '13 at 07:39