Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arguments to superclass

Tags:

java

Hey guy's i'm trying to use a constructor to accept a ton of variables and then pass the relevant information to the superclass constructor.

The error im getting is that when i use this.variable it's telling me to create that variable in the class, but i thought calling the super would allow me to use it this way.

public class AuctionSale extends PropertySale {

private String highestBidder;   
public AuctionSale(String saleID, String propertyAddress, int reservePrice, String    highestBidder) {
    super(saleID, propertyAddress, reservePrice);
    this.saleID = saleID;
    this.propertyAddress = propertyAddress;
    this.reservePrice = reservePrice;
    this.highestBidder = "NO BIDS PLACED";
}

As you can see i've called the superclass propertysale to get the variables.

Superclass -

public class PropertySale {
// Instance Variables
private String saleID;
private String propertyAddress;
private int reservePrice;
private int currentOffer;
private boolean saleStatus = true;

public PropertySale(String saleID, String propertyAddress, int reservePrice) {
    this.saleID = saleID;
    this.propertyAddress = propertyAddress;
    this.reservePrice = reservePrice;
}

There is a lot more additional constructors but i believe they are irrelevant right now.

like image 521
Rrr Avatar asked Jun 01 '13 03:06

Rrr


3 Answers

The reason that you get the error is because the following variables have private access in the PropertySale class:

saleID
propertyAddress
reservePrice

You cannot access them in the subclass AuctionSale unless the superclass declares them protected or public. However, in this case it is not necessary: you pass these three variables to the super constructor, so they get set in the base class. All you need in the constructor of the derived class is to call super, and then deal with variables that the derived class has declared, like this:

public AuctionSale(String saleID, String propertyAddress, int reservePrice, String    highestBidder) {
    super(saleID, propertyAddress, reservePrice);
    this.highestBidder = "NO BIDS PLACED";
}
like image 110
Sergey Kalinichenko Avatar answered Sep 29 '22 18:09

Sergey Kalinichenko


The private variables can only be accessed in the class where they are declared, no where else. Protected or public variables can be accessed in subclass.

In any way what's the use of passing the variables of the class to its own constructor?

Your saleID,propertyAddress,reservePrice are all private variables in super class. That restricts the usage.

You are however setting the variables through superclass' constructor so you do not have to set it yourself....

public class AuctionSale extends PropertySale {

private String highestBidder;   
public AuctionSale(String saleID, String propertyAddress, int reservePrice, String    highestBidder) {
    super(saleID, propertyAddress, reservePrice);//This should be sufficient
    //this.saleID = saleID;
    //this.propertyAddress = propertyAddress;
    //this.reservePrice = reservePrice;
    this.highestBidder = "NO BIDS PLACED";
}    

If you want to access private variables, best practice is to write getter and setter methods in super class and use them wherever you want.

like image 36
pinkpanther Avatar answered Sep 29 '22 19:09

pinkpanther


You have marked the variables in super class as private which means they won't be inherited.mark them as public,default,or protected and test.Private fields are only visible in the class itself.

like image 39
Sanjaya Liyanage Avatar answered Sep 29 '22 20:09

Sanjaya Liyanage