my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confuse in {Parent p = new Child();} Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confuse in {Parent p = new Child();}" Watch "confuse in {Parent p = new Child();}" New topic
Author

confuse in {Parent p = new Child();}

Kelvin Mak
Ranch Hand

Joined: Oct 04, 2002
Posts: 51

What is the ref of p? a Parent or Child?
I think, p in line 1 is a Child. There is a method hiding in Child class, cause a method is a static method in Parent & Child class. The JVM cannot see the amethod in Child, so it will invoke the amethod in Parent. But why will invoke the varible i in Parent if p is a Child? What is p ref at that time?
If treat the ref p in line 1 as a Parent, it is easy to know what happen. But it is said from JLS, it should be a Child ref in line 1. confuse
[ November 14, 2002: Message edited by: Kelvin Michael ]

SCJP1.4
Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93
Kelvin,
-When a non static method is invoked on an object using a refrence, it is the class of the current objectdenoted by the refrence,that determines which method to be invoked.
-When a variable or static method is invoked on an object using a refrence, it is the type of refrence not the class of the current object,that determines which method to be invoked.
hope it is clear now
regds
Arpana


SCJP1.4(91%)
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
here's what my study book says, altered for the example you give):
"The compiler is able to determine which method (amethod) is called, depending on the object on which the method is called. If the object is a Child object, then the output will be "Derived.amethod()" and if the object is a Parent then the output will be "Base.amethod()"

I ran your code and got this output:
C:\BIN>javac Child.java
C:\BIN>java Child
99
Base.amethod()
C:\BIN>

here is the key line in the code:

p is a reference of type Parent ...
...even though the object type is Child

so variables and methods of Parent will be referenced in any code such as p.i ( which = 99).
now let's add something new to this code in the
Child class:

the output is now:

C:\BIN>javac Child.java
C:\BIN>java Child
99
Base.amethod()
-1
Derived.amethod()
C:\BIN>

we did not create a new Child; we cast the object type "p" into a Child object type then
assigned that to a Child c reference type.
Note: this example shows a slight flaw in my opinion - it's fine to override methods in child classes BUT I have a design problem with what was done with int i.
We usually build classes to take advantage of inheritance and this seems to violate the principle by altering the variable i.
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Hi, Kelvin
When you're creating object like
Parent p = new Child();
and if : class Parent and class Child has variable i, and method m(), then
Sample rule:
p.i - is a value of Parent's i (always)
p.m() - is a method of Child (if it's overridden,if they're static, then mothod of Parent will be called)
That's all.
P.S. I advice to change members and methods(i and amethod() ) to static and non-static,compile and test it. - The best way to learn+remember is practice.
Regards,
Jamal Hasanov
www.j-think.com
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
Jamal,
I changed the methods to delete the static modifiers thus:

my output changed exactly like you said:
with static modifers:
C:\BIN>javac Child.java
C:\BIN>java Child
99
Base.amethod()
-1
Derived.amethod()
without static modifiers:
C:\BIN>javac Child.java
C:\BIN>java Child
99
Derived.amethod()
-1
Derived.amethod()
C:\BIN>
[ November 14, 2002: Message edited by: david eberhardt ]
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
you can still get the desired output :
with static modifers on the methods and variables:
C:\BIN>javac Child.java
C:\BIN>java Child
99
Base.amethod()
-1
Derived.amethod()
by casting the object and reference types:
Parent p = new Child(); //1
System.out.println(p.i);
p.amethod();

Child c = (Child)p;
System.out.println(c.i);
c.amethod();
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Hi, David
I didn't understand - you're asking me or correcting something in my codes???... Can I help?
Regards,
Jamal Hasanov
www.j-think.com
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
clarification:
Jamal,
You were correct.
Do you also agree with how I did same thing only using different way?
Kelvin Mak
Ranch Hand

Joined: Oct 04, 2002
Posts: 51
hi, all guys. I found a simply way on this question.

When you're creating object like
Parent p = new Child();
and if : class Parent and class Child has variable i, and method m(), then
Sample rule:
* p.i - is a value of Parent's i (always)
* p.m() - is a method of Child (if it's overridden,if they're static, then mothod of Parent will be called)

