If an A class is an implementation of an X interface, will the subclasses of A also be?

Asked

Viewed 77 times

1

With the interface Posicionavel:

package projeto;

public interface Posicionavel
{
  public boolean mesmaPosicao(Posicionavel p);
  public boolean mesmaPosicao(int[] x);
  public int[] posicoes();
}

And the class Celula:

package projeto;
public abstract class Celula implements Posicionavel
{
    protected String cor;
    protected int x, y;

    public Celula(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int[] posicoes()
    {
      return new int[]{x, y};
    }

    public boolean mesmaPosicao(Posicionavel p)
    {
      return posicoes().equals(p.posicoes());
    }

    public boolean mesmaPosicao(int[] x)
    {
      return posicoes().equals(x);
    }  
}

The class below will also be an implementation of Posicionavel?

package projeto;

public class CelulaBoca extends Celula 
{ 
    public CelulaBoca(int x, int y) 
    {
        super(x, y);
        this.cor = Cores.VERMELHO;
    }    
}

2 answers

1

Yes, it will be, the inheritance makes the new class have all the characteristics of the mother class, its fields, its methods, and its explicitly stated types. A child class can have more than a mother, but never less, it’s a matter of contract fulfillment.

This kind of doubt arises because you still don’t understand what heritage is for and you’re decorating how you use the mechanism. This never works and creates the illusion that you are object-oriented programming because you understand the mechanisms. When you understand what heritage is for, that sort of thing becomes natural. You can start here, but don’t stop there. It can also be useful: Classes that implement interfaces are considered subclasses? and What is heritage of type and state?, and yet Principle of Liskov’s replacement.

  • Does it inherit all attributes even if there is one that is private? For if it does not inherit, the subclass may have "less things"...

  • Yes, inherits everything, the private just says that the daughter class can’t access the field (and not attribute that it’s wrong term they learned), but it’s there, there’s no way it can’t be. I guess you didn’t read the links that I went through. Almost everyone understands wrong heritage and then ends up using wrong.

1


Yes and you can prove by creating a positional reference variable that, without any problem, will contain the instance of a class that implements it directly (Cellula), or indirectly (Celulaboca).

If you declare the line below, it will compile and run smoothly:

int qualquerValorDeX = 1;
int qualquerValorDeY = 2;
Posicionavel posicionavel = new CelulaBoca(qualquerValorDeX, qualquerValorDeY);

This only allows access to methods visible in Positional Interface.

I say visible, because there could be private methods in this interface (yes, you can) and all implementations of Posicionavel (Celula, Celulaboca, etc) will inherit this behavior, however will not have visibility direct to access them (which does not happen with public access modifier, for example).
This does not matter if on the other end of the operator = i am referring to an instance that implements Posicionavel(Celula, Celulaboca, etc), which has N new methods and with the highest possible visibility that is the public due to the reference variable being of the type Positionable:

posicionavel.mesmaPosicao(new int[1]);
posicionavel.mesmaPosicao(new CelulaBoca(qualquerValorDeX, qualquerValorDeY));
System.out.println(Arrays.toString(posicionavel.posicoes()));

Ah, one more thing... If Positionable has any static (yes, you can) and public method, any implementation (Cellula, Cellulaboca, etc) will not inherit these methods because static methods never inherited through inheritance. This also serves for static attributes, never are inherited because what is static belongs only to class.

Browser other questions tagged

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