class already defined in null - java

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.

Related

How can I change the variable of my Object?

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){
...
}
}

Should protected fields in an Abstract super class be accessed using super or this in sub-classes?

Suppose I have the following Abstract class.
public abstract class Account {
protected String Id;
protected double balance;
public Account(String Id, double balance) {
this.Id = Id;
this.balance = balance;
}
}
And the following subclass
public class CheckingAccount {
public CheckingAccount(String Id, double balance) {
super(Id, balance)
if(super.balance > 10_000) this.balance += 200;
}
}
Both 'this' and 'super' are allowed in the context of the subclass when accessing a protected member. Is it better to use one over the other? 'super' makes it explicit where that field comes from. I understand I can just use balance without specifying the implicit parameter but I was just curious in terms of how this is utilized in practice if one wanted to specify the implicit parameter.
Since CheckingAccount inherits protected field balance from Account so it DOES NOT matter using super or this keyword to access the field balance in CheckingAccount class. However, I prefer 'this'.
If there is a protected method in Account class (base class) and there is a its overridden method in CheckingAccount class, you will have to carefully use super or this in this case because they are not the same body implementation!
I'd argue you shouldn't use any protected fields, to enforce encapsulation. Provide a protected void addToBalance(double value) method would be the cleaner way.
I was just curious in terms of how this is utilized in practice if one wanted to specify the implicit parameter
For somewhat academic reasons, here's where it makes a difference:
public abstract class Account {
protected String Id;
protected double balance;
public Account(String Id, double balance) {
this.Id = Id;
this.balance = balance;
}
}
public class CheckingAccount {
// overwrite existing field
protected double balance;
public CheckingAccount(String Id, double balance) {
super(Id, balance);
this.balance = balance;
if(super.balance > 10_000) this.balance += 200;
}
}

Using a single parameter from a Super class where the super class uses more parameters

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.

Initialize ArrayList of Type Abstract class

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.

Does a subclass inherit the private properties of its parent class, in java?

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.

Categories