15
I have an interface Veiculo
, a class Peugeot
implementing Veiculo
. That one Peugeot
is considered subclass?
15
I have an interface Veiculo
, a class Peugeot
implementing Veiculo
. That one Peugeot
is considered subclass?
17
Let’s conceptualize things. Although not everyone agrees with these definitions, I will put an understanding that many have.
Is the ability to reuse code of something existing.
It’s something internal for the class.
It is itself selectively "copying" what exists in the base class. It takes the fields and methods that exist in the class and takes them as its own.
You usually find yourself inheritance. That is, one class derives from another class.
Contrary to what many people think, even the private parts of a class are inherited by the class that inherits it, only visibility is limited.
It’s the ability of one guy to pass for another, somehow the replacing in certain situations as if it were the expected type itself.
It is something external of the one who consumes the type.
A method expects an object of a type, but it can receive an object of another type that meets the requirements of the originally accepted type.
It usually happens by polymorphism. It may be of a concrete class, a abstract class, one interface or a trait (in Java 8 something similar is possible), among other possibilities in some languages.
Note that because it is something of external use everything that refers to the subtype must be public.
The classes usually have the structural subtype and the behavioral subtype. The interface, pure, is only a contract, is about typing and not about implementation, has neither state, nor behavior.
Java only works with nominal subtype.
It is common when it comes to inheritance that we are talking about both things, even though they are separate concepts. It has a language that subclass without subtype (not Java). Some have totally different concepts, but I will not go into detail, is not the focus of the question.
So some say that Java does not allow multiple inheritance and others say yes. Depends on the definition. Java allows simple subclass and multiple subtypes.
In part the question, not very clear but responsive, is already answered. Interfaces has more to do with subtype than with subclass. But in Java 8 it is possible to have interfaces that behave like subclasses. But only when using the methods default, but let’s put it aside to facilitate.
So:
public interface MeioTransporte { ... }
public interface Veiculo extends MeioTransporte { ... } //subtipo
public class Carro implements Veiculo { ... } //sutipo
public class Peugeot extends Carro { ... } //subclasse e subtipo
I put in the Github for future reference.
The examples are not good, but I followed the question. Veiculo
would hardly be an interface.
Classes that implement interfaces are only doing subtype, so the interfaces are supertypes of classes or other interfaces.
I’ll reinforce that it is difficult to make subclass and subtype right. Most programmers don’t even know what OOP means. Even experienced programmers have difficulty getting it right in several cases. So be very careful to manipulate this "weapon".
Browser other questions tagged java oop classes interface
You are not signed in. Login or sign up in order to post.
Perfect!!!!!!!
– Aline
Will help me in a presentation I will do hj.
– Aline