0
public class RentalApt extends Apartment{

 private String tenant;
 private boolean rented;

 public RentalApt(String owner, int size, boolean rented, String who){
   super(owner,size);
   tenant = who;
   this.rented = rented;
 }
 public boolean getRented(){
  return rented;
}
 public String getTenant(){
  return tenant;
}
 public void setRented(boolean isRented){
  rented = isRented;
}
 public void setTenant(String who){
  tenant= who;
}
 public String toString(){
  String s = super.toString();
  return (s + " occupied? " +  rented + " tenant: " + tenant);
}
}

Suppose you create a new PricedApt class that is derived from the RentalApt class (so it's derived from a derived class). It adds one new double attribute, price, which tells what the monthly rental for the apartment is. Here is a constructor call for the class:

PricedApt p = new PricedApt("jill", 900, true, "jack", 1050.00);

Using super appropriately, write a toString method for the class which, in addition to telling about jill as owner and jack as tenant, adds to the toString output the phrase " price: " + price. (Note: a super call to toString in PricedApt will use the toString method from its base class, RentalApt.

public class PricedApt extends RentalApt {

private double price;

public String toString() {
//code

So I know that I need to recycle the toString() method in RentalApt, but I'm getting an error with rented and tenant, as they are private. I've tried everything I know, but I haven't managed to get past that error. Here's what I have so far:

public String toString() {
String s = super.toString();
return (s + " occupied? " +  rented + " tenant: " + tenant+ " price: " + price);
 }

I've tried some things with the keyword super, but nothing successful. Sorry for this question: I know it's been answered before, but nothing I've seen from past answers solved my elementary problem.

Astro
  • 1
  • 1
  • 1
    You don't have to repeat the rented and tenant fields in PricedApt's toString, as they are already in the String that super.toString returns. Just leave them out. – Wander Nauta Mar 29 '15 at 02:00
  • You *almost* got it. Leave the other fields to the super class, just add the new stuff. `return (s + " price: " + price); That's a beauty of the inheritance. – PM 77-1 Mar 29 '15 at 02:01

2 Answers2

2

PricedApt.toString() should only reference price and RentalApt.toString().

You're on the right track, just remove rented and tenant from PricedApt.toString()

Quicksilver002
  • 735
  • 5
  • 12
  • I thought of that, but specifically didn't try it since I was sure it wouldn't work. Silly me. Thanks! – Astro Mar 29 '15 at 02:04
1

If the superclass does not provide an accessor method for the private field, this is a strong indication that your shouldn't be trying to access it in the subclass.

If these are all classes that you can change, then the correct solution may be to add an accessor method to the superclass. If you don't want the field to be exposed in the public API, make it protected or package private.

Alternatively change the subclass so that it doesn't need to access the superclass private field of the subclass. (For a toString method, you can often embed the result of the superclass toString. It depends on your requirements for formatting, etcetera.)

But if you want to ignore that advice, it is possible to access it using reflection. See the linked Q&A for the details.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216