简体   繁体   中英

class assignment error with abstract polymorphism and inheritance

public class LawClient extends Client
{
    boolean defendant;
    String caseTopic;

        LawClient (String n, boolean d)
    {
        name = n;
        defendant = d;  
    }

        LawClient (String n, boolean d, String c, String e)
    {
        name = n;
        defendant = d;
        caseTopic = c;
        email = e;
    }

    public String determineStatus()
    {
        if(caseTopic == null)
        {
            return "none";
        }
        else
        {
        String s = "";
        s += "defendant: " + defendant +"\n" + "CaseTopic: " + caseTopic;
        return s;
        }
    }
}

I get 2 errors for no sutable constructor for the lawclient constructors but don't know what I did wrong or how to fix it .

Here is super class so you can run it or look at it.

abstract class Client
{
    protected String name;
    protected double balance;
    protected String email;

    Client (String n)
    {
        name = n;
    }

    Client (String n, String e)
    {
        name = n;
        email = e;
    }


    public String getName()
    {
        return name;
    }

    public double getBalance()
    {
        return balance;
    }

    public String getEmail()
    {
        return email;
    }

    public String setName(String a)
    {
        name = a;
        return name;
    }

    public double adjustBalance(double b)
    {
        balance = b;
        return balance;
    }

    public String setEmail(String e)
    {
        email = e;
        return email;
    }

    public abstract String determineStatus();

    public String toString()
    {
        String a = "";
        a += "name: " +name + ("\n")+"Balance: " +balance + ("\n")+"Email: " +email + ("\n");
        return a;
    }
}

The problem is how constructors work in Java for inherited classes. If you do not specify a call to a constructor of the parent class, Java automatically inserts the super() method at the top of the constructor.

For the following constructor of LawClient:

LawClient (String n, boolean d)
{
    name = n;
    defendant = d;  
}

Java is making a call to super() before attempting to assign n to name, but there is no constructor that matches in your Client class.

If you add a no-args constructor to the Client class everything should work:

Client () {
    //no-args
}

Alternatively, you can call the proper super-class constructor inside of the LawClient constructor as so:

LawClient (String n, boolean d)
{
    super(n); // this will call the first constructor of the Client class
    name = n;
    defendant = d;  
}

Just invoke the Client constructor on both LawClient constructors with the appropriate arguments super()

For example

LawClient (String n, boolean d)
{  
    super(n);
    defendant = d;
}

LawClient (String n, boolean d, String c, String e)
{
    super(n, e);
    defendant = d;
    caseTopic = c;
}

When you define any constructor for a class (like you did for the Client class), the compiler does not automatically generate a default (no-argument) constructor for that class. However, when you define a constructor in a subclass and do not explicitly invoke a superclass constructor, the compiler automatically inserts a call to the default constructor of the superclass. Since Client does not have a default constructor, that's what's causing the error.

The solution is to rewrite the constructors of your LawClient class to invoke the appropriate superclass constructors:

LawClient (String n, boolean d)
{
    super(n);
    defendant = d;  
}

LawClient (String n, boolean d, String c, String e)
{
    super(n, e);
    defendant = d;
    caseTopic = c;
}

An alternative would be to explicitly define a default constructor for Client . Your current code would work but this would violate both encapsulation (since you would be initializing Client fields from the subclass constructor) as well as the DRY principle .

All classes require a constructor. The first line of any constructor must be a call to a constructor of the parent class.

To be helpful java creates a default empty constructor only if you have not specified any and it also creates a call to the the default parent class constructor if you do not specify a call so

class Parent{
}

class Child extends Parent{
}

is exactly the same as

class Parent{
    public Parent(){
        super();
    }
}

class Child extends Parent{
    public Child (){
        super();
    }
}

what your code is trying to do is

public class LawClient extends Client
{
    ....
    LawClient (String n, boolean d)
    {
        super();
        name = n;
        defendant = d;  
    }

    LawClient (String n, boolean d, String c, String e)
    {
        super();
        name = n;
        defendant = d;
        caseTopic = c;
        email = e;
    }

which is not working as

Client(){
}

is not present.

so either you need to specify a no argument constructor or call a spefic constructor in each of the LawClient constructors.

I see two things:

  1. When you define constructors with parameters in the parent class, they must be called explicitly by the child.

  2. Check the visibility of your classes and constructors. They are not all public . When there is no modifier, the it is only visible from the same package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM