1

I have this problem :

I am trying to compile this :

public class Agora extends CreditCardPayment {

public void run() {

CashPayment ad1 = new CashPayment(12f);
ad1.paymentDetails();

CashPayment ad2 = new CashPayment(156.4f);
ad2.paymentDetails();

int cardnum1 = 1253462136;
CreditCardPayment ad3 = new CreditCardPayment(325.99f , "Visa" , "12/1/2015" , cardnum1);
ad3.paymentDetails();

int cardnum2 = 1235623151;
CreditCardPayment ad4 = new CreditCardPayment(999.99f , "Master" , "25/6/2016" , cardnum2);
ad4.paymentDetails();
 }
}

but it keeps giving me this error :

Agora.java:3: error: constructor CreditCardPayment in class CreditCardPayment cannot be applied to given types; public class Agora extends CreditCardPayment {

required: float,String,String,int found: no arguments reason: actual and formal argument lists differ in length

And this is the CreditCardPayment class :

public class CreditCardPayment extends Payment{

public CreditCardPayment(float x ,String name , String exp_date , int card_num) {
 super (x);
 Card_name = name;
 Expiration_date = exp_date;
 Card_number = card_num;
 }

public String paymentDetails() {
  return super.paymentDetails() + "Card's name :" + Card_name + " , Expiration Date :" + Expiration_date + " , Card Number :" + Card_number ;
  }

  private String Card_name;
  private String Expiration_date;
  private int Card_number;
  }

I am a computer science student and don't know much yet so please excuse me if this is just a silly mistake but I can't find it. Any help will be appreciated !

2
  • 2
    Try recompiling the classes. Commented Jan 9, 2015 at 20:07
  • 3
    Inheritance is a is-a relationship. When you create a Agora it also must create a CreditCardPayment. Since you need to pass arguments to the CreditCardPayment constructor, you need to use super() similar to how it is used in CreditCardPayment Commented Jan 9, 2015 at 20:08

2 Answers 2

1

Building on @clcto's comment: Java provides a no-argument default constructor for classes unless you specify one or more other constructors. You have provided constructors for CreditCardPayment, so there is no no-arg constructor for this class.

You have not provided constructors for Agora, so the default behaviour is to assume a no-arg constructor, as if you had written

public Agora() {
    super();
}

But there is no no-arg constructor for the superclass (CreditCardPayment), so this fails.

Sign up to request clarification or add additional context in comments.

Comments

0

For some reason you are creating your Agora class as a subclass of CreditCardPayment. Because you haven't supplied a constructor for Agora, the default constructor is supplied by the compiler, which attempts to call the default constructor for the superclass, CreditCardPayment(). But no such constructor exists; the only constructor for CreditCardPayment takes 4 parameters. That explains the compiler error.

Your Agora class seems to use CreditCardPayment; it is not itself a CreditCardPayment. The relationship is "uses", not "is a". Agora uses CreditCardPayment, but Agora isn't a CreditCardPayment.

I would remove the extends CreditCardPayment clause on your Agora class definition; it's not needed and it shouldn't be there.

public class Agora {

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.