File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serializable Problem

 
Damodar Mukhopadhyay
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is The code from the book of K&B Can Any one please tell me how we use the private methods??? how the private methods of Dog Class involbs??? :roll:

(shouts not necessary in topic title)
[ February 26, 2007: Message edited by: Barry Gaunt ]
 
Damodar Mukhopadhyay
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is rhe code..............




class Dog implements Serializable {
transient private Collar theCollar;
public int dogSize;
public Dog(Collar collar, int size) {
theCollar = collar;
dogSize = size;
}


public Collar getCollar() {return theCollar; }

private void writeObject( ObjectOutputStream os)
{
try {
os.defaultWriteObject();
os.writeInt(theCollar.getCollarSize());
}catch(Exception e) { e.printStackTrace(); }
}

private void readObject(ObjectInputStream is) {
try {
is.defaultReadObject();
theCollar = new Collar(is.readInt());
} catch (Exception e) {e.printStackTrace(); }
}
}//End of dog class
 
Damodar Mukhopadhyay
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How the private readObject and writeObject methods are invocked...
 
Nandu Jawale
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Through reflection. Java reflection provides fields, methods and constructors to be made accessible via setAccessible(boolean) method.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Damodar,

I appreciate you for imposing the Why.
I too don't know what happens exactly there.
Here is a hint that can happen.
Hope it will improve your thought process...

interface Catch{

private void display();
public void callDisplay();

}

class Try{

private void display() {
System.out.pritntln("I like the way you thinking");
}

pulic void callDisplay(){
display();
}

}

Cheers
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
correction class Try implements Catch
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am sorry for the above example where i have written private method in interface.

I am also very curious to know how the private(readObject) method is accessed from Dog class.

Any suggestions welcome
 
Damodar Mukhopadhyay
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is still not clear to me...
My question is how the private methods are implemented and by whom?
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method is invoked by the JVM. JVM can access your private methods. But other classes would not be able to access the same. Serializable is a marker interface having no methods. There are many methods you can have in your class which implements serializable to tell the JVM what to do at runtime with the method. A look at the api would be good. Here you go.
 
Damodar Mukhopadhyay
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You so much
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic