What is the difference between keywords extends and implements in java?

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 will be able to access the methods of the base class.

    
asked by anonymous 28.03.2017 / 13:05

2 answers

11

extends

You use extends when you want to apply inheritance to your class.

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

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

You can apply various levels of inheritance. For example:

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

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

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

implements

You use implements when you want to implement an interface.

Classes are not implemented. Implements only interfaces.

An interface "signs a contract" between classes in which it defines behaviors that must be overridden by the class that inherits them (if this is a concrete class).

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();
}

You can also apply inheritance between interfaces (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
    }
}

Worth reading these chapters of Caelum's handout: Heritage and Interfaces

    
28.03.2017 / 13:41
7

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 type, and implements is always that it wants to make an implementation, in case a class implements an interface.

Possibilities for 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 an implementation so it can never use implements , it can only use extends , and it can never derive from a class either, otherwise it would inherit implementations for itself, something that setting does not make sense to an interface.

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

Because the interface never has an implementation, it would not make sense for it to have a method or attribute that it only uses (private), since all the interface does is declare methods and attributes to be 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
    }
}
    
28.03.2017 / 13:45