question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possibility to override parent class variables with child class variables in super constructor

See original GitHub issue

Is 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:open
  • Created 3 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Frottycommented, May 19, 2020

I still don’t understand why you cannot simply move the variable assignments to the constructor?

Doesn’t work if those values are used in the super-constructor.

0reactions
Frottycommented, May 22, 2020

That’s the best possible solution with the current language features, yes. Not as sexy as I would like it to be, but it works.

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).

But that’s why this issue is a feature request and not a bug report :p

Fair enough.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found