• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

A query on reusing the classes

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi this is Kajol,
Good Morning everybody. I was going through Thinking in JAVA and got skuck in between. Heres the example where I have got stuck:
//: SprinklerSystem.java
// Composition for code reuse
package c06;
class WaterSource {
private String s;
WaterSource() {
System.out.println("WaterSource()");
s = new String("Constructed");
}
public String toString() { return s; }
}
public class SprinklerSystem {
private String valve1, valve2, valve3, valve4;
WaterSource source;
int i;
float f;
void print() {
System.out.println("valve1 = " + valve1);
System.out.println("valve2 = " + valve2);
System.out.println("valve3 = " + valve3);
System.out.println("valve4 = " + valve4);
System.out.println("i = " + i);
System.out.println("f = " + f);
System.out.println("source = " + source);
}
public static void main(String[] args) {
SprinklerSystem x = new SprinklerSystem();
x.print();
}
} ///:~
In this class SprinklerSystem.java i was unable to get to know the behaviour of WaterSource source datatype
Can anybody please explain me in detail as i went through the theory of Thinking in JAVA but couldnt grasp much .......
As per Thinking in JAVA says " For the
non-primitive objects, just put handles inside your new class, and for the primitives just define them inside your class."
So all i could get from this si WaterSource source is the non-primitive object of class WaterSource in class SprinklerSystem
It also says "One of the methods defined in WaterSource is special: toString( ). You will learn later that every non-primitive object has a
toString( ) method, and it�s called in special situations when the compiler wants a String but it�s got one of these objects. So in
the expression:
System.out.println("source = " + source);
the compiler sees you trying to add a String object ( "source = " ) to a WaterSource. This doesn�t make sense to it, because
you can only "add" a String to another String, so it says "I�ll turn source into a String by calling toString( )!" After doing this
it can combine the two Strings and pass the resulting String to System.out.println( ). Any time you want to allow this
behavior with a class you create you need only write a toString( ) method. "
So here in System.out.println("source = " + source); , is source the value taken from toString();
I am really confused with this ......
Please reply back at the earliest...
Thanks
Kajol
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, for non primitive data types System.out.println will see whether a toString() method is defined. It will use the vaule returned from this method to show the object.
Shah G.
[This message has been edited by Muhammad Ali (edited January 07, 2001).]
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kajol,
Just tell me this :
Is Thinking in Java , your first book on Java. In that case it would be better if you got a simpler one like The Complete Reference - Java 2, 3rd ed. Inspite of its size, it is not intimidating.
Thinking in Java goes into much more detail and it is a fantastic book. But it is better to start off with a simpler book :
In Java, all non-primitive objects are inherited from a class called Object. Object class defines various methods and one of them is toString(). toString is used to provide a String representation of your object.
Since water source is a new class which you have created, you can override the toString method of Object class to provide a more specific user friendly description of your class.
so you can say :
public String toString(){
return " Hello, I am a water source";
}
And System.out.println when ever it sees your Watersource object will call toString on the object and print the output as
"Hello, I am a water source";
Hope you have understood.
The example give in TIJ is to illustrate default initialization. All primitive values are initialiezed to corresponding zeros :
byte, short, int, long shall be set to 0.
char to '\u0000'
boolean to false
float and double to 0.0 and
all non primitives shall be set to null.
Why don't you run the program and see the output.
SJ
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said..
---------------------------------------------------------------
So all i could get from this si WaterSource source is the non-primitive object of class WaterSource in class SprinklerSystem
---------------------------------------------------------------
I can read some confusion in your understanding of the above code. Remember 'WaterSource source' in the code is just a handle to oject that would be instantiated using the 'new WaterSource()'
syntax. 'source' is just a handle pointing to the object of type WaterSource and not an object in itself.
As Sajan rightly said,method toString() can be overridden to return specific user friendly string than having a string representation of non-primitive objects.
Hope this helps.
GOOD LUCK.
 
Kajol Shroff
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sajan & Rajendra,
I went through ur foroum.......
That was really nice ........i think now i am less confused with it...
But I have a small doubt...
See in WaterSource class the toString() is returning s..which is as mentioned s = new String ("Constructed"),
So i think it should return the value to the string i.e. Constructed....but all i can understand that it is returning the default value of non-primitive values i.e. null.......
Please clarify this.........Kajol
 
