+ 1

Inheritance(java)

why is this code not working? https://code.sololearn.com/chiMUy96A4UE/?ref=app

22nd Oct 2017, 4:24 PM
oyl
6 Answers
+ 4
Your println call in Superclass needs to be within a class method. class Superclass { int cat; Superclass() { System.out.println(cat); } } class Subclass extends Superclass { Subclass() { cat=10; } } class Dog{ public static void main(String[] args) { Subclass obj = new Subclass(); } } Note: This will output 0 the way that it is, due to the fact that the variable cat isn't assigned when the Superclass is created (Superclass is always created prior to Subclass)
22nd Oct 2017, 4:35 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Generally speaking in Java you can't call a method within a class outside of a method that belongs to the class unless that method is returning a value that is being set to a class variable. The println() method doesn't return a value to set to a class variable. This will result in an error as it doesn't follow Java's structuring. You're basically trying to call a method as if it is itself a class member variable which doesn't make sense. In order to call a method inside a class, it must be within a method that belongs to that class. This has nothing to do with inheritance. int cat is, by default, initialized to 0 when an instance of Superclass is created, as any variable of the int type that is not set would be. Since Subclass is a child of the Superclass the Superclass must exist prior to the creation of the Subclass. In main, when the instance of Subclass is created the Superclass' constructor is called first as Subclass is an instance of Superclass as well (see polymorphism). At that time int cat has not been set so it will get the default value of 0 which is printed out in the Superclass' constructor instead of the value 10 which cat is given when the Subclass is created after the Superclass has been created. class Superclass { Superclass() { System.out.println("Superclass created!!"); } } class Subclass extends Superclass { Subclass() { System.out.println("Subclass created!!"); } } class Main{ public static void main(String[] args) { Subclass obj = new Subclass(); } } if you run the code above you'll see the output: Superclass created!! Subclass created!! Likewise, when obj is destroyed first the Subclass is destroyed then the Superclass. The Superclass is essentially wrapped around the Subclass.
22nd Oct 2017, 5:48 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Sorry, tired = poor explanation. Maybe this will help clear things up a bit: public class myClass { int number = getValue(); // Method call is OK here as long as the proper value is returned // method call by itself here won't work // Constructor myClass() { // Method call here is OK } classMethod() { // Method call here is OK } }
22nd Oct 2017, 6:21 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
@Chaoticdawg 1) is it because System.out.println() is not a method or variable that is why it wont get inherited? 2) i dont understand the note that you provided.can you explain more thankyou
22nd Oct 2017, 4:54 PM
oyl
+ 1
thankyou for your help:)
22nd Oct 2017, 6:27 PM
oyl
0
"Generally speaking in Java you can't call a method within a class outside of a method that belongs to the class unless that method is returning a value that is being set to a class variable."wow such confusion
22nd Oct 2017, 5:59 PM
oyl