8 Replies Latest reply: Mar 14, 2010 10:55 PM by EJP RSS

    how to inherit through generic

    843793
      Its better to design through composition than through inheritance to avoid compile time binding. In order to do that we use deligation. Now we can also do similar thing using templatized inheritance. Following is an example in c++ for the same:
      //========================================
      #include<iostream>

      using namespace std;

      class A {
      public:
      A(){cout<<"In ctor of A"<<endl;}
      };

      class B {
      public:
      B(){cout<<"In ctor of B"<<endl;}
      };

      template<class T>
      class C : public T {
      public:
      C(){cout<<"In ctor of C"<<endl;}
      };


      int main(void) {
      C<A> ca;
      C<B> cb;
      return 0;
      }
      //======================================

      Now a similar java program with generic fails to compile. Following is the content of generic_inher.java :
      //======================================
      class A {
      public A(){System.out.println("In ctor of A");}
      }

      class B {
      public B(){System.out.println("In ctor of B");}
      }

      class C<T> extends T {
      public C(){System.out.println("In ctor of C");}
      }

      public class generic_inher {
      public static void main(String [] args) {
      C<A> ca = new C<A>();
      C<B> cb = new C<B>();
      }
      }
      //=====================================

      But the above errors out during compilation. Following is the error:
      javac generic_inher.java
      generic_inher.java:10: unexpected type
      found : type parameter T
      required: class
      class C<T> extends T {
      ^
      1 error

      How to do the above in java as shown by C++ ?
        • 1. Re: how to inherit through generic
          843793
          Hello Amitava,

          C++ does parametric type polymorphism (generics) via polyinstantiation, Java doesn't.

          The simple answer is: You can't do that or anything like it in Java.

          With kind regards
          Ben
          • 2. Re: how to inherit through generic
            EJP
            Its better to design through composition than through inheritance to avoid compile time binding.
            Agreed.
            In order to do that we use deligation.
            Delegation.
            Now we can also do similar thing using templatized inheritance.
            No we can't. Using templatized inheritance is not a 'similar thing' to using composition. It is inheritance. And it doesn't avoid compile time binding either.
            • 3. Re: how to inherit through generic
              843793
              Can u explain why it won't avoid compile time binding. We are now in a position to create completely different kind of class. So its as good as composition. I know we should not do like this, since this way we cannot specify the interface of the class we are inheriting, so next time we might inherit some class with completely different interface and will require more changes in derived class. Lets keep this interface issue aside, can't we say that the derived class is specialized by client. lets take an example:
              consider window class is a type of shape and it inherits rectangle for now. Now instead of that we can create a member shape * and deligate the same to shape object.

              class window {
              shape *s;
              window(shape *a) {
              s = a;
              }
              int size(){
              return s->size();
              }
              };

              so we can say that the functionality of size is delegated to shape.
              Now clients can do the following:
              Client 1:
              rectangle r(1,3,4,5);
              window a1 = new window(&r); //this is a rectangle window.

              client 2:
              oval o(2,3);
              window a2 = new window(&o); //this is an oval window.

              Similarly with inheritance same effect can be realized as below:
              Client 1:
              window<rectangle> a1 = new window<rectangle>(); //this is a rectangle window.

              client 2:
              window<oval> a2 = new window<oval>(); //this is an oval window.

              I think u r correct, but it would be nice why you say that delegation is not compile-time binded but the above is compile time binded?
              • 4. Re: how to inherit through generic
                EJP
                Can u explain why it won't avoid compile time binding.
                Can you explain why it will?
                I think u r correct, but it would be nice why you say that delegation is not compile-time binded
                Because it isn't.
                but the above is compile time binded
                Because it is inheritance, and you said yourself above that inheritance entails compile-time binding. QED.
                • 5. Re: how to inherit through generic
                  843793
                  Many thanks for all your replies. Templatized inheritance is available only in C++, and it can help us to change the interface in case clients want to. I will think more, in case such a design can have help in any way. Also compile time binding is a misnomer and I request you to please ignore that error that I wrote at many places. Every think is compile time binding. With composition client of the classes can easily change the things.
                  • 6. Re: how to inherit through generic
                    EJP
                    Every [thing] is compile time binding.
                    No it isn't. If you use composition with private members there is no compile-time binding between classes and their clients.
                    • 7. Re: how to inherit through generic
                      843793
                      Hi ejp, can u please provide one small example for this.
                      • 8. Re: how to inherit through generic
                        EJP
                        You wrote:
                        Its better to design through composition than through inheritance to avoid compile time binding.
                        So why are you asking me to provide an example?