0

I have two classes, "Quote" (superclass) and "RealQuote" (subclass). "Quote" has a following features:

// Attribute
private String sentence;

// Constructor
public Quote(String aSentence) {
   sentence(aSentence);

// Accessors
public String sentence() {
   return sentence;
}
public void sentence(String newSentence) {
   sentence = newSentence;
}

I need to utilize the constructor in Quote to create a constructor in subclass RealQuote that has attributes: String sentence, String speaker.

As the attribute for the speaker is not included in the superclass, I wonder how can I utilize it to create a constructor with different number of parameters? I assume I will have to call the existing constructor somewhere, but how can I add more parameters to it in the subclass?

I tried this:

public RealQuote(String aSentence, String aSpeaker) {
   super();
   speaker(aSpeaker);
}

However, obviously it does not work, because the called superclass constructor does not have enough arguments, but I don't know what to add there or even if anything I have this far is correct.

Angmar
  • 195
  • 6
  • Yeah you can call super with parameters. Try it out :) – GamingFelix Feb 14 '20 at 14:04
  • I tried, but it doesn't let me because the sentence attribute is private (and has to remain so). The link to the previous question does not seem to answer to my question, as when I try to follow the answer, I still get an error stating: "sentence has private access in Quote". I tried adding the accessor in its place, but I get a following error: "cannot reference this before supertype constructor has been called". – Angmar Feb 14 '20 at 14:14
  • So to understand, you call super(aSpeaker) and it's not working? – GamingFelix Feb 14 '20 at 14:19
  • If you just want to set/create two new variable in the subclass you can literally set them in the constructor too, without calling the super class. public RealQuote(String aSentence, String aSpeaker) { super(); this.aSentence = aSentence, this.aSpeaker = aSpeaker } (Ofc u gotta declare aSpeaker and aSentence as variables inside the subclass too) – GamingFelix Feb 14 '20 at 14:25
  • No, speaker is not in the superclass. I call super(sentence) and it says that it is private (as it is). And if I call super(sentence()), it says that the supertype constructor has to be called first (?). My instructions do not allow me to declare an attribute for the sentence in the subclass, because I have to utilize the superclass constructor. – Angmar Feb 14 '20 at 14:31
  • Yes of course you can not reach a variable that is in the super class in the subclass. Why do you think that you need to access the sentence variable? In your subclass, the two variables you have are: String aSentence, String aSpeaker. Shouldn't those be that you want to do something with / use in the subclass? – GamingFelix Feb 14 '20 at 14:38
  • I thought I needed to access the sentence variable, because that is how the correct solution is presented in the answer to the previous question (the one that was linked). But apparently not, as now I tested super(aSentence) and seemed to work. – Angmar Feb 14 '20 at 14:50
  • Glad I could help @Angmar also once you have set it. Because it's a subclass. You'll actually be able to reach it by the superclass getter/access method. But since you're working in the subclass, there is technically no need for you to use the superclass like that. Maybe there would be some reason, like maybe you want to set/use that variable that way. But you could just use the subclass only as well. – GamingFelix Feb 14 '20 at 14:53
  • Also, I'd really suggest you look more into constructors and how they work. I like to think of them just as another way of Setting variables. Have a good day! @Angmar – GamingFelix Feb 14 '20 at 14:55

0 Answers0