I was looking for the asnwer long time and i get it a bit plicated. What is a difference between inherits and extends of classes ?
My question was born after reading this ebook and i was using extends
syntax so it maked me wonder.
Extends Classes
class A {
a = 2;
constructor(x) {
this.a = x;
}
}
class B extends A {
}
Class Inheritance
class A {
a = 4;
A(x) {
a = x;
}
drive() {
output( "A" )
}
}
class B inherits A {
drive() {
inherited:drive()
output( "B" )
}
}
Can i use constructor
when inherits
classes ? or name constructor
when extend classes ?
What is the differenc when using super
or inherited
?
Can i use inherited
syntax when extending class ?
I read that super
is a direct way for the constructor of a child class to reference the constructor of its parent class.
I was looking for the asnwer long time and i get it a bit plicated. What is a difference between inherits and extends of classes ?
My question was born after reading this ebook and i was using extends
syntax so it maked me wonder.
Extends Classes
class A {
a = 2;
constructor(x) {
this.a = x;
}
}
class B extends A {
}
Class Inheritance
class A {
a = 4;
A(x) {
a = x;
}
drive() {
output( "A" )
}
}
class B inherits A {
drive() {
inherited:drive()
output( "B" )
}
}
Can i use constructor
when inherits
classes ? or name constructor
when extend classes ?
What is the differenc when using super
or inherited
?
Can i use inherited
syntax when extending class ?
I read that super
is a direct way for the constructor of a child class to reference the constructor of its parent class.
-
2
As far as I'm aware
inherits
is not a keyword in ES6...!? – deceze ♦ Commented Feb 11, 2016 at 11:08 -
1
There is no mention of
inherits
in the spec – Davin Tryon Commented Feb 11, 2016 at 11:09 -
2
Inheritance (noun, not keyword) is a concept.
extends
(ES6 keyword) is an implementation of that concept. When one classextends
another, it inherits its properties... – deceze ♦ Commented Feb 11, 2016 at 11:11
2 Answers
Reset to default 7inherits
is not a keyword in ES6. In that place of a class
declaration, only extends
is valid, you've got a syntax error.
And neither are a = 4;
in a class body nor inherited:drive()
. The book section you found this in even explicitly states "Consider this loose pseudo-code (invented syntax) for inherited classes".
Inheriting refers to the relationship between a derived class (the child) and the base class (the parent). The derived class can use certain methods and fields within the base class according to accessibility levels
Extending is interchangeable with Inheriting and usually is used in java (since the syntax for inheritance in java is the keyword extends. In C#, it is colon :