php editor Zimo will take you to explore the use opportunities of abstract classes and interfaces in Java. In Java programming, choosing when to use abstract classes and when to use interfaces is an important decision. This article will answer this common question for you and help you better understand how to choose the appropriate abstract class or interface in different situations.
I vaguely understand the difference between abstraction and interface, but what I simply cannot understand is when to use abstraction and when to use interface. What variables do I need to select an interface abstraction and vice versa? Most of the answers online are meant to show the difference, but even I understand them. I don’t know when is more appropriate.
The general old rule is: stick with interfaces until you are forced to write abstract classes.
interface in java is a non-instantiable type that defines a public interface that other types can
choose to comply with.
, so you do not need to use the word
abstract.
. This is no longer the case, interfaces can now have a default implementation used in
implementing classes unless replaced.
helper methods. These can help with default implementations.
multiple interfaces.
abstract class is just a class that cannot be instantiated. Abstract classes do not need to define any
abstract methods, although you will often want to do so.
,
private,
protected, and package-private.
. Methods
in abstract classes are not implicit abstract and must be marked as such.
ing classes must use
super to call the constructor in its own constructor. Abstract classes can have instance variables of any visibility and they work just like instance variables in any parent class.
one class, which can be an
abstract class or a concrete class. The
abstractness of a class does not change the number of superclasses it is allowed to have.
abstract classes should be the exception rather than the norm. You should use abstract classes if you need to maintain some internal state while also letting subclasses define your own behavior. A good example comes from the java swing gui library:
AbstractButton. abstractbutton
is an abstract parent for things that behave vaguely like buttons in windows, including buttons, toggle buttons, and items in menus. Anyone can subclass
abstractbutton and implement its members to define a button-like display. But
abstractbutton also has to maintain a lot of internal state (much of it from
jcomponent) in order to communicate with the window and the overall swing api.
interface. If you want to provide a default implementation for some of these methods and use a java version released after 2014, use an interface. If you need a default implementation but are still stuck in the 2000s, use abstract classes. Regardless of java version, if you need private state (such as instance variables) or custom initialization (such as constructors), use abstract classes.
Suppose you have an interface, for example:
interface animal { string makenoise(); }And several classes that implement animal:
class cat implements animal { string makenoise() { return "meow"; } } class dog implements animal { string makenoise() { return "woof"; } }Now you can use interfaces as return types, so methods can return
cat or
dog.
string makeanimalmakenoise(int animalid) { animal animal = pretendanimalservice.getdogorcat(animalid); animal.makenoise(); }Abstract class:
Suppose you have an abstract class:
abstract class animal { public string makenoise() { return "whistle"; } abstract public string getname(); }Now, any class that extends it needs to implement the
getname method:
class Cat extends Animal { String getName() { return "Pussy"; } } class Dog extends Animal { String getName() { return "Fido"; } }Both of the above classes implement the required
getname method, but they also have access to the parent class
makenoise method, which can also be overridden if desired.
The above is the detailed content of When to use abstraction and when to use interfaces?. For more information, please follow other related articles on the PHP Chinese website!