Rajendra Deshpande
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kajol,
Back to your code....
class WaterSource {
private String s;
WaterSource() {
System.out.println("WaterSource()");
s = new String("Constructed");
}
public String toString() { return s; }
}
public class SprinklerSystem {
private String valve1, valve2, valve3, valve4;
WaterSource source;
int i;
float f;
void print() {
System.out.println("valve1 = " + valve1);
System.out.println("valve2 = " + valve2);
System.out.println("valve3 = " + valve3);
System.out.println("valve4 = " + valve4);
System.out.println("i = " + i);
System.out.println("f = " + f);
System.out.println("source = " + source);
}
Examine the above and you would find that the class WaterSource has not been instantiated. You just have a handle 'source' but where is the object....You have not created a WaterSource object.
Hence the 'System.out.println("source = " + source)' above gives you null. To create an object and have a handle to it you need to have 'WaterSource source = new WaterSource()'. Try this and see.
regards,
Rajendra.
 
Kajol Shroff
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajendra,
I totally agree to what u say....but i got confused because in Thinking in JAVA Bruce Eeckel has mentioned :
"System.out.println("source = " + source);
the compiler sees you trying to add a String object
( "source = " ) to a WaterSource. This doesn�t make sense to it, because
you can only "add" a String to another String, so it says "I�ll turn source into a String by calling toString( )!" After doing this it can combine the two Strings and pass the resulting String to System.out.println( ). Any time you want to allow this
behavior with a class you create you need only write a
toString( ) method. "
I got confused because of this statement......
What say......???
Kajol
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Kajol Shroff:
Hi Rajendra,
I totally agree to what u say....but i got confused because in Thinking in JAVA Bruce Eeckel has mentioned :
"System.out.println("source = " + source);
the compiler sees you trying to add a String object
( "source = " ) to a WaterSource. This doesn�t make sense to it, because
you can only "add" a String to another String, so it says "I�ll turn source into a String by calling toString( )!" After doing this it can combine the two Strings and pass the resulting String to System.out.println( ). Any time you want to allow this
behavior with a class you create you need only write a
toString( ) method. "
I got confused because of this statement......
What say......???
Kajol


Kajol,
The java operator + was overloaded to work with String operands, eg. ..aString + anotherString;
In order for the compiler to accept the 2nd argument source it has to denote a String type. The equivalent of System.out.println("source = " + source); is System.out.println("source = " + source.toString()); thus, making the two arguments consistent to the requirement of the expression involving the overloaded + operand.
One thing to remember is that all classes directly or indirectly extend the Object class. The Object class provides toString() as a utility method for returning the textual representation of the object. As in this case, class WaterSource has decided to override the toString() method to provide its own textual representation of its object. (If you try to comment out WaterSource's toString() method you will still be able to compile because it knows println() could still call class Object's toString() method.)
Now, this deals only w/ compile time or why a compiler accepts the println() arguments.
Back to your original question, Is source the value taken from toString()? At runtime, Rajendra's observation, "You have not created the WaterSource object, therefore your output gives you null" is valid. At runtime, JVM initializes all of the class members and so String s was initialized to null. The value of s would have been initialized to "Constructed" if the constructor was called by creating the WaterSource object with new. Since that did not happen, s remained referencing null in source.toString() and so its textual equivalent, "null" was eventually printed in println().
Hope this clears up things.
------------------
~James Baud
Talk, does not cook rice - Chinese Proverb

[This message has been edited by James Baud (edited January 08, 2001).]
 
Kajol Shroff
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,
Thanks for ur reply...
Now i got it exactly........
So in this program it is first converting the handle source to a string and since we have overloaded the toString() method in WaterSource class it is jsut printing the default value of toString()i.e. null.
If we would have typed WaterSource = new WaterSource() then it would have printed Constructed..
Am i right James....
PLese correct me
Kajol
 
James Baud
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kajol,
Just a few more tidbits...
1) "+" is an overloaded operator
2) WaterSource is overriding its superclass, Object's toString() method
3) toString() is a method therefore it does not have a value, it is returning s, a member variable with default value of null
4) Right on the sweet-spot. "If we would have typed WaterSource = new WaterSource() then it would have printed "Constructed" " because operator new will call the constructor WaterSource()
5) IMO, knowing the difference between overloading and overriding is very important in understanding OO concepts, inheritance and polymorphism
------------------
~James Baud
Talk, does not cook rice. - Chinese Proverb
[This message has been edited by James Baud (edited January 09, 2001).]
 
Kajol Shroff
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a few more tidbits...
1) "+" is an overloaded operator
2) WaterSource is overriding its superclass, Object's toString() method
3) toString() is a method therefore it does not have a value, it is returning s, a member
variable with default value of null
4) Right on the sweet-spot. "If we would have typed WaterSource = new WaterSource()
then it would have printed "Constructed" " because operator new will call the constructor
WaterSource()
5) IMO, knowing the difference between overloading and overriding is very important in
understanding OO concepts, inheritance and polymorphism
------------------
~James Baud
Talk, does not cook rice. - Chinese Proverb
Hi James,
Ur expalnation is really nice.......i could really understand it in a much better way....
Keep writing ..to my queries..........
Thanks a lot...
Kajol
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!