whats the problem? | SoloLearn: Learn to code for FREE!

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; } }

5/29/2020 3:36:33 PM

yahel

16 Answers

New Answer

+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

+1

SPICEAPPLE 🍎 { active: 0 } thanks, but is there a way that is more similar to how I did it?

+1

Ok, LOL :)

+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.

+1

Avinesh 👌🏻👌🏻

0

Please show the link to your code, do not paste your code in the question, it's hard to read.

0

Kuba Siekierzyński https://code.sololearn.com/cWOjG0kjpe08/?ref=app

0

I think the addMoney() should not be a static method. Also, I'm confused about the parameters of it.

0

SPICEAPPLE , still not working...

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

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

0

SPICEAPPLE 🍎 { active: 0 } ok, thanks. Btw it says you have an error in the code you just inserted...

0

Sorry 'bout that, my phone died at the last minute. and its working now xD.

0

Avinesh thank you, I can see now what was the problem...

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.

0

Avinesh , thanks! btw, why in line 37 it doesn't need the "static" KeyWord?