File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes constructor and inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructor and inheritance" Watch "constructor and inheritance" New topic
Author

constructor and inheritance

Anissa Pary
Ranch Hand

Joined: Jan 02, 2012
Posts: 40
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!!!
qin weiping
Greenhorn

Joined: Jan 02, 2012
Posts: 8
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
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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.


SCJP
Visit my download page
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18992
    
    8

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.
qin weiping
Greenhorn

Joined: Jan 02, 2012
Posts: 8
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
qin weiping
Greenhorn

Joined: Jan 02, 2012
Posts: 8
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
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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....
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40057
    
  28
Gin? Do you mean Qin?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40057
    
  28
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40057
    
  28
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40057
    
  28
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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40057
    
  28
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.
Pedro Cunha
Greenhorn

Joined: Dec 13, 2011
Posts: 5
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
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructor and inheritance