4

I have interface Car:

public interface Car {
    void run();
}

Also i have a base class that implements this interface:

public class Base implements Car {
    @Override
    public void run() {
        //some implementation here
    }
}

Also i have one more class that must implement Car interface So why i can not do something like that?

public class Der extends Base implements Car  {
   //no implementation of interface here because Base class already implements it
}

Why my base class can not implement interface? What is internal reason of this?

3
  • 2
    It's implements, not implement. And extends must be before implements. So: public class Der extends Base implements Car Commented Jun 12, 2014 at 6:57
  • extends must come before implements, for example, public class Der extends Base implements Car {, but has already been pointed out, that would the same as using public class Der extends Base { Commented Jun 12, 2014 at 6:58
  • Your Base class is implementing the interface Car Commented Jun 12, 2014 at 6:59

7 Answers 7

4

When expending from Base, your Der will of course have the run method because it implements the Car. So

public class Der extends Base {

}

is sufficient

If you need to override run, you can easily do that. The code would look like

public class Der extends Base {

   @Override
   public void run() {
      //do whatever here
   }
}

If you need to use Der wherever Car will be used, you can of course do that.

Finally if you need Car to also implement some other interface the syntax would be

public class Der extends Base implements SomeInterface{

}
Sign up to request clarification or add additional context in comments.

Comments

1

If your Base class has implemented Car Interface and this base class if extended by Der Class then there is no need to again implement Car interface in Der class.

like if

Class Base implements Car{

}

and then

Class Der extends Base{

}

Then all the methods in the interface implicitly access in your Der class.

Comments

1
public class Der implements Car extends Base {
   //no implementation of interface here because Base class already implements it
}

you must use extends keyword before implements keywork

right :

public class Der extends Base implements Car {
       //no implementation of interface here because Base class already implements it
    }

Comments

0

Base class (super class of Der) already implements Car interface, hence, there is no need to implement it explicitly.

Comments

0

When extending Base your Der class will have access to the run method and will actually be able to be considered as of type Car

ie :

Car a = new Der(); 

Will be totally valid.

Comments

0

That is why there is no multiple inheritance in java.

see if you provide implementation for run method then what about overriden method of Base Class

so for redundancy its not allowed.

Comments

0

Implements keyword should come after Extends keyword in your example, like below:

    public class Der extends Base implements Car  {
          //no implementation of interface here because
          // Base class already implements it
    }

No issues if you do the extends and implements in one class, even extending the class i.e. Base which had already implemented same interface i.e. Car.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.