I have an abstract class Customer. It's a very simple class, only setting 5 string variables as well as 5 static int variables. Better to show what I mean by this:

As a disclaimer I made the code as simple as possible, I have more logic involved in my abstract class that doesn't pertain to the question.


Abstract Class

public abstract class Customer {
     private String Name, Address, Phone, Email, Company;
     public static final int NAME = 0, ADDRESS = 1, PHONE = 2, EMAIL = 3, COMPANY = 4;

     public Customer(String Name, String Address, String Phone, String Email, String Company) {
         setValues(Name, Address, Phone, Email, Company);
     }

     private void setValues(String Name, String Address, String Phone, String Email, String Company) {
         setName(Name);
         setAddress(Address);
         setPhone(Phone);
         setEmail(Email);
         setCompany(Company);
     }
//declare public getters and setters methods below
}

My question is as follows:

I have a class that extends this abstract class called Customer (different package). If I set up the constructor in this class as such:


Object Class

public class Customer extends Main.Customer {
     private String Name, Address, Phone, Email, Company;

     public Customer(String Name, String Address, String Phone, String Email, String Company) {
         super(Name, Address, Phone, Email, Company);
     }    
}

Does this set my String variables as to whatever I pass through the constructor? As in when I instantiate this class as an object, how would I be able to 'get' a variable from it?
For example: (Assume String1 -String5 are strings of some sort)

public class Random {
private Customer customer = new Customer(String1, String2, String3, String4, String5);

}

How would I then call the object later on in the class to return a string (of any single variable). As in if my abstract class wasn't abstract but the main class I was using to instantiate as an object, I'd get the variable like so: String name = customer.getName();


TL;DR:

Just unsure how to get variables from an object extending an abstract class.

有帮助吗?

解决方案

Drop the variables from your subclass so they don't shadow the variables with the same name in the parent class.

//sub class
public class Customer extends Main.Customer {
 //DROP THESE private String Name, Address, Phone, Email, Company;

    public Customer(String Name, String Address, String Phone, String Email, String Company) {
        super(Name, Address, Phone, Email, Company);
     }    
}

And add getters to your parent class:

//parent class
public abstract class Customer {
 private String Name, Address, Phone, Email, Company;
 public static final int NAME = 0, ADDRESS = 1, PHONE = 2, EMAIL = 3, COMPANY = 4;

 public Customer(String Name, String Address, String Phone, String Email, String Company) {
     setValues(Name, Address, Phone, Email, Company);
 }

 private void setValues(String Name, String Address, String Phone, String Email, String Company) {
     setName(Name);
     setAddress(Address);
     setPhone(Phone);
     setEmail(Email);
     setCompany(Company);
 }

 public String getName() {
   return Name;
 }

 public String getAddress() {
   return Address;
 }

 //etc....

}

Also, I really recommend using different names for your parent and subclass to avoid confusion.

其他提示

Some considerations before start:

  • In java by code-convention variables starts with lower-case. It will help code readability for people including you.
  • Don't have two classes with the same name, is very confusing. You can call it for example ACustomer or AbstractCustomer and the other one Customer or SomethingCustomer

  • It isn't Object class it's Concrete Class a class that you can have instances of it.

  • As Customer inherits ACustomer you don't have to define again the ACustomer fields, Customer already has them. If you do you are hiding those from parent.


public class Customer extends ACustomer {

         public Customer(String name, String address, String phone, String email, String company) {
             super(name, address, phone, email, company);
         }    
    }
  • You are calling an overrideable method inside the constructor take care about that, cause if setXXX is override then perhaps you could have a NullPointerException.
  • For your question in how to get member you can define getters.

public abstract class ACostumer{
      private String name;


     public String getName() {
       return name;
     }

 }

Then in client code:

ACustomer customer = new Customer(...);
customer.getName(); 

Your subclass is overshadowing the private properties of the abstract class.

public abstract class Customer {
    private String Name, Address, Phone, Email, Company;

public class Customer extends Main.Customer {
    private String Name, Address, Phone, Email, Company;

so any get methods in your abstract class of the form

public String getName() {
    return Name;
}

would return the never initialized variable name in the subclass. Whereas, when you call super(...), the set functions there would set the variables of the abstract class.

So you are setting one set of variables, but reading another set of variables that were never initialized.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top