-3

I have two classes, called Customer and Transactions. When trying to to extend Customer in Transactions, i get an error. Can anyone help? This is my code

public class Customer{

    public String name;
    public String surname;
    public int id;
    public int ban;
    public double balance;


    public Customer(String pName, String pSurname, int pId, int pBan, double pBalance)
    {
        name = pName;
        surname = pSurname;
        id = pId;
        ban = pBan;
        balance = pBalance;
    }

}

public class Transactions extends Customer{

    public double Deposit(){
        double deposit = Double.parseDouble(JOptionPane.showInputDialog("How much would you like to deposit?"));
        balance = balance + deposit;
        JOptionPane.showMessageDialog(null,"Transaction complete. Your new balance is " + balance,"Transaction Complete", JOptionPane.PLAIN_MESSAGE);
        return balance;

    }

    public double Withdraw(){
        double withdraw = Double.parseDouble(JOptionPane.showInputDialog("How much would you like to withdraw?"));
        if(balance >= withdraw){
             balance = balance - withdraw;
             JOptionPane.showMessageDialog(null,"Transaction complete. Your new balance is " + balance,"Transaction Complete", JOptionPane.PLAIN_MESSAGE);
            }
        else{
            JOptionPane.showMessageDialog(null,"Error. You were trying to withdraw more than you have in your account.","ERROR", JOptionPane.ERROR_MESSAGE);
        }
        return balance;
    }

    public void checkBalance(){
        JOptionPane.showMessageDialog(null, "Name: " + name + " " + surname + "\nID: " + id + "\nBAN: " + ban + "\nBalance: " + balance);
    }
4
  • Code poorly formatted. What error are you getting? What have you tried?
    – Taylor
    Nov 10, 2014 at 14:27
  • constructor Customer in class Customer cannot be applied to given types; Nov 10, 2014 at 14:28
  • It seems that there is no constructor for Transactions and as you are extending the Customer Class which has a constructor that has some arguments. So either add a constructor for Transactions and call the Customer constructor from this.
    – Bharath
    Nov 10, 2014 at 14:29
  • stackoverflow.com/questions/525548/…
    – pomkine
    Nov 10, 2014 at 14:32

3 Answers 3

0

You should either define a constructor in Transactions class with the same parameters of the Customer constructor, or you should add a parameter-less constructor to Customer class.

When a class doesn't have any constructor (as your Transactions class doesn't), the compiler generates a default constructor with no parameters. This constructor invokes the parameter-less constructor of the super class. If the super class doesn't have a parameter-less constructor (which is not generated for your Customer class, since it already has a different constructor), your code won't compile.

0

You haven't defined a constructor for the class Transactions

It's calling the implicit constructor of the superclass that doesn't exist because you defined your own constructor

0

You broke the inheritance/encapsulation mechanism.

  1. In Java class all members must be "private".
  2. If you want some members to be accessible by the subclasses, those members must be declared "protected".
  3. In the subclass, you must declare a constructor that should call the superclass constructor via super keyword. Check the example below.

     public class Person { 
         protected String name;
         protected String age;
         public Person(String name, String age) {
             this.name = name;
             this.age = age;
         }
     }
    
    
    public class Student extends Person {
         private String Level;
         public Student(String name, String age, String level){
             super(name, age);
             this.level = level;
         }
    }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.