method overidden problem .....

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

    method overidden problem .....

    class A
    {
    void test(int a)
    {
    System.out.prin tln("Normal .... Class A");
    }
    }

    class B extends A
    {
    int test(int a)
    {
    System.out.prin tln("Class B");
    }
    }
    i have this code .. a comiler error comes .... here in class B the method can't be overidden ..... why this happens

    i think .. it is due to method signature mismatching or anything else ... plz explain and if possible plz explain me in details method signature .......


    any help is most welcome ....
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by dmjpro
    i have this code .. a comiler error comes .... here in class B the method can't be overidden ..... why this happens

    i think .. it is due to method signature mismatching or anything else ... plz explain and if possible plz explain me in details method signature .......


    any help is most welcome ....
    DJ, if you do get time please read the classes here.

    You cannot override on return types except in 1.5 with co-variant return types. In that code you have different return types and one is not a subclass of the other so that won't work.

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      thanx for ur reply .....

      but i think tell me ... what is method signature and how it is important in mothod override ....

      thanxxxx

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        thanx for ur reply .....

        but tell me ... what is method signature and how it is important in mothod override ....

        thanxxxx

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by dmjpro
          thanx for ur reply .....

          but tell me ... what is method signature and how it is important in mothod override ....

          thanxxxx
          signature is a combination of
          method name,
          method arguments,
          types of arguments,
          order of types of arguments
          and number of arguments.

          If two methods have exactly the same of these then the two methods have the same signature.

          You can only override a method with one with the same signature and the same or covariant return types.

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            right now i m clear .... but plz tell me one more thing .....


            subsignature ....... thanxxx

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by dmjpro
              right now i m clear .... but plz tell me one more thing .....


              subsignature ....... thanxxx
              From the JLS

              "The signature of a method m1 is a subsignature of the signature of a method m2 if either

              • m2 has the same signature as m1, or
              • the signature of m1 is the same as the erasure of the signature of m2."

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                would u be more clear ... beacause i also saw the line but not understood .....

                thanxxxx

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by dmjpro
                  would u be more clear ... beacause i also saw the line but not understood .....

                  thanxxxx
                  Have you done any generics yet, DJ?

                  Comment

                  • dmjpro
                    Top Contributor
                    • Jan 2007
                    • 2476

                    #10
                    without doing generics can't it be understood???

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by dmjpro
                      without doing generics can't it be understood???
                      Unfortunately.
                      There are not too difficult to understand
                      Here is a simple tutorial for them.

                      Comment

                      Working...