Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (javk):

When I extend a class in java how can I add more objects to it?

OpenStudy (anonymous):

Do you mean add members? because for that you just define them as part of the extending class, like members of any class. Objects of the extending class will have both the parent's members and the members of the extended class. If not, then I don't get it.. what do you mean adding objects to a class? objects are instances of the class, what does it mean to 'add' them to the class itself? What are you trying to do?

OpenStudy (javk):

I'm sorry I'm a bit confused and didn't know what word to use. I have a class for a letter template and need to extend that class and add a body to the letter. I can't figure out where to put the body.

OpenStudy (anonymous):

I see. Ok let me get this right, you have a class for a letter and every letter has a body, right? Then you want to have templates for letters that come with predefined values for the body? In that case your extending class should be the template and not the letter. The letter should be the parent class containing a member holding the body, and the template should extend it and fill the body with a specific value in its constructor.

OpenStudy (anonymous):

It is essential that you understand polymorphism when working with stuff like that. Read this: http://howtodoinjava.com/2013/07/15/what-is-polymorphism-in-java/ Notice that both `a1` and `a2` are Animal type variables, and yet the print different results. It is important to understand it. Then you can treat objects of your Template classes as if they were Letter objects and change the functionality of the letter objects by overriding methods

OpenStudy (javk):

yeah I understand all that. I extended the letter template class to produce a letter I just have no clue as to what goes in the main method

OpenStudy (anonymous):

I removed all the temporary messages I wrote.. I keep making small mistakes.. so say we have ```cpp class Letter { public String body = ""; } class LetterTemplate extends Letter { public LetterTemplate(){ this.body = "something here"; } } public class MyProgram { public static void main(String [] args) { Letter my_letter = new LetterTemplate(); // Do something with my_letter } } ``` So, what's the problem now?

OpenStudy (anonymous):

Sorry about that lol, I woke up and my head has slow start hehe

OpenStudy (javk):

np, I'm fully awake and still can't seem to be functioning fine. shouldn't it be the Letter extends LetterTemplate?

OpenStudy (anonymous):

no, that's my point. just like Animal shouldn't extending Dog, but the opposite. The reason is that if you think about a Dog, it is first of all a Dog that's why it has its own class, but then it's also an Animal. An Animal is not a Dog necessarily, it could be many things. In your case a TemplateLetter is also a Letter. It is a specific kind of Letter. So it extends Letter.

OpenStudy (javk):

No, no...it's the other way around, I have a LetterTemplate that I need to extend to create another letter. The second letter should have everything from the LetterTemplate and in addition to that it should have a body too Can I show you the template?

OpenStudy (javk):

This is the template I have so far ``` java // public class LetterTemp { public String getDate() { return "<date>"; } public String getSenderAddress() { return "Art Foundation\nChildren's Spring Program"; } public String getMessageBody() { return ""; } public String getRecipientName() { return "Parent"; } public String getSenderName() { return "JAK"; } public String renderTemplate() { StringBuilder sb = new StringBuilder(); sb.append(getDate()+"\n"); sb.append(getSenderAddress()+"\n"); sb.append("\n"); sb.append("\n"); sb.append("Dear "+getRecipientName()+"\n"); sb.append("\n"); sb.append(getMessageBody()+"\n"); sb.append("\n"); sb.append("Yours sincerely,\n"); sb.append(getSenderName()); return sb.toString(); } public static void main(String[] args) { LetterTemp lt = new LetterTemp(); String letter = lt.renderTemplate(); System.out.println(letter); } } ```

OpenStudy (anonymous):

Letter has all this things but also 'body' right?

OpenStudy (javk):

right.

OpenStudy (anonymous):

I still think Template should be extending Letter. Then there are two options. To set values different initial values for the members of Letter which is ideal if those values should change letter, or override functions to return different values

OpenStudy (anonymous):

Let me ask you this. after you create the letter, what do you want to do with it?

OpenStudy (javk):

Email it to the parents. Basically what I am trying to do is 1) create a template that I can always use for emailing parents about the upcoming children's workshop activities 2) utilize this template each time by simply adding substance to the body

OpenStudy (anonymous):

Ok, first you create a template letter and then set its body. right?

OpenStudy (javk):

