JAVA: Is the concept of subtyping and inheritance the same?

Based on the note at the end of this answer , it seems that subtyping and inheritance are slightly different concepts in Java. What is it? Is inheritance only when the class declaration contains an extends ... clause extends ... ? If so, then the class will not inherit from Object, even if it is a subtype, right?

+5
source share
5 answers

Inheritance is a way to achieve subtyping. Taken from Wikipedia :

In programming language theory, subtyping (also subtype polymorphism or inclusion polymorphism) is a form of type polymorphism in which a subtype is a data type that is associated with another data type (supertype) using some notion of substitution, which means that program elements, usually subroutines or functions written to work on supertype elements can also work with subtype elements.

In short, let's take a look at this:

 class Super { } class Child extends Super { } 

This is inheritance, because Child inherits everything from Super .

 Super super = new Child(); 

This is subtyping because we mean Child as Super . Therefore, you should understand what I mean when I say that Inheritance allows subtyping, but it’s not the same thing.

For example, you can achieve subtyping using interfaces:

 class Child extends Super implements ISomeInterface { } 

Now we can refer to Child as:

 ISomeInterface someInterface = new Child(); 

Here we refer to it as an ISomeInterface type, without requiring an inheritance relationship.

Your questions

All Objects in Java are subclasses of type Object . They implicitly have extends Object in the title of their class. This is how the language works. So yes, every object is a subtype of the Object class.

In Java, inheritance is only available with the extends .

Additional reading

Edit

Listen to everything that Marco Topolnik says. He's pretty smart, you know.

+6
source

The footnote says:

How this happens, the concept of a “subtype” does not fully correspond to “inheritance”: interfaces without a superinterface are indeed subtypes of Object ( § 4.10.2. Subtyping between classes and types of interfaces ), although they are not inherited from Object .

Interfaces can extend only other interfaces - none of them are actually extends Object , explicitly or implicitly. And yet, all Object methods are available for each interface. This makes an interface like List<> subtype of Object - it has all the method signatures that Object will have - even if the interface itself does not inherit the implementation of these methods from the Object class.

+5
source

Inheritance and subtyping are two separate concepts. A type can only inherit its parent type; therefore, inheritance is tied to a subtype relationship. However, the opposite is not true: the subtype does not necessarily inherit anything from its parent. Language rules dictate exactly what is inherited by the subtype.

In the Java example, private members are not inherited. In Java 8, interfaces can declare static methods, but their subtypes do not inherit these members.

+3
source

Inheritance is explicit; subtyping is implicit.

Everything (except primitive types) is a subtype of the object. If you explicitly use the extends , then you are using inheritance.

See also: Inheritance is not subtyping

+1
source

Subtyping refers to interface compatibility. Type B is a subtype of A if every function that can be called on an object of type A can also be called on an object of type B.

Inheritance refers to the reuse of implementations. Type B inherits from another type A if some functions for B are written in terms of functions A.

Usually a subclass can use each method from its superclass; inheritance does not need it

source: here

+1
source

Source: https://habr.com/ru/post/1201342/


All Articles