Bluey
Bluey

Reputation: 53

Subclass of abstract class can't see constructor

Spent some time pulling my hair out here. Seems black and white, but I just can't seem to get it to work. Did a lot of digging on top of what my classes have said about abstract classes in java, but to no avail.

What I'm trying to do: as part of an assignment (so no big hints please, unless it's my IDE or something), I'm making a customer class abstract, and then going ahead and making some subclasses from that. In this way the abstract class will be instantiated by creating a subclass that utilizes the abstract classes methods/properties. With me so far?

package customer;

abstract class Customer {
    private String id;
    private String name;

    public Customer(String id, String name) {
        this.id = id;
        this.name = name;
    }

    //accessors
    public double getDiscount(double amt) {
        double discount = 0;
        return discount;
    }
    public String getID() {
        return this.id;
    }
    public String name() {
        return this.name;
    }
}

Abstract Customer class, seems all good, simple, easy. Now the solid subclass RetailCustomer

package customer;

public class RetailCustomer extends Customer {
    private double rateOfDiscount = 0.04;

    public RetailCustomer(String id, String name, double rate) {
        super(id, name);
        this.rateOfDiscount = rate;
    }
    //mutators
    public void setDiscount(double rate) {
        this.rateOfDiscount = rate;
    }
    //accessors
    public double getDiscount() {
        return this.rateOfDiscount;
    }
}

Ok. Again, very simple. RetailCustomer extends Customer abstract class, and is supposed to make use of the abstract classes constructor, as seen with

public RetailCustomer(String id, String name, double rate) {
        super(id, name);
        this.rateOfDiscount = rate;
}

However my IDE (Eclipse) is showing an error "The constructor Customer(String,String) is undefined". Even though it's clearly there in the abstract class.

Note: just copying syntax from https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

Also as an added question (which I could most likely sort out with experimenting):

The abstract Customer class implements some methods that are just simple accessors.My understanding is that unless these are instantiated in some way by retialCustomer, the object wont instantiate as all methods need implementations?

Thank you in advance for any hints or pointers you can give. Like I said, it seems simple to me, but at the same time, getting zilch :(

Upvotes: 3

Views: 200

Answers (1)

async
async

Reputation: 1537

Your code looks fine. Rebuild it (you may also want to have a look at IntelliJ IDEA ;) ). Also please follow convention and rename your class to RetailCustomer.

The abstract Customer class implements some methods that are just simple accessors.My understanding is that unless these are instantiated in some way by retialCustomer, the object wont instantiate as all methods need implementations?

You probably need to rephrase this, because its unclear what your asking. All subclasses will inherit the parent class' implementation. Even if your parent class is abstract, if you implement some of the methods in your class, then all the child classes will inherit those implementations. Indeed, you will still not be able to instantiate your abstract class, but you are able to use those methods from your child classes.

If you're happy with what the methods do in Customer, there is no need to override them in RetailCustomer.

What I'm trying to do: as part of an assignment (so no big hints please, unless it's my IDE or something)

+1.

Upvotes: 2

Related Questions