Simple doubt... Please help

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

    Simple doubt... Please help

    look at this example.....

    [code=java]
    class InterfaceTest1
    {
    void test();
    }
    class ClassTest1
    {
    public void test(){}
    }
    class ClassTest2 extends ClassTest1 implements Interface
    {
    public void test(){} //Method overriden or method implementation?
    }
    [/code]

    How it is posiible ......???

    Please explain.

    Kind regards,
    Dmjpro.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by dmjpro
    look at this example.....

    [code=java]
    class InterfaceTest1
    {
    void test();
    }
    class ClassTest1
    {
    public void test(){}
    }
    class ClassTest2 extends ClassTest1 implements Interface
    {
    public void test(){} //Method overriden or method implementation?
    }
    [/code]

    How it is posiible ......???

    Please explain.

    Kind regards,
    Dmjpro.
    You got your class names and variable names wrong there dj. The method test is overriden of course and also the class satisfies the contract of the interface by implementing the method test as well. Normally this is not done because it's useless. Either you're using an abstract class or an interface. And choice is usually obvious.

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by r035198x
      You got your class names and variable names wrong there dj. The method test is overriden of course and also the class satisfies the contract of the interface by implementing the method test as well. Normally this is not done because it's useless. Either you're using an abstract class or an interface. And choice is usually obvious.

      Actually what happens to me.
      Today I see the Collection interface carefully.
      There I see a method int hashCode() and the class which by default extends Object and implements Collection interface or it's sub-interfaces have to implement the method int hashCode().
      That's why I questioned it.

      Kind regards,
      Dmjpro.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by dmjpro
        Actually what happens to me.
        Today I see the Collection interface carefully.
        There I see a method int hashCode() and the class which by default extends Object and implements Collection interface or it's sub-interfaces have to implement the method int hashCode().
        That's why I questioned it.

        Kind regards,
        Dmjpro.
        It is important for concrete classes to implement it that's why it was included in that interface.

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Originally posted by r035198x
          It is important for concrete classes to implement it that's why it was included in that interface.
          Yup right.
          But Object class itself contains int hashCode() method.

          Kind regards,
          Dmjpro.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by dmjpro
            Actually what happens to me.
            Today I see the Collection interface carefully.
            There I see a method int hashCode() and the class which by default extends Object and implements Collection interface or it's sub-interfaces have to implement the method int hashCode().
            That's why I questioned it.

            Kind regards,
            Dmjpro.
            You don't *have to* implement hashCode() and its compadre equals(), those
            methods are just part of the interface to emphasize that you should reimplement
            them in several occasions. Read the Collection API docs for those two methods.
            And of course, if you don't implement them the interface contract is still fulfilled
            because the Object class implements them both (but not the way some collections
            expect them to be implemented).

            kind regards,

            Jos

            Comment

            Working...