0
Why do I get error here (Inheritance)?
https://code.sololearn.com/cCt7rgGS8jkU/?ref=app Why can't I give the value to the superclass variable from the subclass?
8 Answers
+ 1
class Football extends Cricket{
public Football() {
super.runs= 100;
}
}
+ 1
class Football extends Cricket{
{ runs=100; }
}
+ 1
this brackets here defines 'initializer block' it is used as alternative to constructor
https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
0
HrCoder what if I just want to access the variable from the superclass without using any constructor or method. Is that possible?
0
zemiak cool it works!
But why doesn't it work without those curly brackets?
0
outside curly brackets or methods it is a place for declaring fields, but you are try to assign value to existing fields so you need some executable block of code
0
zemiak so the brackets make the lines executable? I dont clearly get what you are trying to say.
0
zemiak ok thanks