Oleksandr Papchenko
Oleksandr Papchenko

Reputation: 2211

Implement interface by inherited Class in Java

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?

Upvotes: 4

Views: 150

Answers (7)

TrungTran05T3
TrungTran05T3

Reputation: 238

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
    }

Upvotes: 1

Ranjith
Ranjith

Reputation: 61

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.

Upvotes: 0

geoand
geoand

Reputation: 64011

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{

}

Upvotes: 4

rachana
rachana

Reputation: 3414

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.

Upvotes: 1

Sanjay Rabari
Sanjay Rabari

Reputation: 2081

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.

Upvotes: 0

benzonico
benzonico

Reputation: 10833

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.

Upvotes: 0

Darshan Mehta
Darshan Mehta

Reputation: 30819

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

Upvotes: 0

Related Questions