Hello,
can I use super in my constructor if super class does not throws an expcetion but subclass throws an exception for example
It throws me null pointer exception however when I commenting out my super(name) it works Is problem in Exception that I throw in subclass but do not throw in superclass???
thank you in advance!!!
i have never heard that a sub-class cannot throw a new exception. of course i dont know everything. and YES you can have as many public classes as you want(not in the same file though i dont think, but the code doesnt explicitly say they are the same file), but one of them must include a main() method(he might have a third class with main() in it that he didnt post). and you dont NEED package statements, although they have a use.
Anissa Pary wrote:It throws me null pointer exception however when I commenting out my super(name) it works Is problem in Exception that I throw in subclass but do not throw in superclass???
If you get an exception thrown at run-time, the best strategy is to look at the exception and see what it says. Picking a random feature of your code and trying to blame that feature is not likely to be a good strategy at all.
So. What does the exception say? Post the whole stack trace.
Anissa Pary wrote:It throws me null pointer exception however when I commenting out my super(name) it works Is problem in Exception that I throw in subclass but do not throw in superclass???
If you get an exception thrown at run-time, the best strategy is to look at the exception and see what it says. Picking a random feature of your code and trying to blame that feature is not likely to be a good strategy at all.
So. What does the exception say? Post the whole stack trace.
Randall Twede wrote:i have never heard that a sub-class cannot throw a new exception. of course i dont know everything. and YES you can have as many public classes as you want(not in the same file though i dont think, but the code doesnt explicitly say they are the same file), but one of them must include a main() method(he might have a third class with main() in it that he didnt post). and you dont NEED package statements, although they have a use.
i know what you said haha but i think in a file you‘d better build a package the auth probably have several java file i know
gin was right about one thing. i can compile both your classes but without a main() i cant try to run them and that is where your problem is....at runtime
Design problem: You have a class called Ingredient, which has a name. Now name field exists in all its subclasses. Even if it is private, which it ought to be. You should not add a name field in any of the subclasses, beacuse that would hide the superclass’ field. You should allow access to the superclass field in subclasses via a public (or, sometimes protected) “get” method. In fact, you should make use of the name field in the superclass, and the subclasses can simply inherit any methods which use name.
You should, therefore, keep super(name) and delete both the name field and this.name = name; from the Alcohol class. You should not repeat or override the getName() method in the subclasses; they ought to inherit the superclass’ method unchanged. In fact, you could label the superclass’ getName() method final.
More design problems:
You have a field called isEqual or similar, which you can delete, because you never use it. You also seem not to use your state field, so you can delete that.
You should not say if (...) return true; else return false; A better form is described here.
I think you have your alcoholContent field in the wrong place. It ought to be in the superclass. Now you can call your constructors like this:and . . .I think the Ingredient class should be abstract. I think it should have one constructor and only one, which takes the two parameters. You use constructors to enable you to create instances, and you use them to enable you to restrict object creation. You want to make sure the alcohol content and name are set up, so you make sure all your versions of the constructor set up alcohol content and name. What I have shown you does that. [There are other ways to achieve that.]
You should be throwing IllegalArgumentException (IAE) not plain Exception, and deleting the throws declarations because IAE is unchecked. The Exception throwing is best done by the Ingredient constructor.
You should implement all the methods you need in Ingredient, then you will find an advantage of object-oriented programming that what I have written constitutes the entire Juice class All the necessary methods are inherited, probably unchanged.
You have not implemented equals() correctly. You should override it, not overload. Search this forum because overriding equals() is much more difficult than many people think.
You are calling a public method from a constructor. A non-private method called from a constructor should be labelled final, otherwise it might be overridden in a subclass to do something different. I would suggest you can dispense with set methods for the name and alcohol content, because they are not going to change during the program. You can alter alcohol content by adding more methylated spirits (!!) or coke, but that is more adding another ingredient than changing alcohol content.
Campbell Ritchie wrote:You are calling a public method from a constructor. A non-private method called from a constructor should be labelled final, otherwise it might be overridden in a subclass to do something different.
@Anissa: Campbell's point is very important, and failure to follow his advice can lead to errors that are very tough to find (or duplicate). In addition, you should always document this form of "self-use".
An alternative to making the method final is to add a private 'helper' method, viz:Do you see the difference? Now, if someone overrides the 'set' method, the object will still be created the same way.
It's a prime example of David Wheeler's famous quote: "every problem in Computer Science can be solved by adding a layer of indirection."
BTW, you'll notice that I also changed the method to throw an IllegalArgumentException. This is the normal exception to throw in the case of an invalid parameter passed to a method; it also doesn't need to be declared (unless you want to).
I said there are other ways to achieve that, and WG has here shown a nice different way to set your alcohol level. [I prefer to do it with a glass of beer, but that’s a different story ]
There are usually several different ways to do most things in computing.
You are calling a public method from a constructor. A non-private method called from a constructor should be labelled final, otherwise it might be overridden in a subclass to do something different.
i need to study this concept more because it seems important
Post by:autobot
Not so fast naughty spawn! I want you to know about
a bit of art, as a gift, the permaculture playing cards