1 2 3 4 Previous Next 54 Replies Latest reply: Jul 10, 2008 2:49 PM by 807589 RSS

    Selective Inheritance

    509689
      Hi, My problem is a design problem. Any help would be appreciated.

      I am given an Interface which extends a value object. The interface contains many methods, static final variables, instance variable, etc. The requirement is that neither the given interface or value object can be changed.
      I need to create a new object which exposes many of the members of the interface, but keeps others unexposed, and implements or declares new methods and variables as well. I considered a wrapper class, however had several problems as well as cumbersome, and cannot have class final variables initialized. I would like some ideas on a re-design. following is an example.

      public Class GivenObject{
      /**
      * public static final a = "A"
      * public static final a = "B"
      * private void doSomething1(){}
      * public boolean doSomething2(){}
      * public boolean doSomething3(){}
      */ plus alot of otherstuff....
      }

      public interface IgivenVO extends GivenObject
      {
      /**
      * public static final a = "A"
      * public static final a = "B"
      * private void doSomething1(){}
      * public boolean doSomething2(){}
      * public boolean doSomething3(){}
      */ plus alot of otherstuff....
      }

      /* the above interface and Class cannot be changed*/
      /* now in my class I want to expose only those items I have included*/
      public Class MyClass{
      /**
      * public static final a = "A"
      * public boolean doSomething3(){}
      * public/private custom methods
      */ plus some otherstuff....
      }

      }
        • 1. Re: Selective Inheritance
          807589
          I am given an Interface which extends a value object.
          Stop right there. Interfaces in Java may only extend other interfaces; it is impossible to extend a class with an interface. Period.

          ~
          • 2. Re: Selective Inheritance
            800472
            Given your requirements, I think the only thing you can do is provide a wrapper. This isn't uncommon. What do you find cumbersome about it?

            Also your description makes no sense. You don't extend objects. You extend classes, but even so, interfaces don't extend classes. I assume you meant to say that you have a class which implements an interface.
            • 3. Re: Selective Inheritance
              509689
              Ok, My mistake....the VO is actually another interface, but lets just forget the VO all together. We know that I am only needing to use the interface defined, so lets talk about the analogy considering only this given interface.
              • 4. Re: Selective Inheritance
                807589
                EMSDeveloper wrote:
                We know that I am only needing to use the interface defined, so lets talk about the analogy considering only this given interface.
                But the premise is shaky from the outset. You shouldn't extend an interface to hide certain methods of that interface; that's a huge design smell. You should prefer composition to inheritance in such a case.

                ~
                • 5. Re: Selective Inheritance
                  509689
                  Ok. Please define composition in terms of code.
                  • 6. Re: Selective Inheritance
                    807589
                    EMSDeveloper wrote:
                    Ok. Please define composition in terms of code.
                    [http://en.wikipedia.org/wiki/Object_composition]

                    <quote>
                    Composition is contrasted with subtyping, which is the process of adding detail to a general data type to create a more specific data type. In composition, the composite type "has an" object of a simpler type, while in subtyping, the subtype "is an" instance of its parent type. Composition does not form a subtype but a new type.

                    A real-world example of composition may be seen in an automobile: the objects wheel, steering wheel, seat, gearbox and engine may have no functionality by themselves, but if you compose them, they may form an automobile object, which has a higher function, greater than the sum of its parts in a trite sense.
                    </quote>
                    • 7. Re: Selective Inheritance
                      509689
                      My understanding of composition is similar the the method of using a wrapper. I have posted more code below. The problem I was have is that I wanted to somehow inherit all of the final statics from the interface, which was causing problems. If I am understanding you correctly, I should just scrap the entire idea of inheriting a car from a truck, and just build a car from ground-up with parts from the warehouse?.?.?.



                      public interface IgivenVO extends GivenObject
                      {

                      public static final a = "A"
                      public static final a = "B"
                      private void doSomething1(){}
                      public boolean doSomething2(){}
                      public boolean doSomething3(){}
                      plus alot of otherstuff....
                      }

                      /* the above interface and Class cannot be changed*/
                      /* now in my class I want to expose only those items I have included*/
                      public Class MyClass{

                      IgivenVO givenVO;

                      MyClass(IgivenVO givenVO){
                      this.givenVO = givenVO;
                      }

                      public static void main(String args[]){
                      MyClass myClass = new MyClass(new IgivenVO);
                      }
                      }
                      • 8. Re: Selective Inheritance
                        807589
                        Why do you want to inherit final statics?
                        • 9. Re: Selective Inheritance
                          509689
                          I took a look at the definition you posted. What I am trying to do in perspective is this. I have a car....How do I extract only the engine from the car?
                          • 10. Re: Selective Inheritance
                            807589
                            The problem I was have is that I wanted to somehow inherit all of the final statics from the interface
                            That is indeed a problem. That isn't a good reason to extend a class. It's an outright bad reason if that's the only reason you're extending a class.
                            If I am understanding you correctly, I should just scrap the entire idea of inheriting a car from a truck
                            Yes, you should scrap that idea. A car is not a truck (unless it's an [El Camino|http://en.wikipedia.org/wiki/Chevrolet_El_Camino] - haha). A Car is a MotorVehicle. A Truck is a MotorVehicle.

                            ~
                            • 11. Re: Selective Inheritance
                              509689
                              I need to do decomosition
                              • 12. Re: Selective Inheritance
                                807589
                                EMSDeveloper wrote:
                                I took a look at the definition you posted. What I am trying to do in perspective is this. I have a car....How do I extract only the engine from the car?
                                class Car {
                                    public Engine removeEngine() {
                                        // ... your code here ...
                                    }
                                }
                                ~
                                • 13. Re: Selective Inheritance
                                  807589
                                  I need to do decomosition
                                  I don't think you have a good understanding of what you do or don't need at this point... :o)

                                  ~
                                  • 14. Re: Selective Inheritance
                                    807589
                                    Careful! I drive a hybrid.
                                    1 2 3 4 Previous Next