What is the difference between Java Keywords extends and Java Implements?

Asked

Viewed 7,914 times

6

I would like to know when they should be used and how we can distinguish them. I know that basically Implements, means that this class implements a class or an interface. E extends can access the methods of the base class.

  • 2

    extends is only for inheritance, when you want a particular class to "extend" its functionality to another, so that it can use this original functionality or adapt to your need. implements is for interface only, because this works as a contract, and you need to "implement" in the class the methods that the interface has.

  • Just a note, @diegofm, for those who are creating interfaces. extends can also be used in the context of interface hierarchy https://docs.oracle.com/javase/tutorial/java/IandI/nogrow.html

  • @diegofm, but extends can be used in either a derivative class or a base class? or only a derivative class can have extends?

  • @jorgesaraiva did not understand your question.

  • @jorgesaraiva, I believe this article can help you : http://www.tiexpert.net/programacao/java/heranca.php

2 answers

13


extends

Is used extends when you want to apply inheritance to your class.

When we say the class A stretches the class B, means that A inherits some (or all) class methods and attributes B.

The only methods and attributes that are not inherited are those that have the access modifier private.

Several levels of inheritance can be applied. For example:

class A extends B { }
class B extends C { }
class C extends D { }

By doing this, members with access modifier public of the classes D, C and B are accessible in the A. Members with access modifier protected class B are also accessible in the A.

In Java it is possible to inherit only from one class. There is no multiple inheritance.


implements

Is used implements when you want to implement an interface.

No classes are implemented. Only interfaces are implemented.

A "contract firm" interface between classes that defines behaviors (methods) that must be overridden by the class that inherits them (if that is a class concrete).

An interface can contain methods and constants. Java constants are defined by the words static and final. Ex: public static final String NOME = "xpto";.

The methods of an interface have no body. They have only their definition. Ex:

public interface Imprimivel {
    public void imprime();
}

Inheritance between interfaces can also be applied (exclusively):

public interface Monstro {
    public void ameacar();
}

public interface MonstroPerigoso extends Monstro {
    public void destruir();
}

public class Godzilla implements MonstroPerigoso {
    @Override
    public void ameacar() {
        //implementação
    }
    
    @Override
    public void destruir() {
        //implementação
    }
}

These chapters of the Caelum booklet are worth reading: Inheritance and Interfaces

  • 2

    All that remained was to quote what was mentioned by @Jeffersonquesado, about interfaces can also use extensions among themselves.

  • @diegofm updated

8

implements is used when a class implements an interface, extends is when a class extends another class (concrete or abstract) or when an interface extends another interface. That is, the keyword extends is used for when a type (class or interface) is derived from its same type, and the implements is whenever you want to make an implementation, in which case a class implements an interface.

Possibilities of using Keywords:

class Classe {}
class SubClasse extends Classe {}

interface Interface {}
interface SubInterface extends Interface {}

class SubClasse2 implements Interface {}
class SubClasse3 extends SubClasse implements Interface {}

The interface never has implementation so it can never use the implements, she can only use the extends, and it also can never derive from a class, otherwise it would be inheriting implementations for itself, something that by definition makes no sense for an interface.

The two Keywords represent that the class or interface is inheriting all methods and attributes and consequently having access to them, but there are still access modifiers which can for example declare methods and attributes such as private so that they only belong to the class that declared it.

Because the interface never has an implementation it would not make sense that it has a method or attribute that only it uses (private), because all the interface does is declare methods and attributes so that they are implemented by classes.

Example:

interface Interface2 {
    //private int i = 0;  <<< ERRADO! apenas public, static e final são permitidos
}

class Classe2 {
    public int i = 0;
    private int j = 1;
}

class SubClasse4 extends Classe2 {
    public void imprime() {
        System.out.println(i);
        //System.out.println(j); <<< ERRADO! O atributo não está visível para a subclasse
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.