• Post Reply Bookmark Topic Watch Topic
  • New Topic

Queries in Java  RSS feed

 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have few clarifications.


1>
class A{
protected void methodA();
}

class B extends A{
public void methodA();
}

will the above code work ??? Is overriding possible ???
what are the scopes of access modifier allowed in super class and for subclass ??


2> What is Serialization,Deserialization and Externalization ??

3> What is Difference between a class that implements Serializable and a class that does not implement Serializable. ??

4> When do we go for Serialization.Please help me with an example.?? Any Advantages and disadvantages of Serialization ???



 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Yes, this will work. So long as the subclass overrides with the same or wider scope it is OK. This only applies to overriding, you can overload with any scope you like.
2) Serialization is writing a description of the class to the file system, deserialization is reading it back reconstructing the class in the same state (hopefully!). Externalization is the same idea, but does not provide the default protocol that Serialization does, you'll need to provide your own.
3) One can be serialized, one cannot. If it is a customer class, you will almost certainly have to provide your own serialization/deserialzation methods (writeObject and readObject).
4) You do it when you need to, simple as that. An example might be to serialize some session data on a server when load is too great, then recreate it when required. Or to serialize a class on one computer and send it to another where it can be deserialized and used. You need to be aware of what data will be retained, destroyed and may even mutate; you need to know that the constructor will not run, you need to be able to deal with non-serializable members. You also need to take care with newer versions of your classes. If you make a change, can you still deserialize older classes? Does this matter in your application? You may also have to worry about performance, if you are relying heavily on serialization, will you begin to overload the file system, would it be better to store the state another way?

Best thing to do is create a simple class and start doing some examples.

HTH

J.
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jason Irwin,
Thanks for the awesome replies....Thanks once again

1) Yes, this will work. So long as the subclass overrides with the same or wider scope it is OK. This only applies to overriding, you can overload with any scope you like.
Cool.i tried it its working.



2) Serialization is writing a description of the class to the file system, deserialization is reading it back reconstructing the class in the same state (hopefully!). Externalization is the same idea, but does not provide the default protocol that Serialization does, you'll need to provide your own.
Any example for Externalization in java ??


3) One can be serialized, one cannot. If it is a customer class, you will almost certainly have to provide your own serialization/deserialzation methods (writeObject and readObject).
Any example of Customer class please. ??



4) You do it when you need to, simple as that. An example might be to serialize some session data on a server when load is too great, then recreate it when required. Or to serialize a class on one computer and send it to another where it can be deserialized and used. You need to be aware of what data will be retained, destroyed and may even mutate; you need to know that the constructor will not run, you need to be able to deal with non-serializable members. You also need to take care with newer versions of your classes. If you make a change, can you still deserialize older classes? Does this matter in your application? You may also have to worry about performance, if you are relying heavily on serialization, will you begin to overload the file system, would it be better to store the state another way?

Best thing to do is create a simple class and start doing some examples.
-- A simple example needed to serialize the session data on a file




Irwin's comments :: Serialize a class on one computer and send it to another where it can be deserialized and used. You need to be aware of what data will be retained, destroyed and may even mutate; you need to know that the constructor will not run, you need to be able to deal with non-serializable members. You also need to take care with newer versions of your classes. If you make a change, can you still deserialize older classes? Does this matter in your application? You may also have to worry about performance, if you are relying heavily on serialization, will you begin to overload the file system, would it be better to store the state another way?
--- Erwin an example for this scenario please



Deepak Lal




 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would use Externalization when Serialization does not provide what you need. I have never needed to use it.

Sorry, there was a typo. 3) should have said "custom class", i.e. one you have written.

4) The exact details of the process with depend greatly on your application. Do you have data that cannot (must not!) be serialized (for example, a database cursor). Do you even need to serialize anything?

Like I said, the best thing to do it just start doing your own examples. Then, if you are having problems, people can look at your code and offer advice.
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jason,




You would use Externalization when Serialization does not provide what you need. I have never needed to use it. --> I need an example..

Sorry, there was a typo. 3) should have said "custom class", i.e. one you have written. --> Its ok

4) The exact details of the process with depend greatly on your application. Do you have data that cannot (must not!) be serialized (for example, a database cursor). Do you even need to serialize anything?

Like I said, the best thing to do it just start doing your own examples. Then, if you are having problems, people can look at your code and offer advice.

i have a Customer class with customer details like cust_user,cust_passwd,cust_addr1,cust_addr2.
can you tell me how to achieve serialization for the below code snippet ??




 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Lal wrote:
------------------------------------------------------------------------------------------------
HIGHER SCOPE to LOWER SCOPER
DAFAULT->PROTECTED->PUBLIC ->PRIVATE ----> is it Correct
------------------------------------------------------------------------------------------------

No, that is not correct. I suggest you look up scopes. This is Java basics, it will be in every basic Java book and website.

You would use Externalization when Serialization does not provide what you need. I have never needed to use it.
I need an example..

I already explained the difference. If you need an example, I suggest you do some research.

i have a Customer class with customer details like cust_user,cust_passwd,cust_addr1,cust_addr2.
can you tell me how to achieve serialization for the below code snippet ??

You already know that, you mentioned the interface in your first post.

I get the impression that this is a class assignment or something. Folks round here don't mind helping, but that does not extend to doing other people's work for them.
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the awesome replies....Thanks once again

1) Yes, this will work. So long as the subclass overrides with the same or wider scope it is OK. This only applies to overriding, you can overload with any scope you like.
Cool.i tried it its working.


How come you didn't get a compile time error for the methods not being abstract?
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu Kansal wrote:How come you didn't get a compile time error for the methods not being abstract?

*sigh* I hate it when I don't spot stupidly simple things like that. And, of course, the class needs to be abstract as well.
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer is

[edit]Re-format columns[/edit]

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!