Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

In Java, what is the difference between a abstract class and an interface?

0
Posted

In Java, what is the difference between a abstract class and an interface?

0

While you can’t instantiate either one, this is where the similarity ends, and it is probably this reason why people ask it. An abstract class is marked abstract because you want a class that contains methods that can only be used by children classes that extend it (and supposedly add some remaining ingredients to make the class valid). This allows a class to contain code, but can’t be instantiated (for whatever reason you need). An interface, on the other hand, is really an API contract. It contains no code, just the list of methods that any class claiming to implement it needs to create. These two things have such a different semantic meaning that I was surprised the first time I heard it. But it has been asked in every one of my screening interviews. As a design note, if you are building a library, make your users implement an interface instead of extending one of your classes, since Java doesn’t do multiple inheritance, it is mean to use up a user’s one chance at extension.

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123