• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Serializable Problem

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How the private readObject and writeObject methods are invocked...
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Through reflection. Java reflection provides fields, methods and constructors to be made accessible via setAccessible(boolean) method.
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
correction class Try implements Catch
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is still not clear to me...
My question is how the private methods are implemented and by whom?
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You so much
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic