• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Object behavior when reassign !

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the code below what triggers my curiosity and my understanding is that the line tagged with Tag: produces q.y = 7 ?
Anyone can direct my research on this ?
<pre>
class Shape {
int x=7, y=7;
int getY() {
System.out.println("Shape " + y);
return y;
}
}
class SubShape extends Shape {
int x=5, y=5;
int getY() {
System.out.println("SubShape " + y);
return y;
}
}
class CastObj extends Object {
public static void main(String args[]) {
Shape p = new Shape();
System.out.println("p.y = " + p.y + " p.getY() = "+ p.getY());
Shape q = new SubShape();
System.out.println("q.y = " + q.y + " q.getY() = "+ q.getY());

Tag:System.out.println("p.y = " + p.y + " q.y = "+ q.y);
System.out.println("p.getY() =" + p.getY() + " q.gety() " + q.getY());
}
}
// Result printed out
//
// Shape 7
// p.y = 7 p.getY() = 7
// SubShape 5
// q.y = 7 q.getY() = 5
// p.y = 7 q.y = 7
// Shape 7
// SubShape 5
// p.getY() =7 q.gety() 5

//

</pre>

[This message has been edited by Vroummmmm (edited June 12, 2000).]
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I have not completely wrapped my head around this concept either, but I thought I'd take a poke at it anyway.
Obviously, you have overridden getY(), but when you create a variable with the same identifier in a subclass as is in the superclass, there is no overriding taking place. It is simply that you are using the same name again (similar to declaring a variable inside a method with the same identifier as a member variable).
Upon initialization of SubShape, a Shape object must first be "created" so that a SubShape object can be built around it.
Let's go for an analogy here:
There is a Car class. It has a member variable color and an instance method drive(). There is a subclass of Car called Focus which has its own member variable called color and it has overridden drive() with
e73
its own drive() method.
Let's use our imaginations for a moment. At the assembly plant they get a Car and want to turn it into a Focus. The first thing they do is notice that drive is overridden meaning: don't drive like a car, drive like a Focus. They take the Car engine out and put a Focus engine in there.
The next thing they do is see that Focus has a color property. They spray the paint on the car which masks the original color, and it becomes a Focus. What they have not done here is to first remove the old paint of the Car before applying the new paint of the Focus (which is what happened with the drive() method), they just painted over it.
Now, when an object is referred to using an identifier of a superclass, any properties and/or methods that were added on since it was just a car are now "invisible"(only from accessiblity land, not for good). This means that the new color is no longer masking the other color, but the engine (since it was replaced, not modified) stays. All we're left with for color is whatever the color property of the Car is, before it was masked by the color of the Focus.

Try it out:
class Car {
String color = "Silver";

public void drive(){
System.out.println("putt putt putt");
}
}
class Focus extends Car {
String color = "Red";
public void drive(){
System.out.println("Vrooom Vroooom");
}
}
class Driver {
public static void main(String[] args){
Focus f = new Focus();
Car c = f;
f.drive();
c.drive();
System.out.println(c.color);
System.out.println(f.color);
}
}

[This message has been edited by Paul Caudle (edited June 12, 2000).]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vroummmmm:
[B]In the code below what triggers my curiosity and my understanding is that the line tagged with Tag: produces q.y = 7 ?
Shape q = new SubShape();

in this reference when you are calling variable it will look into Shape ,but when you are calling method it looks into actual object referencing i.e SubShape .. so when you are calling q.any variable it will print variable of Shape ...if you call q.any method it will look into SubShape

 
Vroummmmm
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guy, when I will found the theory I a will post it!
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens is when u make a request of an object , the object makes it's own interpretation of that request.This in essence is the fundamental behind polymorphism.
The Java compiler checks to see if u have defined a method with exactly the same name,return type,no.,type,and order of args. as the method in parent class.This is overriding.Then the method called will be one in derived class.Not otherwise.However no such checking will be done for variables since the superclass has no knowledge about what variables a subclass may have added.
I hope i have answered satisfactorily.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic