A question about Generic Class.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    A question about Generic Class.

    Now a days i am looking at Generics. Which is very interesting.
    But i have a confusion .......

    [code=java]
    class GenericClass<T> {
    T obj;
    GenericClass(T t){
    this.obj = t;
    }
    }

    class MyMainClass{
    MyMainClass(){
    GenericClass<Nu mber> gc = new GenericClass<In teger>();
    }
    }[/code]

    Now my question is that, why it shows me error .....Compiler says that incompatible types.
    But what i think as Integer extends Number .... why it's not valid please explain me!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You should've used a bounded wildcard '? extends Number' for the class type
    parameter and you don't have a no-arg constructor.

    kind regards,

    Jos

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by JosAH
      You should've used a bounded wildcard '? extends Number' for the class type
      parameter and you don't have a no-arg constructor.

      kind regards,

      Jos
      Thanks....I know i have to use bounded wildcard,But i need the clarification on it.
      Actually I posted wrong.
      And i think you didn't get my point.

      [code=java]
      class GenericClass<T> {
      T obj;
      GenericClass(T obj){
      this.obj = obj;
      }
      T getObj(){return obj;}
      [/code]

      [code=java]
      GenericClass<Nu mber> gc = new GenericClass<In teger>(100);
      Number num = gc.getObj();
      [/code]

      Why it's not valid?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by dmjpro
        Thanks....I know i have to use bounded wildcard,But i need the clarification on it.
        Actually I posted wrong.
        And i think you didn't get my point.

        [code=java]
        class GenericClass<T> {
        T obj;
        GenericClass(T obj){
        this.obj = obj;
        }
        T getObj(){return obj;}
        [/code]

        [code=java]
        GenericClass<Nu mber> gc = new GenericClass<In teger>(100);
        Number num = gc.getObj();
        [/code]

        Why it's not valid?
        No need to worry; I got your point. Think of three other classes:

        [code=java]
        class MeansOfTranspor tation { }
        class Car extends MeansOfTranspor tation { }
        class Submarine extends MeansOfTranspor tation { }
        [/code]

        and define a garage like this:

        Code:
        Garage<MeansOfTransportation> garage= new Garage<Car>();
        The garage itself can store submarines, but a Garage<Car> can't. That's why
        your example isn't valid. btw, I thought you were working on inner classes
        and security policies or do you just hop around randomly?

        kind regards,

        Jos

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Originally posted by JosAH
          No need to worry; I got your point. Think of three other classes:

          [code=java]
          class MeansOfTranspor tation { }
          class Car extends MeansOfTranspor tation { }
          class Submarine extends MeansOfTranspor tation { }
          [/code]

          and define a garage like this:

          Code:
          Garage<MeansOfTransportation> garage= new Garage<Car>();
          The garage itself can store submarines, but a Garage<Car> can't. That's why
          your example isn't valid. btw, I thought you were working on inner classes
          and security policies or do you just hop around randomly?

          kind regards,

          Jos

          lolz! Thanks Josh.
          Yeah you are right ... i was learning Inner Class and Java Policies.
          Inner class is so far clear but how java.policy works .... Have a look at my java policy thread..please!

          Actually what I thought that
          [code=java]
          GenericClass<Nu mber> gc = new GenericClass<In teger>(100);
          Number num = gc.getObject();
          [/code]
          It is type safety that's why i told due to some reasons the concepts got out.
          Anyway I got your point of view.
          Thanks.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by dmjpro
            Anyway I got your point of view.
            Thanks.
            That isn't my point of view; that is how the Java language is defined.

            kind regards,

            Jos

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              Originally posted by JosAH
              That isn't my point of view; that is how the Java language is defined.

              kind regards,

              Jos
              Everything is related to reality and you everytime make understand with real example.
              And where is my policy answer .... ? :-)

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by dmjpro
                Everything is related to reality and you everytime make understand with real example.
                And where is my policy answer .... ? :-)
                I don't know; I didn't participate in that thread and I'm sure that r035198x is very
                well capable to answer any reasonable question.

                kind regards,

                Jos

                Comment

                Working...