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?
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
The Liskov Substitution Principle is a design principle that heavily focuses on the idea of subtyping
The different relationships between Java classes. This will help you gain a deeper understanding of OOP.
Edit
Listen to everything Marko Topolnik says. He is pretty smart you know.
abstract class X { abstract int method(); }
, have I inherited from it on not, according to your implied criterion? –
Octagonal 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 fromObject
.
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.
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.
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
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
© 2022 - 2024 — McMap. All rights reserved.
Child
dos not inherit formISomeInterface
? What exactly items does it not inherit? – Octagonal