A Subclass – Intro to Java Programming

You’ve just worked with the question class and now its time to form a Subclass. We’ve all seen these multiple choice questions, as a question and there’re multiple choices hence the name. By the way, if you ever want to know what’s the original name of the Java language, it was Oak, which was a name that was so dull and boring that they changed it. Now, of course, one could put this entire text here into a question class and set the answer to this string three. But that too would be dull and boring. It would be better to have a class that specifically deals with multiple-choice questions. So that we could declare an object, somewhat like this. We make a Choice Question, that’s what I’m calling this class. We still need to set the text of course, but then for the choices I want to be able to call a method that I call addChoice that adds the particular choice and then specifies whether it’s false or true. When I formed the Choice Question class, I didn’t want to start from scratch. I want to inherit as much as possible from the question class that’s already written. Let’s see how to do that. I’ll define the ChoiceQuestion class and then I specify that it extends the question class, that’s the Java keyword for denoting that ChoiseQuestion should become a subclass of question. Into the body of the subclass, I put in any new methods that are present in the subclass but not the super class. Such as the Add Choice Method. You also put in the implementations of any methods that needed to change in the sub class. For example, we’ll need to change the display method, since displaying a choice question must also display the choices. And finally, you of course need to add any instance variables that the choice question class needs to do its job. In our case, that would be the list of the choices. The key point is what you don’t put in here. When you define a subclass, you never include any of the methods from the superclass that work just fine. For example, the set text method will work fine, and we can simply inherit it from the superclass. Also, you don’t want to include any fields from the superclass. You get those automatically, and if you were to include them again, you’d get ’em twice, which is not good. The point is that, what you put inside here is the difference from the superclass. Everything that the subclass has, that the superclass doesn’t have at all or that it doesn’t have in the same way. Let’s have a closer look at exactly how that works for question and choice question. Here is our question class, here is the choice question class. Now have a close look and tell me, which methods does the choice question class inherit? Just give me the names of all those methods that choice question doesn’t redefine, that it simply picks up from question. Which methods does choice question overrides? That is which method does choice question need to redefine because the one’s from question are not satisfactory. And finally, which method or methods does choice question add that weren’t present in the question class in the first place? Just give me the names of the methods, all together there are 5 of them.

Leave a Reply

Your email address will not be published. Required fields are marked *