- 1

[SOLVED] can't override the method

I have no idea what the super class is even supposed to do and because the Account method for amount is private, when I do try to call amount it won't call it. I wanna keep it private so I can learn to override with private methods but I don't know how. Code is in comments.

23rd Feb 2021, 4:20 PM
Serana Zentha
Serana Zentha - avatar
8 Answers
+ 4
public class Main { public static void main(String[] args) { int a = 5; double b = 10.2; System.out.println(doubleTheValue(a)); System.out.println(doubleTheValue(b)); } //complete the method for integer value type public static int doubleTheValue(int a) { return a*2; } //overload the method for double value type public static double doubleTheValue(double a) { return a*2; } } //program to implement override
27th Sep 2022, 4:09 PM
Parthasarathi Panda
Parthasarathi Panda - avatar
+ 1
Serana Zentha instead of 'private' members, you can have 'protected' members. The protected members will be accessible in the class as well as all the classes that inherit from it. https://www.sololearn.com/learning/2156/ I hope I understood your question correctly. Also, instead of copy pasting the code in the answers to the thread, save it as a Java code in the code playground and link it here. That way you won't be limited by the character limit of the answers and also it will be easier to reference line numbers and compiler errors/warnings
23rd Feb 2021, 5:35 PM
XXX
XXX - avatar
+ 1
Hehe I hope you know that the cat isn't mine. It is a meme cat😅
23rd Feb 2021, 6:25 PM
XXX
XXX - avatar
0
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); double saving = read.nextDouble(); double checking = read.nextDouble(); Account savingAcc = new SavingAcc(saving); Account checkingAcc = new CheckingAcc(checking); System.out.println(savingAcc.getIncome()); System.out.println(checkingAcc.getIncome()); } } class Account { private double amount; public Account(double amount) { this.amount = amount; } public double getAmount() { return amount; } public double getIncome() { return 0; } } class SavingAcc extends Account { public SavingAcc(double amount) { super(amount); } //Override the method for saving account public double getIncome() { return amount+(amount*0.2); } }
23rd Feb 2021, 4:21 PM
Serana Zentha
Serana Zentha - avatar
0
Class CheckingAcc extends Account { public CheckingAcc(double amount) { super(amount); } //override the method for checking account public double getIncome() { return amount+(amount*0.05); } } (Ignore bracket mistakes could not fit code into one comment by copy paste)
23rd Feb 2021, 4:23 PM
Serana Zentha
Serana Zentha - avatar
0
I figured out that you can't override if the main class you're getting the information from is retricted, so it can't be private. That sucks.
23rd Feb 2021, 4:38 PM
Serana Zentha
Serana Zentha - avatar
0
Ok, thank you so much XXX nice cat btw
23rd Feb 2021, 5:52 PM
Serana Zentha
Serana Zentha - avatar
0
public class Main { public static void main(String[] args) { int a = 5; double b = 10.2; System.out.println(doubleTheValue(a)); System.out.println(doubleTheValue(b)); } //complete the method for integer value type public static int doubleTheValue(int x) { return x*2; } //overload the method for double value type public static double doubleTheValue(double y) { return y*2; } }
14th Feb 2024, 6:37 PM
S8ul Yuvraj
S8ul Yuvraj - avatar