Sam Stone
10/16/2023, 5:32 PMopen class Parent(open val x: Int)
class Child(override val x: Int): Parent(x)
Instead of
open class Parent(open val x: Int)
class Child(override val x: Int): Parent()
Wesley Hartford
10/16/2023, 5:33 PMopen class Parent(open val x: Int)
class Child(x: Int): Parent(x)
x
a (possibly abstract) member of parent, but not put it in the constructor.ephemient
10/16/2023, 5:41 PMopen class Parent(open val x: Int)
class Child(override val x: Int) : Parent(x = x + 1)
is legal (and confusing)open class Parent {
abstract val x: Int
}
class Child(override val x: Int) : Parent()
then there is no ambiguity and no duplicationKlitos Kyriacou
10/16/2023, 5:44 PMclass Parent {
private int x;
public int getX() { return x; }
public Parent(int x) { this.x = x; }
}
For which you now need:
class Child extends Parent {
Child(int x) {
super(x);
}
// You don't need the below, therefore you don't need class Child(override val x: Int)
private int x;
public int getX() { return x; }
}
Or, of course, you can use Ephemient's suggestion, equivalent to:
class Parent {
abstract int getX();
}
ephemient
10/16/2023, 5:46 PMclass Parent {
private int x;
public int getX() { return x; }
public Parent(int x) { this.x = x; }
}
class Child extends Parent {
private int x;
@Override
public int getX() { return x; }
public Child(int x) { super(x); }
}
where there are two different fields x
which are not necessarily in sync with each otherSam Stone
10/16/2023, 5:51 PMopen class Parent<T>(val value: T)
class Child(override val value: Int): Parent(value)
Or
open class Parent(val value: Int)
class Child(val value1: Int, val value2: Double): Parent(value1)
It would be nice if there was a simple way to specify the type of T (e.g. class Child(_): Parent<Int>(_)
- and Child(1F)
would be a type error), or add an additional property (e.g. class Child(_, val value2: Double): Parent(_)
) and avoid the need to repeat constructors.ephemient
10/16/2023, 5:55 PMopen class Parent<T> {
abstract val value: T
}
class Child(override val value: Int) : Parent<Int>()
open class Parent<T>(val value: T)
class Child(value: Int) : Parent<Int>(value)
Int
at least twice either way but that is for different reasons (https://youtrack.jetbrains.com/issue/KT-43594)Sam Stone
10/18/2023, 4:23 AMChild
a data class
and only have a primary constructor and name the property the same as in the parent class, because I need to add val/var. Hence, none of the suggested solutions will work (unless I want to declare abstract properties in the parent class, which is not ideal).ephemient
10/18/2023, 10:18 PMDavid Kubecka
10/19/2023, 8:08 AM