Can you subclass an interface in java




















Problem is, when I need to use my Reload method one of four things will eventually happen:. Each of these "solutions" has unique problems.

For example, solution 3 introduces more code just to access a method of a specific type, while solution 4 means I can only access Reload ; and not the methods inherited from Weapon. When subclasses begin to implement interfaces , is it an indication that you need to rethink your design?

There is no "right" solution, this is fully context dependend. In an ideal design, you start with. However, if you have to put a weapon into a collection of Weapon objects from a generic lib you cannot change easily, and then you need to retrieve objects from that collection at a different place in your program which deals with ReloadableWeapon objects again, then you might run into the situation where you cannot easily avoid a cast to the other type.

So in short, try to distribute the responsibilities in your code so you have one part dealing only with weapons, one part with reloadables, and one part with reloadable weapons, so you can avoid most downcasts. What I would like to point, is that by respect of the Liskov substitution principle : "objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.

For example, a Visitor could have different implementations for Dogs and Cats siblings of their parent Animal class. If you create ReloadableWeapon to extend Weapon, you should not have code that knows if something is Reloadable or not, or it means your extension of Weapon breaks the correctness of your uses of a generic weapon. In a complex system it is understandable that you may have to dynamically perform an action without statically knowing whether this object supports the action. In my experience, it is best to ask the object — not by casting, but by guarding access to the method.

For example:. This is similar in effect to a downcast but is a bit more typesafe. In particular, you can now have different kinds of weapons that are reloadable without having to inherit from Reloadable. This composability also allows you to handle weapons that support a combination of multiple interfaces, without having to create a new class for each combination: the main Weapon class is often sufficient if you supply the available actions through the constructors.

This solution is the exact opposite of using a Visitor. With the visitor you cannot add arbitrary new classes but can add more actions through visitors. This is because the visitor interface describes which classes are supported. But here we have added a new method to the Weapon interface. So we cannot add arbitrary new actions, but we can create more subclasses that implement these actions.

Whether a visitor or these method objects are more appropriate depends on how you expect to extend your code in the future: is it more likely to add new actions then prefer a visitor or more likely to add new implementations then prefer method objects?

Sign up to join this community. Java Interfaces vs. Abstract Classes. The blue class knows only the interface. The abstract class implements the interface, and the subclass inherits from the abstract class. Tweet Jakob Jenkov.

Featured Videos Sponsored Ads. All Trails. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Skip to content. Change Language. Related Articles. Table of Contents. Save Article. Improve Article. If you have nothing to contribute, please stay OFF this site. Thank you for your contribution! This post helped me to understand the output of the program that I wrote.

Show 2 more comments. Active Oldest Votes. Improve this answer. Okay, so it does work. I'm gonna add this the original. So to use another example, let's say I have the abstract class animal. Therefore all subclasses of "Dog" also implement the interface "Pet" without having to individually implement pet, right? Yes, in that case all subclasses of Dog will have the additional interface basetype Pets. The methods you declare in Pets will then have to be implemented in Dog , since Dog I assume from your example is not abstract.

If you want other Pets Dogs, such as Shepherd or Chihuahua to behave differently than Dog as Pets, then you can simply override those methods in each of the Shepherd and Chihuahua classes.

They are still Pets though, no matter what you do. I'm trying to think of scenarios where this will be useful, using interfaces instead of inheritance.

There are lots, and lots and lots! Add a comment. Jeroen Vannevel Jeroen Vannevel



0コメント

  • 1000 / 1000