Just treat p as a Parent reference, everything will be cleared.
* p.i - is a value of Parent's i (always). //ok, no problem.
* p.m - is a method of Child. if it's overridden, Parent.m() will be placed with Child.m(). if it's static, because static method cannot be overriden, so p.m() still is the Parent.m().
It needn't think so much, just regards p is the Parent ref will be ok.
I don't know my opinion is correct or not, pls correcting me if any mistake.
thx all guys at this question.
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Kelvin and David,
Yes, you're right. You can read JLS 8.4.8.5 Example: Invocation of Hidden Class Methods for clarification...
Regards,
Jamal Hasanov
www.j-think.com
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
Kelvin and Jamal,
excellent posts!
and I will check out "JLS 8.4.8.5 Example" - thanks.
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
"Hiding" static methods and "overriding" instance methods are similar -

output = 99
"Derived.amethod()"
J.L.S. 8.4.6.2 Hiding by Class (Static) Methods
"If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclass ... otherwise accessible ..."



output = 99
"Base.amethod()"
J.L.S. 8.4.6.2 Hiding by Class (Static) Methods
"A hidden method can be accessed by using a qualified name or by using a method invocation expression(15.11) that contains the keyword super or a cast to a superclass type."

note 1) in the second code example, we used "a cast to a superclass type" when we created the Parent p reference type (the superclass type).
note 2) Parent.amethod() would result in "Base.amethod()"
but super.amethod() would fail to compile because we are calling it from within the static main method. ( super and this always refer to instance objects)
[ November 18, 2002: Message edited by: david eberhardt ]
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
Originally posted by Jamal Hasanov:
Hi, Kelvin
When you're creating object like
Parent p = new Child();
and if : class Parent and class Child has variable i, and method m(), then
Sample rule:
p.i - is a value of Parent's i (always)
p.m() - is a method of Child (if it's overridden,if they're static, then mothod of Parent will be called)
That's all.
P.S. I advice to change members and methods(i and amethod() ) to static and non-static,compile and test it. - The best way to learn+remember is practice.
Regards,
Jamal Hasanov
www.j-think.com

yes, but let me write it out a little differently:
Sample rule:
p.i - is a value of Parent's i (always)
p.m() - is a method of Child (if it's a non-static method; overridden)
p.m() - is a method of Parent (if it's a static method of Parent; )
here's a file a played with to check out all the rules:
[PRE]
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
Hi..
I went through all the posts..but I still cannot understand that if we create a reference p as follows:
<b> Parent p = new Child();
Is p a Parent or Child?
</b>
I understand that we are creating an instance of class Child that is cast to type Parent...but what does this mean EXACTLY..
Thanks,
Cathy.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Cathy,
p is a variable whose compile type (or declared type) is Parent.
At runtime the variable holds a reference to an object whose type (runtime type or real type) is Child.
In Java a variable of a given type can refer to instances of its subtypes.
Every expression has a compile type known by the compiler.


SCJP2. Please Indent your code using UBB Code
M Sharma
Ranch Hand

Joined: Dec 13, 2001
Posts: 106
I went through all the posts above.
I concluded certain things :
As written by Arpana

-When a non static method is invoked on an object using a refrence, it is the class of the current object denoted by the refrence,that determines which method to be invoked.
-When a variable or static method is invoked on an object using a refrence, it is the type of refrence not the class of the current object,that determines which method to be invoked.

So as far as variable is concerned, it will always depend upon the type of the reference variable, regardless whether the variable is static or not.
But in case of method, non-static method will depend upon the type of the object denoted at run-time and static methods will depend upon the type of the reference variable.
But i wanted to know, what is the rationale behind this, that in case of static method it is being executed depending upon the type of the reference variable....
Is it because static things belong to the Class and not to the instance of that class ?
Pls correct me if i am wrong anywhere....


Regards, Manish
SCJP 1.4
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
The Java compiler will resolve what it can at compilation time - I am talking about those static, private and final methods. But instance methods which can be inherited, namely those which are not final or private (which are implicitly final), must be resolved at run-time because the compiler cannot know the correct method to call. This method binding (connecting a method call to a method body) in Java happens polymorphically via late binding, aka dynamic binding or run-time binding.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confuse in {Parent p = new Child();}
 
Similar Threads
Overriding static methods
static method invocation
run time error
doubt
Inheritance