Possibility to override parent class variables with child class variables in super constructor
See original GitHub issueIs your feature request related to a problem? Please describe. Unless I’m doing it wrong, you can’t override parent (abstract) class member variable from the child class
Describe the solution you’d like Being able to override (abstract) parent member variables with child class member variables of the same name, maybe with an “override” keyword This would enable things like this :
public abstract class CustomUnitDefinition extends UnitDefinition
string name = "Default Unit Name"
construct( int unitId, int origID )
super( unitId, origID )
setName( name ) // Since you can't instanciate CustomUnitDefinition , setName should be called with the value of MyUnit.name
public class MyUnit extends CustomUnitDefinition
string name = "Child Unit Name" // Should override CustomUnitDefinition .name
construct( int unitId, int origID )
super( unitId, origID )
// setName( "Child Unit Name" ) gets called from parent CustomUnitDefinition construct()
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Overriding member variables in Java ( Variable Hiding)
So overriding variable of base class is not possible, but base class variable value can be set (changed) from constructor of inherited class....
Read more >Access Super Class Methods and Instance Variables Without ...
We can invoke the overridden method of the parent class with the help of the super keyword. super() is used for executing the...
Read more >Why the Instance Variable of the Super Class Is Not ... - DZone
Well generally, we say that the Child class will override the variable declared in the Parent class, and parent.x will give us whatever...
Read more >Overriding Parent Class Method - Incremental Java
We have overriden the getPerimeter() method from the parent class. To override it, we must have the identical method signature, but put in...
Read more >Subclassing and Inheritance - Learning Java, 4th Edition [Book]
Classes in Java exist in a hierarchy. A class in Java can be declared as a subclass of another class using the extends...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Doesn’t work if those values are used in the super-constructor.
I suppose what’s best and the sexiness here lies in the eye of the beholder. I personally would prefer a different solution here. If you don’t need access to the
PigDefinition
class/type at runtime, you could also get rid of it completely in favor of a builder pattern (see cascade operator).Fair enough.