I'm getting the error cannot find symbol - constructor Customer whilst trying to set up this class.
public class PersonalCustomer extends Customer
{
public PersonalCustomer(String accountNumber, Address address, Name name)
{
super(accountNumber, address);
name = name;
}
}
Below is the class Customer.
public abstract class Customer
{
private String accountNumber;
private Address address;
private int balance;
private char customerType;
public Customer(String accountNumber, Address address, char customerType)
{
accountNumber = "";
address = address;
balance = 0;
customerType = ' ';
}
Can anyone help as too where I'm going wrong?
Customer constructor is declared with three arguments:
public Customer(String accountNumber, Address address, char customerType)
while call from child class only provides two:
super(accountNumber, address);
super(accountNumber, address, CustomerType);
Java compiler is trying to find the super() constructor with 2 arguments, which unfortunately does not exist. Bails out with an error!
Pay attention to Customer's constructor signature, it has 3 parameters.
Now, you're calling it from PersonalCustomer's constructor providing only 2.
Related
I was trying to understand how constructors work and came up with two questions. I have two classes, one for an address and another for a person. the Person class has two Address objects in it. Here is a simplified example of what I'm doing:
private class Person{
private String name;
private Address unitedStates;
private Address unitedKingdom;
Person()
{
this.name = "lary"
}
Person(String n)
{
this.name = n;
//Can I call Address(string, string) here on unitedStates and unitedKingdom?
}
}//end of person class
private class Address{
private String street;
private String country;
Address()
{
this.street = "1 Washington sq";
this.country = "United States";
}
Address(String s, String c)
{
this.street = s;
this.country = c;
}
}
}
If I leave Person() as is, will it fill the the values for unitedStates and unitedKindom with "1 Washington sq" automatically?
And
Can I pass arguments for the Address object where I left that comment in the example?
Fields of an object will always automatically be set with a default value, if not initialized by yourself. The value depends on the data type of the field (see here https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html). The default value of a field that represents an object is null.
Since you didn't initialize the fields unitedStates and unitedKingdom, their values will be null. What you can do is initializing the fields inside the Person constructors:
Person()
{
this.name = "lary";
this.unitedStates = new Address();
this.unitedKingdom = new Address();
}
Person(String n)
{
this.name = n;
this.unitedStates = new Address("myStreet", "myCountry");
this.unitedKingdom = new Address();
}
You could also use one constructor in another with the keyword this. Note that I have added a third constructor that is called by the other constructors:
Person(String n, Address unitedStates, Address unitedKingdom)
{
this.name = n;
this.unitedStates = unitedStates;
this.unitedKingdom = unitedKingdom;
}
Person(String n)
{
this(n, new Address("myStreet", "myCountry"), new Address());
}
Person()
{
this("lary", new Address(), new Address());
}
Address field are just initialized as null. you have to assign it an Address instance, in User constructor for example, like
unitedStates = new Adress();
wich will call the Address's constructor with no parameters.
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.
Do forgive me if the title is not correct, I thought this very question has to with "Polymorphism" but didn't want to complicate the title.
I am learning Java and following "Java: Learn to Program", As I am going along, I am applying the knowledge and creating my own scenarios to see
how "Polymorphism" is applied. I would appreciate it if someone can help me understand how to do this task. I have three classes:
Abstract Employee
Manager (Subclass of Employee)
Restaurant
Employee class and Manager class are pretty straight forward. I am trying to create a restaurant and every restaurant has a manager. My question is:
Should I pass "Manager" type as constructor arguments of "Restaurant" class or instantiate the "Manager" object in the constructor?
public abstract class Employee{
private String _empName;
private double _empSalary;
public Employee( string name, double salary){
_empName = name;
_empSalary = salary;
}
public void setEmpName( String name ){
_empName = name;
}
public String getEmpName(){
return _empName;
}
public void setEmpSalary( double salary ){
_empSalary = salary;
}
public double getEmpSalary(){
return _empSalary;
}
}//CLASS
public class Manager{
private double _yrsOfExp;
public Manager( String name, double salary, double experience ){
super(name, salary);
_yrsOfExp = experience;
}
public void setManagerExperience( double years ){
_yrsOfExp = years;
}
public double getManagerExperience(){
return _yrsOfExp;
}
}//CLASS
This is where I need help, I am declaring the constructor with "MANAGER TYPE". Should I be declaring the instance of "Manager" with the construction instead of
passing "Manager type" with the constructor, please?
public class Restaurant{
private Manager _manager;
private String _location;
//CONSTRUCTOR 1
//SHOULD I PURSUE IT THIS WAY OR
public Restaurant( Manager manager, String location){
_manager = manager;
_location = location;
}
//CONSTRUCTOR 2
//SHOULD I DO IT THIS WAY?
public Restaurant( String name, double salary, double experience, String location){
super(name, salary, experience);
_location = location;
}
public String toString(){
String str = "";
return str;
}
}//CLASS
This is partly a matter of taste and of what else you're going to do with the objects.
If you may ever want to refer to Managers independently, then they want to be their own object rather than properties of the Restaurant.
Since a Restaurant is not itself a Manager, I would suggest that it shouldn't take a Manager's properties in its constructor, and should instead have a Manager assigned to it (either in the constructor or in a setManager() call).
Among other things, that will make much more sense if one Manager is ever in charge of two Restaurants.
So, I'm working on a homework assignment, and I'm having a hard time following some of the directions, I've pasted the assignment below:
Create a hierarchy of five classes, plus one class included as a variable inside:
Person has four String variables: name, address, phone, email
Student is a subclass to Person and has one additional int variable status which takes values of 1, 2, 3, or 4 representing freshman, sophomore, junior, senior
MyDate has three int variables for year, month, and day
Employee is a subclass to Person and has one String variable office, one int variable for salary, and one MyDate variable for dateHired
Staff is a subclass to Employee and has one additional String variable for title
Faculty is a subclass to Employee and has one additional String variable for rank which takes values of Professor, Associate Professor, Assistant Professor, Instructor, and Adjunct. The data for all six classes should be private.
As for methods, you can skip the normal setters and getters if you write a single constructor that has parameters for all data and override the toString( ) method. Constructors of subclasses should use the super class constructor. The toString( ) methods of subclasses should use the toString( ) method of their super class.
The part that throws me for a loop is the idea that a single constructor can be written that will cover all the necessary parameters for the setters and getters instead of writing them in each sub-class. Is this possible? And how so?
You need to use the constructor of the superclass whilst creating the subclass. So it should be:
public class Staff extends Employee {
private String title;
public Staff(String name, String address, String phone, String email, int status, String title) {
super(name, address, phone, email, status);
this.title = title;
}
}
Use the super(/*params of super class*/) to invoke the constructor of the super class and instantiate the inherited attributes. Note that you can only call a superclass constructor as the first statement of a constructor. If you don't call a superclass constructor explicitly, a call to super() (the default constructor of the superclass) is inserted automatically by the Java compiler.
For calling the parent class's toString() use:
public String toString() {
return super.toString() + " ,title : " this.title;
}
Similarly write the constructors and toString() methods of all classes.
As for methods, you can skip the normal setters and getters if you write a single constructor that has parameters for all data and override the toString( ) method.
I think the directions mean that each class you write can have a single constructor that takes parameters for all of its data. Taking the MyDate constructor for example:
public MyDate(int year, int month, int day) {
...
}
And likewise override toString() to report all that information.
Instead of writing
public class A {
private int b;
private int c;
public void setB(int b) {this.b = b;}
public int getB() {return b;}
// same for c
}
you allowed to code
public class A {
private int b;
private int c;
public A(int b, int c) {
this.b = b;
this.c = c;
}
#Override
public String toString() {
return "[b = " + b + ", c = " + c + "]";
}
(The implementation of toString() is just an example, it just needs to print the states of all fields)
This is what you can do
Person(String name,String address,String phone,String email){
//Person constructor
this.name = name;
this.address = address;
this.phone = phone;
this.email = email;
}
public String toString(){
//toString method
return "Name: "+name+" Address: "+address+" Phone: "+phone+" Email: "+email;
}
I'm working on a class that inherits from another class, but I'm getting a compiler error saying "Cannot find symbol constructor Account()". Basically what I'm trying to do is make a class InvestmentAccount which extends from Account - Account is meant to hold a balance with methods for withdrawing/depositing money and InvestmentAccount is similar, but the balance is stored in shares with a share price determining how many shares are deposited or withdrawn given a particular amount of money. Here's the first few lines (around where the compiler pointed out the problem) of the subclass InvestmentAccount:
public class InvestmentAccount extends Account
{
protected int sharePrice;
protected int numShares;
private Person customer;
public InvestmentAccount(Person customer, int sharePrice)
{
this.customer = customer;
sharePrice = sharePrice;
}
// etc...
The Person class is held in another file (Person.java). Now here's the first few lines of the superclass Account:
public class Account
{
private Person customer;
protected int balanceInPence;
public Account(Person customer)
{
this.customer = customer;
balanceInPence = 0;
}
// etc...
Is there any reason why the compiler isn't just reading the symbol constructor for Account from the Account class? Or do I need to define a new constructor for Account within InvestmentAccount, which tells it to inherit everything?
Thanks
use super(customer) in InvestmentAccounts constructor.
Java can not know how to call the only constructor Account has, because it's not an empty constructor. You can omit super() only if your base class has an empty constuctor.
Change
public InvestmentAccount(Person customer, int sharePrice)
{
this.customer = customer;
sharePrice = sharePrice;
}
to
public InvestmentAccount(Person customer, int sharePrice)
{
super(customer);
sharePrice = sharePrice;
}
that will work.
You have to invoke the superclass constructor, otherwise Java won't know what constructor you are calling to build the superclass on the subclass.
public class InvestmentAccount extends Account {
protected int sharePrice;
protected int numShares;
private Person customer;
public InvestmentAccount(Person customer, int sharePrice) {
super(customer);
this.customer = customer;
sharePrice = sharePrice;
}
}
Call the super() method. If you want to call the Account(Person) constructor, use the statement super(customer); Also this should be the first statment in your InvestmentAccount
constructor
You have to call the constructor of the base class explicitly if the base class doesn't have a default constructor (one with no arguments).
In your case, the constructor should be:
public InvestmentAccount(Person customer, int sharePrice) {
super(customer);
sharePrice = sharePrice;
}
And don't redefine customer as an instance variable of the subclass!
Either define a default constructor in the Account class:
public Account() {}
Or call super(customer) in the InvestmentAccount constructor.