JAVA: are the notion of subtyping and of inheritance the same thing?
Asked Answered
M

5

5

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

Meredeth answered 28/8, 2014 at 15:35 Comment(0)
O
6

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 datatype that is related to another datatype (the supertype) by some notion of substitutability, meaning that program elements, typically subroutines or functions, written to operate on elements of the supertype can also operate on elements of the subtype.

In short, let's 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 are referring to Child as a Super. So you should see what I mean when I say that Inheritance allows you to perform subtyping, but they are not the same thing.

For example, you can achieve subtyping with interfaces:

class Child extends Super implements ISomeInterface {
}

Now we can refer to Child as:

ISomeInterface someInterface = new Child();

Here, we're referring to it as the type ISomeInterface, without the need for an inheritance relationship.

Your Questions

All Objects in Java are subclasses of type Object. They implicitly have extends Object in their class header. It's just the way the language works. So yes, every object is a subtype of class Object.

In Java, inheritance is only available through the use of the extends keyword.

Extra Reading

Edit

Listen to everything Marko Topolnik says. He is pretty smart you know.

Orbital answered 28/8, 2014 at 15:44 Comment(7)
Are you suggesting that Child dos not inherit form ISomeInterface? What exactly items does it not inherit?Octagonal
It doesn't inherit anything from the interface. The class realises the interface.Orbital
What, in particular, does it not inherit? In fact, try to disprove this statement: the class inherits everything there is to inherit from an interface.Octagonal
But there is nothing to inherit. Inheritance is by definition the acquisition of functionality and variables from a superclass, by a subclass. This is a contract, where the concrete implementation provides functionality for the interface.Orbital
There is something to inherit, and quite a lot in fact since Java 8: 1. static constants; 2. default methods; 3. static methods. Of these, only 3. is not inherited.Octagonal
Anyway, if there was nothing to inherit, and nothing was inherited, then everything there is to inherit was inherited. You cannot decide which way of looking is correct, so there is no essence in claiming that an interface is special in that way. For comparison, if I extend abstract class X { abstract int method(); }, have I inherited from it on not, according to your implied criterion?Octagonal
I'm not completely following you, but this sounds like one of those conversations where I'm behind. I'll add an edit.Orbital
G
5

The footnote reads:

As it happens the notion of "subtype" is not entirely in line with "inherits from": Interfaces with no super interface are indeed subtypes of Object (§ 4.10.2. Subtyping among Class and Interface Types ) even though they do not inherit from Object.

Interfaces can only extend other interfaces--none of them actually extends Object, either explicitly or implicitly. And yet, all of the Object methods are available on every interface. This makes an interface like List<> a subtype of Object--it has all the method signatures that Object would have--even though the interface itself does not inherit implementations of those methods from the Object class.

Goldeneye answered 28/8, 2014 at 15:47 Comment(0)
O
4

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

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

Octagonal answered 28/8, 2014 at 16:31 Comment(0)
P
1

Inheritance is explicit, subtyping is implicit.

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

See also: Inheritance is not subtyping

Progestational answered 28/8, 2014 at 15:42 Comment(0)
D
1

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

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

Usually, a subclass can use every method from its superclass, inheritance does not need this

source: here

Dannettedanni answered 28/8, 2014 at 15:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.