Reputation: 6546
I was looking for the asnwer long time and i get it a bit complicated. 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.
Upvotes: 2
Views: 4564
Reputation: 211
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 :
Upvotes: 0
Reputation: 664599
inherits
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".
Upvotes: 7