• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

output!

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
I found this question in one of the mock exams for java 5.0
can anyone predict the output for this code..

import java.io.*;
class Player {
Player(){ System.out.println("p");
}
}
class Ex extends Player implements Serializable {
Ex(){System.out.println("c");}

public static void main(String args[]){
Ex f= new Ex();
try
{

FileInputStream fin = new FileInputStream("play.txt");
ObjectInputStream out1 = new ObjectInputStream(fin);
Ex b= (Ex) out1.readObject();
out1.close();


}catch(Exception e){}
}
}
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...

Ouptut is p c

The line
Ex f = new Ex(); will create a Ex object...

In the Ex constructor super class constructor is getting executed ..

so the first "p" and then the constrcutor of Ex..so "c" is printed





Regards

[ November 08, 2005: Message edited by: A Kumar ]
[ November 08, 2005: Message edited by: A Kumar ]
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,A Kumar.
output is p
c
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..A Enter

You are right...I had a similiar program in the same package..

and run that one..so got that output...

It is p c for this program

Thanks for the correction....

I have pasted that code too...




output here is p c p
The explanation i had written in the earlier post(deleted now) is valid for this code.


Regards
[ November 08, 2005: Message edited by: A Kumar ]
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Whenever the JVM tries to read an object from the stream, if the object implements Serializable interface, then object will be contructed without calling its constructor. But if its base class doesn't implement Serializable, then during object construction only for base class, the default no arg cons will be invoked.

This is one of the main diff b/w implementing Serializable interface and Externalizable interface. In case of objects implementing Externalizable interface, while reading the object from the stream at first step Object will will be created using its default no arg constructor, then readExternal method will be invoked.

Regards
Lakshmanan
 
Preetha Vasudevan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried Compiling the code and i got the output as P C P
..Can u explain why i got this output..
 
Lakshmanan Arunachalam
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Preetha,

The reasons are,

1. when ever you try to construct an object, its super class no-arg constructor will be called implicitly by the JVM(provided there is no explicit call to super class's any args constructor).
Hence the statement
Ex c1 = new Ex();

will invoke the base class, in this case Player's no-arg constructor, then Ex constructor. This will print p followed by c.

2. when a serialized object is read from a stream, if it extends any non-serialzed class, then that non-serialized class no-arg
constructor will be invoked.

Note: Just declare that Player implements Serializable interface, see the difference.

Hope this helps !!

Regards
Lakshmanan
 
Preetha Vasudevan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Lakshmanan your explanation was very cler..
 
reply
    Bookmark Topic Watch Topic
  • New Topic