Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading

 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James,
Here is a simple example




The output of this code will be:

In A
In A
In B
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@@ 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
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any takers for james question ??
 
Loganathan Karunakaran
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...


 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic