"New()" Inheritance question

Monkey Forums/Monkey Beginners/"New()" Inheritance question

Leo Santos(Posted 2016) [#1]
Consider this code:
Class Entity

	Field name := "entity"
	
	Method New()
		Error( "Please use New( name:String )" )
	End

	Method New( name:String )
		Self.name = name
	End

End


Class EntityA Extends Entity
End


Function Main()
	Local a := New EntityA
	Print a.name
End


If you run this, you get the error "Please use New( name:String )", which is the behavior I expect, since EntityA is inheriting New() from Entity. but if you replace:

Local a := New EntityA

With:

Local a := New EntityA( "Entity Name" )

You ALSO get an error, this time from Monkey saying "Unable to find overload for new(String).". Why? Shouldn't the method New( name:String ) be inherited as well, since New() was inherited?

I find it a little annoying, because I want all my extended entity classes to use the same New( name:String ) method, unless I explicitly define a new one. As it is, I have to redefine it for every new extended class simply so it can call Super.New( name:String ) ...


therevills(Posted 2016) [#2]
Constructors are not methods and do not get inherited. Sorry you are going have to go down the Super.New route...


Leo Santos(Posted 2016) [#3]
Hmmm, in that case, isn't it inconsistent that New() without arguments gets inherited, but New( name ) doesn't?

Thanks!


Gerry Quinn(Posted 2016) [#4]
I think probably it doesn't really get inherited as such - a default constructor is created containing a call to the super constructor. Isn't that how it works in Java?