0

whats the problem?

abstract class Bank{ final float moneyInBank = 0; float x; Bank(float x){ this.x = x; } } public class Main extends Bank { Main(float x){ super(x); } public static void main(String[] args) { Bank b = new Main (100); b.addMoney(); System.out.println (b.moneyInBank()); } public static float addMoney (float moneyInBank , float x){ return moneyInBank + x; } }

29th May 2020, 3:36 PM
Yahel
Yahel - avatar
16 odpowiedzi
+ 1
Probably this is the closest to the code that you shared. Points to remember- 1) do not make a variable 'final' if you are going to update it's value in the process. A 'final' variable must be a constant value. 2) there is no point in making a class abstract until and unless you have an abstract method that requires implementation in the class extending it. So I have added that abstract method in the Bank class so that it could be implemented. 3) When you create a reference of base class but an object of the child class, only the methods in base class will work and any new method in child class will not be supported in the code. Anyways this is what I could do. But a bad design. You will eventually improve through continuous practice. https://code.sololearn.com/c9BqNP7xsCU4/?ref=app
29th May 2020, 6:09 PM
Avinesh
Avinesh - avatar
+ 1
SPICEAPPLE 🍎 { active: 0 } thanks, but is there a way that is more similar to how I did it?
29th May 2020, 4:10 PM
Yahel
Yahel - avatar
+ 1
Ok, LOL :)
29th May 2020, 4:30 PM
Yahel
Yahel - avatar
+ 1
Because you are overriding the addMoney() and cannot alter the method in child class. Also you would not be able to access moneyInBank variable inside that method since you can only access static members in static context.
29th May 2020, 6:47 PM
Avinesh
Avinesh - avatar
+ 1
Avinesh 👌🏻👌🏻
29th May 2020, 6:49 PM
Yahel
Yahel - avatar
0
Please show the link to your code, do not paste your code in the question, it's hard to read.
29th May 2020, 3:39 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
I think the addMoney() should not be a static method. Also, I'm confused about the parameters of it.
29th May 2020, 3:45 PM
Nootnoot
Nootnoot - avatar
0
SPICEAPPLE , still not working...
29th May 2020, 3:48 PM
Yahel
Yahel - avatar
0
I don't know how your code supposed to do but I changed only a bit of your code xD. edit: Also, feel free to ask me anything about it. https://code.sololearn.com/cy0sN6alRJKA/?ref=app
29th May 2020, 3:59 PM
Nootnoot
Nootnoot - avatar
0
I actually don't know how to make it similar on how you did it. I'm sorry for that. I think I'll leave the rest to you. But I suggest that you should plan it more carefully to make it work better. Remember you can ask me anytime if you are stuck or something xD. edit: you can use this as a reference: https://code.sololearn.com/c185wy1pOV3Q/?ref=app
29th May 2020, 4:18 PM
Nootnoot
Nootnoot - avatar
0
SPICEAPPLE 🍎 { active: 0 } ok, thanks. Btw it says you have an error in the code you just inserted...
29th May 2020, 4:27 PM
Yahel
Yahel - avatar
0
Sorry 'bout that, my phone died at the last minute. and its working now xD.
29th May 2020, 4:29 PM
Nootnoot
Nootnoot - avatar
0
Avinesh thank you, I can see now what was the problem...
29th May 2020, 6:31 PM
Yahel
Yahel - avatar
0
yahel I can see that you are beginner who is working hard to learn this language and that is the reason I give you descriptive answers so that you learn from your mistakes. Anyways good to know that it helped.
29th May 2020, 6:35 PM
Avinesh
Avinesh - avatar
0
Avinesh , thanks! btw, why in line 37 it doesn't need the "static" KeyWord?
29th May 2020, 6:39 PM
Yahel
Yahel - avatar