Classes that implement interfaces are considered subclasses?

9

I have an interface Veiculo , a class Peugeot that implements Veiculo . This Peugeot is considered subclass?

    
asked by anonymous 12.09.2016 / 02:47

1 answer

12

Concepts

Let's conceptualize things. Although not everyone agrees with these definitions, I will put an understanding that many have.

Subclass

It is the ability to reuse something existing code.

It's something internal to the class.

It is itself "selectively copying" what exists in the base class. It takes the attributes and methods that exist in the class and takes them as if they were yours.

This is usually done by inheritance . That is, one class is derived from another class.

Contrary to popular belief, even private parts of a class are inherited by the class that inherits it, only visibility is limited .

Subtype

It's the ability of one type to pass through another, somehow replacing in certain situations as if it were the expected type itself.

It's something external to the guy who consumes the type.

A method expects an object of one type, but it can receive an object of another type that meets the requirements of the type originally accepted.

Normally, the polymorphism . It may be from a concrete class, an abstract class , a interface or a trait ( in Java 8 is possible something good ), among other possibilities in some languages.

Note that because it is something of an external use everything referring to the subtype should be publicly obligatory.

Classes usually have the structural subtype and the behavioral subtype . The interface is just a contract, it's about typing and not about implementation, it has neither status nor behavior.

Java only works with nominal subtype .

Inheritance

It is common when we treat inheritance that we are talking about both, although they are separate concepts. Has subclass language without subtype (not Java). Some have totally different concepts, but I'm not going to go into details, it's not the focus of the question.

So some say Java does not allow multiple inheritance and others say yes . It depends on the setting. Java allows simple subclassing and multiple subtypes.

Is the interface implementation a subclass?

In part the question, not very clear but answerable, is already answered. Interfaces have more to do with subtype and not with subclass. But in Java 8 it is possible to have interfaces that behave as subclasses. But only when using default methods, but we'll leave that aside to make it easier.

Then:

public interface MeioTransporte { ... }
public interface Veiculo extends MeioTransporte { ... } //subtipo
public class Carro implements Veiculo { ... } //sutipo
public class Peugeot extends Carro { ... } //subclasse e subtipo

The examples are not good, but I followed the question. Veiculo would hardly be an interface.

Classes that implement interfaces are only subtypes, so interfaces are supertype classes or other interfaces.

Conclusion

I'll stress that hard to do subclass and subtype right . Most programmers do not have a clue how to do it right, many even know what OOP means. Even experienced programmers have difficulty doing well in many cases. So be very careful to manipulate this "weapon".

    
12.09.2016 / 05:04