Oceana Wickramasinghe wrote:...
output
class A = 10
class B = 30
If x variable inside class A and class B are the same variable, not copies, then why doesnt changing the value of x using "b" reference variable, change the value of x inside A, doesnt that mean class A and B have their own copies of x. Please help me understand.
I think the other responder has the right answer, but an unfortunate spelling mistake. He said there are "to different objects", and meant "two different objects".
I am going to try my own explanation anyway.
When your code executes the statement "A a = new A();", that causes the runtime to create an object of class A; this includes allocating memory for the variables in that newly created object.
When you execute "B b = new B();", you create another object, this one of class B. This includes allocating memory for all of B's variables, some of which were declared in Class A, and are part of an object of class B because of inheritance. But the two different object allocations are not linked to each other. You could execute this second statement 3 times; it would create 3 objects of Class B, and all of their instance variables would be independent of each other. You could execute the first statement 3 times, giving you 3 objects of class A, and with the other 3 statements you would have 6 independent objects.
Does that make it any more clear?
rc