What is the difference between extends and implements keywords




















Inheritance allows a new class to use properties and methods of an already existing class. It improves code reusability. In other words, the programmer can create new classes using already existing classes.

Therefore, it is not necessary to write programs from the beginning. Moreover, the concept of inheritance builds parent-child relationships between the classes. Here, the existing class is called the parent class or a superclass while the new class is called the child class or subclass. When the subclass inherit properties and methods of the superclass, the child class uses the extends keyword.

Figure 1: Java program with Inheritance. In the above program, class Color has a method called display. Class Green has a method called displayNewColor. Class Green extends class Color. Therefore, Color is the superclass while Green is the subclass. An interface cannot implement another interface. Inheritance being an important concept in Object Oriented Programming, is achieved when a subclass extends an another superclass.

A subclass is allowed to extend only one superclass. A class can never extend more than one super class in Java. When a class extends another class, it can override the methods defined in a superclass.

Let us understand keyword extends with the help of an example. In above code, you can observe that class B has extended class A and has overridden the method display defined in class A.

In a similar way an interface can extend more than one interface at a time. As interface do not have to implement any method of the extended interface, hence, it is allowed to inherit multiple interfaces at a time.

In order to better understand the difference between extends and implements , you also need to learn and understand the difference between class and interface in Java. Though both are an integral part of application development using the object-oriented methodology, an interface is more abstract than a class hence it is used to define API or contract. On the other hand, a class provides the concrete implementation of the interface i. Though a class can also be abstract to define a contract, it's the job best done by an interface in Java.

Even after Java 8, only multiple inheritances of type and behavior are supported in Java by introducing default methods in Java 8 , but multiple inheritances of the state are still not supported. If you want to learn more about why multiple inheritances are not supported in Java, see here. This is one of the best and up-to-date courses to learn Java online.

What is the meaning of extending a class? A class in Java can extend to another class to become a subclass. When class B extends class A, B becomes subclass child and A becomes superclass parent. A subclass can reuse all the features of the parent class and code reuse is a major reason for extending a class but few understand that more important is the defining relationship which is later leveraged by Polymorphism.

For example, if class B extends class A then a reference variable of type A can hold the object of B, which means A now becomes polymorphic because it can hold both A and B. This gives birth to a technique of creating flexible software, known as programming for interface than implementation where you always use variables of the parent class or interface to define the core algorithm.

The benefit of doing this is any new class that extends the parent class will have access to that algorithm. This immense power comes by using the extends keyword. You can also read the Head First Design Pattern in Java to learn more about the technique of programming for interface than implementation. The book is also recently updated to cover Java SE 8. Share to Twitter Share to Facebook. Labels: core java , core java interview question. Hi Javin, why extend keyword used before the implements keyword in Java?

Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up. Connect and share knowledge within a single location that is structured and easy to search. Firstly, it helps with readability, being able to see which is the superclass apart from interfaces. Secondly, though 'extends' and 'implements' do the same thing of inheriting from a Type and could be expressed in a single keyword, the different words help to express the different idea behind the inheritance.

We can see this idea is also expressed in how Interfaces 'extend' other Interfaces rather than 'implement' them. This is not a question about why Java avoids multiple inheritance. This is also not about the definition of Interfaces and Abstract Classes nor necessarily their conceptual differences, I think most of us understand those principles well enough.

The reason I ask is because sometimes during the live design phase of projects I find myself changing an Interface to an Abstract Class, or vice versa, then having to go through all its children to switch between 'extends' and 'implements'.

Sure, I could probably plan it out better but this situation just got me thinking if it was all really necessary. If it was a single keyword this would be handled purely by the compiler.

I don't see the need for this distinction between 'extends' and 'implements'; However, I'm not a language designer.. So is there some good reason that I may have overlooked?

I note that Generics actually does throw out this distinction, only using 'extends' for both types. Also, Interfaces can "extend" a list of other Interfaces further blurring the line between 'extends' and 'implements' in my opinion.

If you need to often switch between interface and class and adapt the code accordingly, there might be a misunderstanding of the different semantics that the syntax only highlights:. Now to answer your question, and abstract class is a class, even if it does not provide the implementation of some methods. In my above example, the fact that Vegetable would be an abstract class and would provide no implementation, does not change the strong nature of its relation with Potato.

Conclusion : from a technical point of view, an abstract class that would only define abstract methods without providing any implementation could look very similar to an interface that also defines some methods that need to be implemented.

But both language constructs have different semantics. The difference in syntax only makes explicit this differences in the meanings, in order to force us to think about what we really want to express. The simplest way to answer this is by observing other languagues. In C , both extends and implements have been merged into :.

Does it really matter to use a different keyword here? I don't think so. I don't see any benefit to it. But then again, I was raised with C and their lack of distinction between extension and implementation.



0コメント

  • 1000 / 1000