I googled alot and the problem gives me a Headache because nobody can help me with it on Discord. Now I am here for help.
Thats the Class I use to make the Object
public class Main {
public static void main(String[] args) {
Konto mats = new Konto("Mats", 200);
mats.getAccount();
}
}
That is the class of it:
public class Konto extends Bankautomat{
String name;
int balance;
String prefix = "[Deutsche Bank]";
public Konto(String name, int balance){
this.name = name;
this.balance = balance;
System.out.println("New Account successfully registered!");
}
public void getAccount(){
System.out.printf("\n%s\nName: %s\nBalance: %d$\n", prefix, name, balance);
}
}
And now I want to make a Bankautomat class where I can deposit money on the Konto from "Mats"
public class Bankautomat {
public void deposit(int amount){
}
}
But I cant use mats.balance += amount
and the set or get things also dont help me
please help thank you <3
The way you have it set up, Konto extends Bankautomat right now. In other words, all Kontos are Bankautomats but the reverse is not the case. Since you only declare the field balance in the Konto class, this means it's a property of Kontos but not necessarily of all Bankautomats, so the Bankautomat object doesn't have this unless it's specifically a Konto.
To be able to access it in Bankautomat you should move the field to the Bankautomat class. Then, you'll be able to access it from both, since Konto is a Bankautomat and will inherit it. Something like this:
public class Konto extends Bankautomat{
String name;
String prefix = "[Deutsche Bank]";
public Konto(String name, int balance){
super(balance); // this calls the constructor of the superclass
this.name = name;
System.out.println("New Account successfully registered!");
}
public void getAccount(){
System.out.printf("\n%s\nName: %s\nBalance: %d$\n", this.prefix, this.name, this.balance);
}
}
and the other class:
public class Bankautomat {
int balance;
public Bankautomat(int balance) {
this.balance = balance;
}
public void deposit(int amount){
...
}
}
Related
this might be something really easy to address but I am really new to Java and I am having a problem fixing this error. I am working on a basic ATM Machine (I can't go fancy just yet). I have a BankAccount class, a Checking Account (extends bankAccount), A savingsAccount class (also extends bankAccount) and a User class. In my CheckingAccount class, I want to use one of the parameters from my SUPER class (bankAccount), "balance" but I am not sure if there is a way to only use ONE parameter from a constructor that has more than One parameter. If so, any clue for how to? I am getting an Error : "no suitable constructor found for BankAccount(double)..." . This is my code so far, I appreciate your help.
-- BankAccount Class --
package atm;
public class BankAccount {
public double balance;
public int accountNumber = 333220;
public User user;
public BankAccount(){
balance = 0;
}
public BankAccount(double balance, int accountNumber, User user){
this.balance = balance;
this.accountNumber = accountNumber;
this.user = user;
}
public void accountNumber(){
accountNumber++;
}
public int getAccountNumber(){
return accountNumber;
}
public void deposit (double amount){
balance += amount;
}
public void withDraw(double amount) {
balance -= amount;
}
public double getBalance (){
return balance;
}
public void transfer( BankAccount other,double amount){
withDraw(amount);
other.deposit(amount);
}
}
-- CheckingAccount Class --
package atm;
public class CheckingAccount extends BankAccount {
private int transactionCount;
private static final double transactionFees = 1.50;
private static final int freeTransactions = 5;
public CheckingAccount (double balance){
super(balance); //HERE is where I am confused. I only need to use the "balance"
transactionCount = 0;
}
public void deposit(double amount){
transactionCount ++;
super.deposit(amount);
}
public void withDraw(double amount){
transactionCount ++;
super.withDraw(amount);
}
public void deductFees (){
if (transactionCount > freeTransactions){
double fees = transactionFees * (transactionCount - freeTransactions);
super.withDraw(fees);
}
transactionCount = 0;
}
}
Thanks a lot!
You need to create another constructor in BankAccount.java that only initializes amount like below
public BankAccount(double balance){
this.balance = balance;
}
Read about having multiple constructors in Java here
http://tutorials.jenkov.com/java/constructors.html
Since CheckingAccount is a BankAccount, it must have all the attributes of the BankAccount, including the account number and the User who owns it.
The base class requires accountNumber and user to be supplied by the derived class, so you should change the constructor to forward this information to super:
public CheckingAccount (double balance, int accountNumber, User user){
super(balance, accountNumber, user);
transactionCount = 0;
}
Note 1: In certain situations a derived class could hard-code parameters to be passed to super; this is not one of these situations.
Note 2: Consider making BankAccount abstract, and changing accessibility of its constructor from public to protected.
I have a problem with my program and I don't know why the names and the IDs of the objects don't show up right in the output and also the balance is not used in chkBalance() method Why ??I think the problem with the constructor but? ... and the output I get from this code is ...:
Customer Name Customer ID Balance
null 0.0 200
null 0.0 200
null 0.0 200
public class BankAccount {
private int Balance;
public String AccHolder_Name;
public double AccHolder_ID;
public BankAccount(String Name ,double ID,int Balance) {
Name = AccHolder_Name;
ID = AccHolder_ID;
}
public void withDraw(int a) {
Balance=a-Balance;
}
public void deposit(int b) {
Balance=b+Balance;
}
public int chkBalance(){
deposit(500);
withDraw(700);
return Balance;
}
public void Display () {
System.out.println(AccHolder_Name+"\t\t"+AccHolder_ID+"\t\t"+Balance);
}
public static void main (String args []) {
BankAccount o1=new BankAccount("Aos" , 1101558733 ,3000);
BankAccount o2=new BankAccount("Ahmed" , 1101978733,5000);
BankAccount o3=new BankAccount("Ali",1111650924,7000);
System.out.println("Customer Name\tCustomer ID\tBalance");
o1.chkBalance();
o2.chkBalance();
o3.chkBalance();
o1.Display();
o2.Display();
o3.Display();
}
}
In your constructor you are assigning null to the given parameters and never assign Balance
public BankAccount(String Name ,double ID,int Balance) {
Name = AccHolder_Name; // Will assign #AccHolder_Name to #Name
ID = AccHolder_ID;
}
So you should reverse this assignment and set the Balance field with the given parameter :
public BankAccount(String Name ,double ID,int Balance) {
AccHolder_Name = Name; // Will assign #Name to #AccHolder_Name
AccHolder_ID = ID;
this.Balance = Balance;
}
And about your variables names you should take a look to the naming convention.
The variables should use camel case and for good practice be well representative of their function.
So AccHolder_Name should be accHolderName for example.
Your assignments in the constructor is the wrong way arround. Also, you forgot to assign the Balance argument, which causes Balance to default to 0 and results in 200 after the calculations.
public BankAccount(String Name, double ID, int Balance) {
this.AccHolder_Name = Name;
this.AccHolder_ID = ID;
this.Balance = Balance;
}
An important note on your naming conventions: variables in Java use camel case. It is also discouraged to abbreviate variable names unless they are considered an abbreviation standard. The resulting code would then be:
public BankAccount(String name, double id, int balance) {
this.accountHolderName = name;
this.accountHolderId = id;
this.balance = balance;
}
The constructor of the BankAccount is indeed wrong. Since what '=' operation does is to give the value of the right variable to the left variable, and your code is reversed.
Another problem is that you don't initialize your Balance in your constructor and the compiler will then simply initialize it to 0.
Constructor has issue. Copy bellow code and replace your constructor and check.
public BankAccount(String Name ,double ID,int Balance) {
this.AccHolder_Name = Name;
this.AccHolder_ID = ID;
this.Balance = Balance;
}
My suggestion is to learn java more. Basic you can learn from Java Tutorial.
In the constructor you are assigning it in the other way. Assign it as below
public BankAccount(String Name ,double ID,int Balance) {
this.AccHolder_Name = Name;
this.AccHolder_ID = ID;
this.Balance = Balance;
}
There is always the variable you want to set on the left side and the value for it on the rigt side. As you want to set the String AccHolder_Name to the Name you are giving in the constructor, you have to do it just the other way around.
There are several mistakes:
a. Your constructor is incorrect and not saving the provided
variables.
this.AccHolder_Name=Name ;
this.AccHolder_ID=ID;
this.Balance=Balance;
b. Your withdraw method is incorrect. It should be (balance =
balance-a)
Please see the correct code below.
public class BankAccount {
private int Balance ;
public String AccHolder_Name;
public double AccHolder_ID;
public BankAccount(String Name ,double ID,int Balance) {
this.AccHolder_Name=Name ;
this.AccHolder_ID=ID;
this.Balance=Balance;
}
public void withDraw(int a) {
Balance=Balance-a;
}
public void deposit(int b) {
Balance=b+Balance;
}
public int chkBalance(){
deposit(500);
withDraw(700);
return Balance;
}
public void Display () {
System.out.println(AccHolder_Name+"\t\t"+AccHolder_ID+"\t\t"+Balance);
}
public static void main (String args []) {
BankAccount o1=new BankAccount("Aos" , 1101558733 ,3000);
BankAccount o2=new BankAccount("Ahmed" , 1101978733,5000);
BankAccount o3=new BankAccount("Ali",1111650924,7000);
System.out.println("Customer Name\tCustomer ID\tBalance");
o1.chkBalance();
o2.chkBalance();
o3.chkBalance();
o1.Display();
o2.Display();
o3.Display();
}
}
I want to fill values in my arraylist of abstract class type.Below is my code
public abstract class Account {
private int accountId;
private int customerId;
private double balance;
public Account(int accountId, int customerId, double balance) {
this.accountId = accountId;
this.customerId = customerId;
this.balance = balance;
}
public abstract double deposit(double sum);
public abstract double withdraw(double sum);
}
Above is my abstract class.
Now I have another class bank in which i want to define and declare an arraylist in which i can fill my values .
I declared arraylist as
ArrayList<Account> al=new ArrayList<>();
Now i want to pass values to this arraylist for further use but i couldnot as we cannot instantiate abstract class.I tried this code to fill values in class with main method but couldnot get it because of above reason
Account ac= new Account(1,100,25000);
ArrayList<Account>ac= new ArrayList<Account>();
ac.add(ac);
The whole point of your abstract class is to factorize some code in your application. Using it as a super type is in my opinion a bad practice since you should be using interfaces for that.
To get a complete response to your problem, I would:
Create an interface: Account.java
public interface Account {
public double deposit(double sum);
public double withdraw(double sum);
}
Create an abstract class: AbstractAccount.java
public abstract class AbstractAccount {
protected int accountId;
protected int customerId;
protected double balance;
public Account(int accountId, int customerId, double balance) {
this.accountId = accountId;
this.customerId = customerId;
this.balance = balance;
}
}
And finally provide a default implementation for your interface BankAccount.java
public class BankAccount extends AbstractAccount implements Account {
public Account(int accountId, int customerId, double balance) {
super(accountId, customerId, balance);
}
public double deposit(double sum) {
this.balance += sum;
}
public double withdraw(double sum) {
this.balance -= sum;
}
}
Then you should manipulate:
List<Account> accounts = new ArrayList<Account>();
accounts.add(new BankAccount(1, 1, 10000));
and never care about the implementing type :)
Abstract classes can't be instantiated, but they can be extended. If the child class is concrete, it can be instantiated.
You would also have to implement both your abstract methods to make the class concrete.
Read more here: Java inheritance.
You can add this following code just to get you started :
public class ConcreteAccount extends Account{
public ConcreteAccount (int accountId, int customerId, double balance) {
super(accountId, customerId, balance);
}
public abstract double deposit(double sum) {
//implementation here
}
public abstract double withdraw(double sum) {
//implementation here
}
}
Then after that, you can have :
Account ac= new ConcreteAccount(1,100,25000);
ArrayList<Account> acList= new ArrayList<Account>();
acList.add(ac);
Marking a class abstract means it might have unimplemented methods, therefore, you're not able to create an instance of an abstract class directly due to undefined behavior. What you can do is to define a non-abstract class which extends your Account class and implement the two abstract methods in Account. Something like class BankAccount extends Account { implementations }. After that, you can create instances of class BankAccount and add them into your ArrayList instance. Other instances of class that extends Account can also be added into your ArrayList instance.
The book I am reading says I cant, but my program proves otherwise. For example the code below compiles well, even though i try to access the private properties of the parent class. Then I can freely print them. Can anyone tell me if the book is wrong, or am I doing something wrong?
class Asset
{
private int Id;
private String type;
public int getId()
{
return Id;
}
public String getType()
{
return type;
}
public void setId(int Id)
{
this.Id=Id;
}
public void setType(String type)
{
this.type=type;
}
public void printDescription()
{
System.out.println("Asset Id: "+Id);
System.out.println("Asst type: "+ type);
}
}
class BankAccount extends Asset
{
private String bankName;
private int accountNumber;
private float balance;
public String getBankName()
{
return bankName;
}
public int getAccountNumber()
{
return accountNumber;
}
public float getBalance()
{
return balance;
}
public void setBankName(String bankName)
{
this.bankName=bankName;
}
public void setAccountNumber(int accountNumber)
{
this.accountNumber=accountNumber;
}
public void setBalance(float balance)
{
this.balance=balance;
}
public void printDescriptionnn()
{
System.out.println("The Bank name is: "+ bankName);
System.out.println("Account number: "+ accountNumber);
System.out.println("Your balance is: "+ balance);
}
}
public class AssetTest
{
public static void main(String[] args)
{
BankAccount llogari= new BankAccount();
Scanner input = new Scanner(System.in);
Scanner sinput= new Scanner(System.in);
System.out.print("Type the ID of your asset: ");
llogari.setId(input.nextInt());
System.out.print("Type the type of your asset: ");
llogari.setType(sinput.nextLine());
System.out.print("Give the bank name: ");
llogari.setBankName(sinput.nextLine());
System.out.print("Type the Account Number: ");
llogari.setAccountNumber(input.nextInt());
System.out.print("Type your balance: ");
llogari.setBalance(input.nextFloat());
llogari.printDescription();
llogari.printDescriptionnn();
}
}`
You can access them through public or protected getters but you can't access the private properties directly. In your example, you're using the public setters to modify the property. You can access them through public method !
So to answer you question, private members are not inherited by subclasses. Alternatively, you can have protected members that are inherited by subclasses.
EDIT
From Java Language Specificiation
Members of a class that are declared private are not inherited by subclasses of that class.
Only members of a class that are declared protected or public are inherited by subclasses declared in a package other than the one in which the class is declared.
Because you're not directly modifying the parent class's elements. You're calling public functions that modify the private elements, which is completely valid.
A subclass does not have direct access to the private members of a super class. It only has direct access to the public and protected members.
In this context, direct access means: super.member
If the super class implements protected or public accessor or mutator methods, then you may be able to indirectly access them. Indirect access would look something like: super.getMember() or super.doSomething().
Any subclass does not have permission to direct access of the private members of a super class. It can access to the public and protected members.
When I try inheritance an error comes up "CheckingAccount is already defined in null". What does this mean and how can I fix this?
This is the code that I have:
public class BankAccount
{
double balance;
public void Bankaccount() {
balance = 0;
}
public BankAccount(double initialBalance) {
initialBalance = balance;
}
public void deposite(double amount) {
balance = balance + amount;
}
public void withdraw(double amount) {
balance = balance - amount;
}
public double getBalance(double balance) {
return balance;
}
public void transfer(double amount, double bankAccountOther) {
balance = balance - amount;
}
}
public class CheckingAccount
{
public class CheckingAccount extends BankAccount {
}
}
Could someone please help me out here, I really don't understand what it means
You have the same class defined inside itself, this is the problem:
public class CheckingAccount {
public class CheckingAccount extends BankAccount{
}
}
The inner class should be named differently. Or maybe you intended the outer class to extend BankAccount? it's not clear what you were trying to implement, but maybe this is what you were aiming for:
public class CheckingAccount extends BankAccount{
}
The answer was already successfully given plenty of times, but I would add an unrelated remark, concerning this piece of code:
//constructor
public void Bankaccount()
{
balance = 0;
}
This is not a constructor. If you want it to be a constructor, you must delete void
You should write only -
public class CheckingAccount extends BankAccount{
}
and not
public class CheckingAccount {
public class CheckingAccount extends BankAccount{
}
}
Replace the below :
public class CheckingAccount {
public class CheckingAccount extends BankAccount{
}
}
By this :
public class CheckingAccount extends BankAccount{
}
Your error is most likely right here:
public class CheckingAccount {
public class CheckingAccount extends BankAccount{
}
}
You're defining a CheckingAccount class within an already existent CheckingAccount class. I don't think you can hide an enclosed type.
1 class CheckingAccount {
2 public class CheckingAccount extends BankAccount {
3
4 }
5 }
remove 1st and 5 line above and add no-argument constructor in Super class
Another side note that isn't really an answer:
public BankAccount(double initialBalance) {
initialBalance = balance;
}
should be
public BankAccount(double initialBalance) {
balance = initialBalance;
}
as the former accomplishes nothing.