Is it possible to force a method to be extended in Java?

Asked

Viewed 79 times

4

It is possible to "force" a method to be extensible?

The statement of the exercise says exactly this: we have to create a class, within it create a method and force this method to be extended. From what I understood of the things I read, all methods could be extensible, each with its own peculiarities, but the public is the one that can be extended with the greatest freedom, but that does not mean that it is necessarily extensible, or not?

There is a way to "force" the method to be extensible in Java?

  • Helps immensely!

1 answer

4


The public is not the one that can be extended with more freedom, there is no difference in this aspect regarding the visibility of the method.

All methods are extensible by default in Java, so you don’t have to do anything to force it, only n]ao can put a final in it because therein prohibits it to be extensible.

To force a method to be outstretched has to make it not have implementation, so it has to mark it as abstract, which will force the class to be abstract as well. You can’t force it any other way than by mechanisms external to language.

Something like that:

abstract class CalculadoraBase {
    public abstract int Soma(int x, int y);
}

I put in the Github for future reference.

  • Oh yes, I kept thinking about the access modifiers, and it didn’t even cross my mind about this possibility of the abstract class. I will read a little more about. Thanks, my dear!

  • I’m sorry, I don’t get it, like voting for everything?

  • You can vote on every website, but I think you’ve got it, so I understand you’ve already voted on the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.