plz explain this ?????

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

    plz explain this ?????

    how can i explain this .........

    class S1
    {
    static void Static()
    {
    System.out.prin tln("Here i m ...");
    }
    }

    class S2 extends S1
    {
    void test()
    {
    Static();
    }
    }

    class StaticTest1
    {
    public static void main(String args[])
    {
    S2 s = new S2();
    s.test();
    }
    }


    i think u know the output ..... here i m

    how can i explain thisss .... plz help

    thnaxxxx
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Well, how would you explain it? What do you know is happening (hint, you already told us with the output)?

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by dmjpro
      how can i explain this .........

      class S1
      {
      static void Static()
      {
      System.out.prin tln("Here i m ...");
      }
      }

      class S2 extends S1
      {
      void test()
      {
      Static();
      }
      }

      class StaticTest1
      {
      public static void main(String args[])
      {
      S2 s = new S2();
      s.test();
      }
      }

      i think u know the output ..... here i m

      how can i explain thisss .... plz help

      thnaxxxx
      Well, wouldn't you expect that output DJ? You did call the method called static which prints that message.

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        actually what i understood ..... that is

        the static method never in herited so how the class S2 knows Static

        and how the static method resolved by compiler and what compiler tells the JVM to do???????

        plz clearify me in details ..... thanxxx

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          Originally posted by dmjpro
          actually what i understood ..... that is

          the static method never in herited so how the class S2 knows Static

          and how the static method resolved by compiler and what compiler tells the JVM to do???????

          plz clearify me in details ..... thanxxx
          What does the call to instantiate S2 do? (What does it call?)

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            JVM call these function but JVM take decision according to compiler ???

            as i previously i knew from this site ...... that the staic method resolved during compile time ... but here i stuck

            i think as the S2 extends S1 then inside test() method the Static() calls internally converted into S1.Static()....

            m i right ??????

            plz help .....

            Comment

            • sicarie
              Recognized Expert Specialist
              • Nov 2006
              • 4677

              #7
              Originally posted by dmjpro
              i think as the S2 extends S1 then inside test() method the Static() calls internally converted into S1.Static()....
              The call to instantiate S2 creates the Static() method which is inherited through the 'extends' keyword, and then is invoked in the StaticTest1.

              I'm not too sure on the internals of this - I do not believe that an S1 object is created separately from S2 (as S2 extends, it is considered a member of the superclass Object, and then S1, and then S2 - technically every S2 is an S1...). I'm sure another expert on here could verify/disprove this...

              Comment

              Working...