+ 2

What is wrong with this code?Overriding java

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(double saving) { System.out.println(saving+saving*0.2); } } class CheckingAcc extends Account { public CheckingAcc(double amount) { super(amount); } //Override the method for checking account public double getIncome(double checking) { System.out.println(checking+checking*0.05); } }

22nd Feb 2022, 10:42 AM
Софія Тряско
3 Answers
+ 1
Hi👋 Софія, the first thing I saw, is that both getIncome (in Saving and Checking classes) have to return a double, also in getIncome take a double as an argument but you passe nothing to it, I suggest : public double getIncome() { return getAmount() + getAmount() * xxx ; }
22nd Feb 2022, 12:27 PM
Amine Laaboudi
Amine Laaboudi - avatar
0
In Java you need to add parameter list in your example your Account getIncome method: public double getIncome(){double amount){ return 0; } Java is checking the methods parameter list, and the return type, I hope its helping you
22nd Feb 2022, 12:23 PM
Domonkos Gyömörey
Domonkos Gyömörey - avatar
0
If you want parameter you need to add parameter in the parent class method
22nd Feb 2022, 12:24 PM
Domonkos Gyömörey
Domonkos Gyömörey - avatar