1

I am writing a program in Java and I am facing this problem.

I have made an abstract superclass Customer and a subclass RegisteredCustomer and of course the main class. I cannot find a way of using the constructor of the RegisteredCustomer in the main.

The message The method RegisteredCustomer(String, long, String, String) is undefined for the type RegisteredCustomer even though I have made the exact constructor with these parameters in the RegisteredCustomer.

I have tried RegisteredCustomer.RegisteredCustomer(fn , tel , adr , em); and Customer.RegisteredCustomer.RegisteredCustomer(fn , tel , adr , em);

REGISTEREDCUSTOMER

public class RegisteredCustomer extends Customer {

    private static int count = 0;

    private int id;
    private String email;
    private String  password;

    public RegisteredCustomer(String fullName, long telephone, String adress, String email) {
        super(fullName, telephone, adress);
        this.id = ++ count;
        this.email = email;
        Customer.getCustomers().add(Customer.getCustomers().size() , this);
    }

MAIN

RegisteredCustomer.RegisteredCustomer(fn , tel , adr , em);

zappee
  • 20,148
  • 14
  • 73
  • 129
Vicky K.
  • 7
  • 3
  • 1
    I'm not sure if your code is correct, but it would be `RegisteredCustomer rc = new RegisteredCustomer(fn, tel, adr, em);` As an aside, generally don't store phone numbers as a long. – KevinO May 20 '19 at 19:42
  • how do you propose i store phone numbers ? – Vicky K. May 20 '19 at 21:29
  • Phone numbers are a String. How are you going to store "0118 960 194" as a long? See, e.g., [What's the right way to represent phone numbers?](https://stackoverflow.com/questions/3483156/whats-the-right-way-to-represent-phone-numbers) – KevinO May 20 '19 at 22:18

3 Answers3

3

With RegisteredCustomer.RegisteredCustomer(fn , tel , adr , em); you're trying to call the static method RegisteredCustomer of the class RegisteredCustomer , which does not exist hence it tells you that the method is undefined.

The code below is an example of the method which you're trying to call.

public class RegisteredCustomer {

    ...

    public static void RegisteredCustomer(String fullName, long telephone,
            String adress, String email) {
        ...
    }
}

The correct way of creating an instance of RegisteredCustomer would be to call:

new RegisteredCustomer(fn , tel , adr , em);
Mark
  • 5,089
  • 2
  • 20
  • 31
1

I'm not sure, but try to create a demo class and write there:

RegisteredCustomer rc = new RegisteredCustomer(fn, tel, adr, em);

And then you can change your object there.

Jan Mares
  • 795
  • 10
  • 22
Ronin
  • 11
  • 2
0

There seems to be a syntax error in the way you are initializing an Object in java. The following is one way to write it,

public class TestClass {
    public static void main(String[] args) {
        RgisteredCustomer rc = new RgisteredCustomer("John D", 9175556671L, "NYC", "john.d@gmail.com"); //This is how the base and super class constructors are called
        System.out.println(rc);
    }
}

class Customer {
    private String fullName;
    long telephone;
    String address;
    Customer(String fullName, long telephone, String address) {
        this.fullName = fullName;
        this.telephone = telephone;
        this.address = address;
    }
    public String toString() {
        return fullName + " " + telephone + " " + address;
    }
}

class RgisteredCustomer extends Customer {
    private static int count = 0;
    private int id;
    private String email;
    private String  password;

    public RgisteredCustomer(String fullName, long telephone, String adress, String email) {
        super(fullName, telephone, adress);
        this.id = ++ count;
        this.email = email;
        //Customer.getCustomers().add(Customer.getCustomers().size() , this);
    }
    public String toString() {
        return super.toString() + " " + email + " " + password;
    }
}

In Java this RegisteredCustomer.RegisteredCustomer(fn , tel , adr , em)/Customer.RegisteredCustomer.RegisteredCustomer(fn , tel , adr , em) will mean calling a static method.

Dwaraka
  • 179
  • 8