This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloading" Watch "Overloading" New topic
Author

Overloading

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

I came across a sentence in Kathy Sierra Book which says
"overloaded version of the method to call is based on the reference type of
the argument passed at compile time"

The condition is :
1. Class B extends class A.
2. Class B overloads class A's method.
3. A a= new B();
4. a.method();

The method of class A is invoked.
Can anyone give a example program for me


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
James,
Here is a simple example




The output of this code will be:

In A
In A
In B
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

@ Rekha Srinath

I was able to understand your program.
but it creates a doubt...

Class B has got the both the methods.
How can we prove that the output is from the class A.

Tell me if it is possible.Dont mind if i sound stupid....
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

This is what was meant by the book



So as you can see that in both the cases the actual object was of type Derived, but different methods were called each time


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
James,

How can we prove that the output is from the class A.

I do not exactly understand what you mean by this.

But, what can be said is: B extends A means, B is-A A..That means whatever is there in A, is there in B also.

You have not explicitly rewritten the Method() function in B, which means A's Method() is carried-forward in B also. (Note: If Method() is explicitly coded in B, then it becomes overriding, as you might be aware...)
Ramaprasad Kolla
Greenhorn

Joined: Oct 22, 2008
Posts: 23
Hi James,

Class A {
public void print() {
System.out.println("In class A");
}
}
Class B extends A {
public void print(String str) {
System.out.println(str);
}
}

We know that super class reference can hold the object of subclass but the point worth noting here is: that super class reference can invoke only those methods that are present in super class but not in subclass because super class is unaware of the newly added methods in subclass.
i.e. A a = new A(); or A a = new B()
a is aware of the methods defined in Class A. It doesn't even know the existence of B's own methods.
| Class A
| Its own methods


| Class B
| Methods from super class (i.e. A)
| Its own methods


Thanks,
Ramaprasad K
In Love with Java -@
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

what can be said is: B extends A means, B is-A A..That means whatever is there in A, is there in B also.



which means
Class A has one method i.e Method()
and
Class B has Method() and Method(int a)

Now want my doubt is ,How can we prove that the Method() which is invoked is from the Class A's and NOT from the Class B's.

Hope i put my question in the right way this time.
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Method() is one and the same in both A and B, because its an inherited method...So, in case of inherited methods that have not been overridden, there is no significance in telling whether its the parent's or subclass's method.

But, but, if you OVERRIDE the method, then it can be determined whether parent's or subclass's method is called, depending on the reference type, runtime object and the other overriding rules
[ November 11, 2008: Message edited by: Rekha Srinath ]
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

@@ Rekha Srinath

Thank you for clearing my doubt.
One last thing.
when Class B extends class A,
Will class B have
- reference of the method
- or copy of the method
- or Just an acces to that method
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
I think copy of the method...not sure though...Lets hear to others' say.

But it does not matter to the pogrammer...
[ November 11, 2008: Message edited by: Rekha Srinath ]
Vinay Dinakar
Greenhorn

Joined: Dec 30, 2008
Posts: 17
Any takers for james question ??


Thanks,

~ Vinay Ds
Loganathan Karunakaran
Ranch Hand

Joined: Nov 04, 2008
Posts: 76
One last thing.
when Class B extends class A,
Will class B have
- reference of the method
- or copy of the method


James..

When Class B extends Class A, if a method in super class(i.e. Class A) is declared as "public" or "protected", then we can think that the method is defined in Class B itself...



OCPJP 1.6
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
Loganathan Karunakaran wrote:One last thing.
when Class B extends class A,
Will class B have
- reference of the method
- or copy of the method


James..

When Class B extends Class A, if a method in super class(i.e. Class A) is declared as "public" or "protected", then we can think that the method is defined in Class B itself...





Java manipulate objects(data and methods) by reference.
But Java doesn't pass method arguments by reference, it passes them by value.

So if Class A has a method, class B can have reference to that method.

If the method in ClassA is overridden by ClassB, it becomes a different method. But it never gets copied.


SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloading