Forums Register Login

constructor and inheritance

+Pie Number of slices to send: Send
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!!!
+Pie Number of slices to send: Send
hello my friend , in your programming ,you have not a main method, 1 the main method is the

entrance of the running program 2 in a java file you can't have the two public class in

this programming i can see two public class you should delete the class which in front of

the Alcohol 3 you better have build a package not default package in order to avoid the of

a file has the same class you can write a test class to test the result try
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Anissa, in the future it would help if you use code tags and make sure we understand if the code is one file or two(or an implied 3)
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

Paul Clapham wrote:

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.



yeah,post the whole stack trace
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
NullPointerException...i didnt notice that at first...that is a clue.
it is a runtime exception not a compile time error
give me a moment....
+Pie Number of slices to send: Send
ok you have even me confused..pretty easy to do actually


when I commenting out my super(name) it works



did you mean when you commented out this.name?
regardless, what does super(name); do?
if you guessed nothing you are correct.

im not sure why you are getting the nullpointerexception though
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Gin? Do you mean Qin?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
2
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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).

HIH

Winston

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
For those who want to learn more about inheritance, these could be a good resources:

1) Inheritance: Exam questions tests
2) Sun Certified Java Programmer 310-065
3) Programming fundamental in Java
+Pie Number of slices to send: Send
 

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
Not so fast naughty spawn! I want you to know about
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1297 times.
Similar Threads
Creating Beans by invoking static factory method
cannot find symbol
Primary Key class?
Refactoring Exercise
Polymorphism and "this" keyword, an interesting Q..
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 23:06:29.