Why do I get error here (Inheritance)? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

9th Aug 2021, 2:15 PM
Srinath
8 Answers
+ 1
class Football extends Cricket{ public Football() { super.runs= 100; } }
9th Aug 2021, 2:32 PM
Pariket Thakur
Pariket Thakur - avatar
+ 1
class Football extends Cricket{ { runs=100; } }
9th Aug 2021, 4:12 PM
zemiak
+ 1
this brackets here defines 'initializer block' it is used as alternative to constructor https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
10th Aug 2021, 7:02 PM
zemiak
0
HrCoder what if I just want to access the variable from the superclass without using any constructor or method. Is that possible?
9th Aug 2021, 2:37 PM
Srinath
0
zemiak cool it works! But why doesn't it work without those curly brackets?
9th Aug 2021, 4:46 PM
Srinath
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
9th Aug 2021, 5:48 PM
zemiak
0
zemiak so the brackets make the lines executable? I dont clearly get what you are trying to say.
10th Aug 2021, 2:18 PM
Srinath
0
zemiak ok thanks
11th Aug 2021, 12:12 PM
Srinath