Implements VS Extends: When to Use? What's the Difference

Implements vs extends: When to use? What's the difference?

extends is for extending a class.

implements is for implementing an interface

The difference between an interface and a regular class is that in an interface you can not implement any of the declared methods. Only the class that "implements" the interface can implement the methods. The C++ equivalent of an interface would be an abstract class (not EXACTLY the same but pretty much).

Also java doesn't support multiple inheritance for classes. This is solved by using multiple interfaces.

 public interface ExampleInterface {
public void doAction();
public String doThis(int number);
}

public class sub implements ExampleInterface {
public void doAction() {
//specify what must happen
}

public String doThis(int number) {
//specfiy what must happen
}
}

now extending a class

 public class SuperClass {
public int getNb() {
//specify what must happen
return 1;
}

public int getNb2() {
//specify what must happen
return 2;
}
}

public class SubClass extends SuperClass {
//you can override the implementation
@Override
public int getNb2() {
return 3;
}
}

in this case

  Subclass s = new SubClass();
s.getNb(); //returns 1
s.getNb2(); //returns 3

SuperClass sup = new SuperClass();
sup.getNb(); //returns 1
sup.getNb2(); //returns 2

Also, note that an @Override tag is not required for implementing an interface, as there is nothing in the original interface methods to be overridden

I suggest you do some more research on dynamic binding, polymorphism and in general inheritance in Object-oriented programming

What's the difference between 'extends' and 'implements' in TypeScript

Short version

  • extends means:

The new class is a child. It gets benefits coming with inheritance. It has all the properties and methods of its parent. It can override some of these and implement new ones, but the parent stuff is already included.

  • implements means:

The new class can be treated as the same "shape", but it is not a child. It could be passed to any method where Person is required, regardless of having a different parent than Person.

More ...

In OOP (languages like C# or Java) we would use

extends to profit from inheritance.

... Inheritance in most class-based object-oriented languages is a mechanism in which one object acquires all the properties and behaviours of the parent object. Inheritance allows programmers to: create classes that are built upon existing classes ...

implements will be more for polymorphism.

... polymorphism is the provision of a single interface to entities of different types...

So we can have a completely different inheritance tree for our class Man:

class Man extends Human ...

but if we also declare that Man can pretend to be the Person type:

class Man extends Human 
implements Person ...

...then we can use it anywhere Person is required. We just have to fulfil Person's "interface" (i.e. implement all its public stuff).

implement other class? That is really cool stuff

Javascript's nice face (one of the benefits) is built-in support for duck typing.

"If it walks like a duck and it quacks like a duck, then it must be a duck."

So, in Javascript, if two different objects have one similar method (e.g. render()) they can be passed to a function which expects it:

function(engine){
engine.render() // any type implementing render() can be passed
}

To not lose that in Typescript, we can do the same with more typed support. And that is where

class implements class

has its role, where it makes sense.

In OOP languages as C#, no way to do that.

The documentation should help here:

Interfaces Extending Classes

When an interface type extends a class type it inherits the members of
the class but not their implementations. It is as if the interface had
declared all of the members of the class without providing an
implementation. Interfaces inherit even the private and protected
members of a base class. This means that when you create an interface
that extends a class with private or protected members, that interface
type can only be implemented by that class or a subclass of it.

This is useful when you have a large inheritance hierarchy, but want
to specify that your code works with only subclasses that have certain
properties. The subclasses don’t have to be related besides inheriting
from the base class. For example:

class Control {
private state: any;
}

interface SelectableControl extends Control {
select(): void;
}

class Button extends Control implements SelectableControl {
select() { }
}

class TextBox extends Control {
select() { }
}

// Error: Property 'state' is missing in type 'Image'.
class Image implements SelectableControl {
private state: any;
select() { }
}

class Location {

}

So, while

  • extends means it gets all from its parent
  • implements in this case it's almost like implementing an interface. A child object can pretend that it is its parent... but it does not get any implementation.

What is the difference between extends and implements in java with respect to performance and memory,etc

During a memorable Q&A session, someone asked James Gosling (Java's inventor): If you could do Java over again, what would you change? . I'd leave out classes, he replied

Java does not allow you to extend multiple classes. This avoids some problems related to multiple inheritance. However you can choose to implement multiple interfaces. This is a huge plus.

Some people also believe extends decreases the flexibility of their code.

However, I doubt there is any huge difference in performance, efficiency etc. In fact, they might even produce the same byte-code ( though I'm not sure ). And you are comparing two different things that have different functions, its like asking if Apples are more efficient than Oranges.

extends versus implements versus with

Extends:

Use extends to create a subclass, and super to refer to the superclass.

Extends is the typical OOP class inheritance. If class a extends class b all properties, variables, functions implemented in class b are also available in class a. Additionally you can override functions etc.

You use extend if you want to create a more specific version of a class. For example the class car could extend the class vehicle. In Dart a class can only extend one class.


Implements:

Every class implicitly defines an interface containing all the instance members of the class and of any interfaces it implements. If you want to create a class A that supports class B’s API without inheriting B’s implementation, class A should implement the B interface.

Implements can be used if you want to create your own implementation of another class or interface. When class a implements class b. All functions defined in class b must be implemented.

When you're implementing another class, you do not inherit code from the class. You only inherit the type. In Dart you can use the implements keyword with multiple classes or interfaces.


With (Mixins):

Mixins are a way of reusing a class’s code in multiple class hierarchies.

With is used to include Mixins. A mixin is a different type of structure, which can only be used with the keyword with.

They are used in Flutter to include common code snippets. A common used Mixin is the SingleTickerProviderStateMixin.

What's the difference between the implements & extends keywords in Java

An interface is an abstract specification of how a class should behave whilst a class is a concrete implementation of such a specification.

Therefore, when you write implements you're saying that you are fulfilling some abstract specification in the implementation you've written.

extends means that you take either an implementation (class) or specification (interface) and add to it with different or new functionality (or change the specification of its behaviour), thus modifying its behaviour and extend-ing it.

What is the difference between 'implements' keyword and 'extends' keyword in dart?

extends means we get the implementation of a given class and we can then override members if we want our own implementation for certain variables or methods. You can also add new variables and methods.

implements means you get nothing from the class you implement from. But you promise that your class will be compatible with the interface of the class you are implementing. So no, you are not getting any implementation from the super class and you need to implement everything or declare your class abstract.

implements vs extends in generics in Java

The difference is pretty straightforward: second code snippet does not compile and never will. With generics you always use extends, for both classes and interfaces. Also super keyword can be used there, but it has different semantics.

When to implement and extend?

Inheritance is useful to reduce the amount of code you rewrite. If you have several classes with a few common methods or fields, instead of defining these methods and fields over and over you can factor them into a base class and have each of the child classes extend that base class.

Interfaces (and implements) are useful when you'd like to define a common protocol for how a group of objects should behave. For example, you might want to mandate that objects that are comparable can be compared for equality and hashed, etc.

Using inheritance is ultimately a design choice. Be on the lookout for cases where you define the same methods across several classes; those are excellent cases where you can factor those methods out into a base class. The same goes for classes that observe some of the same characteristics: you can guarantee consistency by putting those characteristics in an interface to be implemented by those related classes.

Inheritance is a big concept in OOP that goes way beyond just PHP. I recommend you read the wikipedia article on inheritance and perhaps Design Patterns by the Gang of Four.

I believe your understanding of inheritance is mainly correct. The next step would be to use it in production.



Related Topics



Leave a reply



Submit