• Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding superclass and subclass objects  RSS feed

 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Questions -
Why cant refrence variable aaa access td eventhough it refers to the instance of timedemo ?
AND
Now My another question is
Please see the comments the question is mentioned.



 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
td is not accessible because at compile-time the type of "aaa " is "a" and the compiler searches for "td" there.


Its like a child is trying to create its parent and it wants the parent to be just like itself!!!

The problem here is that you are trying to expand the limits of "a" at runtime.
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok I got that
You cannot extend limits of instance of parent class.
but In the next example .
You are doing the same thing .
(testclone)super.clone // which returns instance of type Object
and than you are assigning it to refrence x2 which is refrence of testclone (Child class).
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And that works !!

So I am not able to Understand that.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


this dooesnot work because "class a" doesnot contain td.
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pratik D mehta wrote:
but In the next example .
You are doing the same thing .
(testclone)super.clone // which returns instance of type Object
and than you are assigning it to refrence x2 which is refrence of testclone (Child class).


this works !!
But it should not ?
I want to understand this


 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes now coming to your second code..
first see here..
protected Object clone()
throws CloneNotSupportedException
Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object focus on the word "this" which means current object not a object of a class Object
So there is no problem in this line

because super.clone() is returning a object of the type "testclone"

i cannot understand why you are having a problem with this


because x1.clonetest(); will return a object of the type testclone and it should have no problem while assigning to its own refernce type.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is your actual code,i have putted some sys.out over there to make it more clear.




output of above program will be

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I Hope you understand it.
if not let me know

Happy learning..
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said super.clone() is returning a object of the type "testclone"

Than why do we type cast it return (testclone)super.clone.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i think is
super.clone() return a shallow copy of the current object.
in order to get a deep copy we have to do the type casting..
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shanky Sohar wrote:what i think is
super.clone() return a shallow copy of the current object.
in order to get a deep copy we have to do the type casting..


By this you meant that
If a shalllow copy of object is created we will have problems like .
1) If a (shallow) copy of class object is than it would contain refrences of objects which point to the same original object when their copy is made.
So if we change the value of instance variable in the copy of the object it would change the value in the original object.
2) If we clone a object and it contains a reference to the I/O Stream than the copy would also contain the reference to the same stream.
And if we deep copy it
The object copied would contain references to the new objects, rather than the original.

Please give a nodd at this , else give me an example of deep copy and shallow copy
Thankyou Shanky sohar
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Pratik : your coding style is weird.Please follow the naming convention, while writing code.
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Seetharaman Venkatasamy
I didnt get what you are saying .
I dont mean any confusion.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!