When to use extensions and when to use an interface?

We can extend the class, but we cannot implement the class. We can implement the interface, but we cannot extend the interface.

When should we use extends?

+2


source to share


5 answers


extends

used to extend the base class:

class ClassX extends ClassY {
    ...
}

      

or extending the interface:

interface InterfaceA extends InterfaceB {
    ...
}

      



Note that interfaces cannot implement other interfaces (most likely because they have no implementation).

Java does not impose any naming conventions for classes and interfaces (unlike IFoo

for interfaces in the .NET world) and instead uses the difference between extends

and implements

to indicate the difference between the programmer:

class ClassA extends ClassB implements InterfaceC, InterfaceD {
    ...
}

      

Here you can clearly see that you are building an existing implementation in ClassB

and also implementing methods from two interfaces.

+5


source


It's a matter of use. Interfaces can be used as a contract with your application, and then base classes can be used to extend that interface, so it's not a couple.

Take the injection dependency pattern for example:

First, you write the contract:

     public interface IProductRepository
     {
        IList<T> GetAllProducts();
     }

      

Then you renew the contract with the base class:

     public abstract BaseProductRepository : IProductRepository
     {
         public IList<T> GetAllProducts()
         { //Implementation }
     }

      



You now have the option to extend the base to two or more specific classes:

     public class InternetProductRepository extends BaseProductRepository;
     public class StoreProductRepository extends BaseProductRepository;

      

Hope these small examples clear up the differences between continuation and Implementation. sorry i didn't use java for the example, but everything is OO, so i think you get the point.

Thanks for reading, Geo


I didn't fill out the code for the injection dependency template, but the idea is there, also well documented on the net. Let me know if you have any questions.

+3


source


In fact, you can extend the interface - in the case where you define another interface.

There are many quasi-religious arguments in this question and I doubt there is a correct correct answer, but for what it stands here, I take it all. Use subclassing (i.e. extends

) when your different classes provide the same functionality and have common implementation details. Use an interface implementation to signal that your classes provide specific functionality (as specified in the interface).

Note that the two are not mutually exclusive; in fact, if a superclass implements an interface, then any subclasses will be considered to implement that interface as well.

Java does not have multiple inheritance, so a (sub) class can only have one parent class, and a subclass must be carefully considered to select a suitable parent, if any; a parent choice that only reflects a small amount of a class's ability is likely to end up frustrating later if there are other sensible parent classes. So, for example, having AbstractSQLExecutor

SQL Server and Oracle subclasses makes a lot of sense; but having a parent class FileUtils with some utility methods and then subclassing all over the place to inherit this functionality is a bad idea (in which case you should probably declare the helper methods static, or have a reference to FileUtils, instead).

Also, subclasses bind you to the implementation details (of your parent) more than the implementation of an interface. I would say that in general it is best to implement an interface, at least natively, and only create class hierarchies of classes in the same or similar packages with a clear hierarchical structure.

+3


source


Keyword

extends is used to extend a concrete / abstract class. By expanding, u can either override / inherit from the parent class methods. The class can only extend the class. U can also say interface1 extends intenface2.

To implement the interface, the keyword is used

... In this case, u must define all the methods specified in the interface. A class can only implement an interface.

0


source


As you said. the implement

java keyword is used to implement an interface where it is extends

used to extend the class.

It depends on what you would like to do. Usually you should use an interface when you want to enforce an implementation (like a contract). Like a class abstract

(but with an abstract class, you can have non-abstract methods).

Remember that in java you can only extend one class and implement zero for many interfaces for the implementing class. Unlike C #, where you can extend multiple classes with :

and where C # only uses a symbol :

for both interfaces and classes.

0


source







All Articles