Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: extending a class and implementing an interface that have the same method

Tags:

Probably the following cannot be done (I am getting a compilation error: "The inherited method A.doSomthing(int) cannot hide the public abstract method in B"):

public class A {     int doSomthing(int x) {         return x;     } }  public interface B {     int doSomthing(int x); }  public class C extends A implements B {      //trying to override doSomthing...      int doSomthing(int x) {         return doSomthingElse(x);     } } 

Assuming I am allowed to change neither A nor B, my question is can I somehow define C in such a way that it will inherit from both A and B (suppose that it is required for some framework that C will be both an instance of A and B).

Or if not, how would you work around this?

Thanks!

like image 371
rapt Avatar asked Oct 13 '11 22:10

rapt


People also ask

Can you extend a class and implement an interface at the same time?

Note: A class can extend a class and can implement any number of interfaces simultaneously.

Can I use both extends and implements together in Java?

Yes, you can. But you need to declare extends before implements : public class DetailActivity extends AppCompatActivity implements Interface1, Interface2 { // ... } Any number of interfaces can be implemented, if more than one then each needs to be separated with a comma.

Are there any differences between extending a class and implementing an interface?

Difference: implements means you are using the elements of a Java Interface in your class. extends means that you are creating a subclass of the base class you are extending. You can only extend one class in your child class, but you can implement as many interfaces as you would like.

Is it possible to implement an interface and inherit a class at the same time within another class?

Classes don't inherit from interfaces, they just implement them. You can extend (inherit from) a single class, but you can implement multiple interfaces.


2 Answers

make the method public

public class C extends A implements B {      //trying to override doSomthing...      public int myMethod(int x) {         return doSomthingElse(x);     } } 

interface methods are always public

or just use composition instead of inheritance

like image 152
ratchet freak Avatar answered Oct 14 '22 11:10

ratchet freak


The method doSomethis() is package-private in class A:

public class A {     int doSomthing(int x) { // this is package-private         return x;     } } 

But it is public in the interface B:

public interface B {     int doSomthing(int x); // this here is public by default } 

Compiler is taking the doSomething() inherited by C from A which is package-private as the implementation of the one in B which is public. That's why it's complaining -

"The inherited method A.doSomthing(int) cannot hide the public abstract method in B"

Because, while overriding a method you can not narrow down the access level of the method.

Solution is easy, in class C -

@Override public int doSomthing(int x) {     // ... } 
like image 22
Bhesh Gurung Avatar answered Oct 14 '22 13:10

Bhesh Gurung