My first class will compile fine but when i try to compile the second class it is throwing error that void type in my class zoo is not allowed... I dint get the logic... Can anyone help to improve my knowledge of access modifier usage..
When you use a method invocation in an expression, you are saying "invoke this method and use the value it returns as an operand in the expression". Here you are trying to perform String concatenation with the value returned by Zoo.run. So tell us, what value is that?
rizwana mujawar wrote:Thank You so much.... I got to add String instead of void because it will be used as operand in other class.... Thanks ya guys:). Keep helping me like this to understand java better:)
Possibly not a good idea. Why would your run() method return a value? What is run()'s job, and what does its return value actually mean?
And defining methods like run() and sleep() that have the same name as common methods in the core API but that have totally different behavior can lead to lots of confusion.
And naming methods with names that don't describe what they actually do can lead to lots of confusion.
Randall Twede wrote:i thought all classes inherited a default no parameter constructor from the class Object.
Not really, no. What JV means is that the javac tool adds a default constructor if you don’t write your own constructor. You will find hints and suggestions that default constructors are a bad idea; you notice on that javadoc page they say you should always write a constructor.
if i am wrong, try adding a "do nothing" no parameter constructor to Zoo class
Adding code “because it seems to work” is a good way to create bad code. You write a constructor in order to establish the class invariant. If there is a part of the invariant which a constructor doesn’t establish, that constructor should be changed (or if overloaded, deleted).
rizwana mujawar wrote:My first class will compile fine but when i try to compile the second class it is throwing error that void type in my class zoo is not allowed... I dint get the logic... Can anyone help to improve my knowledge of access modifier usage.
The problem does not have anything to do with access modifiers (public / protected / private).
The run() method doesn't return any value (it's void), so you can't print its return value, which is what you're trying to do in line 6 of Moo.java.