My concerns would be 1- Where do I start the subclass? After the last curly bracket ends? 2-is this correct? ``` public class RelocationLetter extends LetterTemplate { public String getMessageBody() { return "We wish to inform you that due to the change in the weather conditions todays children activities will be relocated to the Art Meuseum "; }

OpenStudy (javk):

Yes that's right

OpenStudy (anonymous):

I think i'm trying to see what you mean. I thought you wanted to make many possible Templates, but now I see that you want to have a base class 'Template' and many specific 'Letters', right?

OpenStudy (javk):

yeah, that way I don't have to do extra work unecessarily

OpenStudy (anonymous):

Ok, so what's bothering you with what you have so far?

OpenStudy (javk):

Ok so I just added this to the code and it seems fine ``` class RelocationLetter extends LetterTemp { public String getMessageBody() { return "We wish to inform you that due to the change in the weather conditions todays children activities will be relocated to the Art Meuseum "; } } ``` but now what? how do I make use of this. This isn't really doing anything

OpenStudy (anonymous):

you can put the classes each one in its own file. if they are small classes then you could put them all one after another in a single file. I'd put the template in its own file anyway. What I'm not sure I understand is the idea with the specific letters. Are you sure the contents are going to be always fixed?

OpenStudy (javk):

the stuff in the template never changes , only the body changes, and that is why we are trying to extend the class

OpenStudy (anonymous):

Yes, i'm talking about the stuff that change. At the bottom line you'll still have a restricted amount of extending classes, which means that you have a set of somewhat fixed contents for your letter, is that correct?

OpenStudy (javk):

Correct.

OpenStudy (anonymous):

Ok, so you'll have to think what will be the most comfortable for you as a developer to make. At the bottom line what you want to have is an object of the template which also has a body. You could either do it by making classes overriding the getbody function for specific values as you have now, which seems fine if you don't have too many messages, or you could perhaps use a factory. If it was up to me I'd consider simply passing the message to the Template in the constructor, because I assume each message is used exactly in one place in the code so there will be no duplication and it will be very easy. If I wanted to organize all those messages then I'd consider going with a factory for building letters with specific messages. Anyway I do not see anything wrong with your direction, just create a member of RelocationLetter as following: ```java LetterTemplate letter = new RelocationLetter(); ``` and then use `letter`

OpenStudy (javk):

It's not working. I'm really really really stuck now

OpenStudy (anonymous):

Show me what you're trying to do and explain what's not working

OpenStudy (javk):

According to me this should have worked, but the program just terminates instead I'm using this with the LetterTemp I posted above ``` class RelocationLetter extends LetterTemp { @Override public String getMessageBody() { return "We wish to inform you that due to the change in the weather conditions todays children activities will be relocated to the Art Meuseum "; } public static void main(String [] args) { RelocationLetter newLetter = new RelocationLetter(); newLetter.getMessageBody(); } } ```

OpenStudy (anonymous):

But you're doing nothing with the returned value, it has no effect. Print it instead ``` System.out.println( newLetter.getMessageBody() ); ```

OpenStudy (anonymous):

I just noticed, your `main` method is in the class `RelocationLetter`, why is that?

OpenStudy (javk):

but that would just give me that one line What I should be doing is 1- overriding the getMessageBody method 2- write the main method such that it implements everything in the LetterTemp class except for that one method that I overrode What I did was something else entirely apparently this is the main method ``` public static void main(String [] args) { RelocationLetter newLetter = new RelocationLetter(); System.out.println(newLetter.getMessageBody()); LetterTemp lt = new LetterTemp(); String letter = lt.renderTemplate(); System.out.println(letter); }

OpenStudy (anonymous):

I'm a little confused. Where is your main method located? you have only one main method, right? From the code above I see you have one at RelocationLetter class, why is that?

OpenStudy (javk):

this gives me everything in the LetterTemp class, nothing more nothing less. Where else should the main method be?

OpenStudy (anonymous):

I'd make a class for the application itself and put it there.. it doesn't belong in such classes. What do you get in the final print?

OpenStudy (javk):

haha it was so bad (btw that is sad laughter) ``` We wish to inform you that due to the change in the weather conditions todays children activities will be relocated to the Art Meuseum <date> Art Foundation Children's Spring Program Dear Parent Yours sincerely, JAK ```

OpenStudy (anonymous):

Your main function should look like this: ```java public static void main(String [] args) { RelocationLetter newLetter = new RelocationLetter(); String letter_string = newLetter.renderTemplate(); System.out.println(letter_string); } ```

OpenStudy (javk):

oh WOW that really really reall worked now just give me a while to compare it with the stuff.

OpenStudy (javk):

ok I think I get it! thankyou. thankyou. thankyou. I can almost here your sigh of relief :) I feel like giving you a million medals for your patience.

OpenStudy (anonymous):

Lol, you're welcome =) Don't worry about me, I just hope you get it completely

OpenStudy (javk):

yup I just did the same thing for another letter and it's working.

OpenStudy (anonymous):

Notice that your variable can be of type `LetterTemp` and it will still work: ``` LetterTemp newLetter = new RelocationLetter(); ``` This is useful because you can create an object out of different classes, but treat it the same way using the interface of LetterTemp. For example you could have something like this: ``` LetterTemp newLetter; if( condition ) newLetter = new RelocationLetter(); else newLetter = new OtherLetter(); System.out.println(newLetter.renderTemplate()); ```

OpenStudy (javk):

ok. thankyou

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!