File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Overlaoding questions/doubt 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 "Method Overlaoding questions/doubt" Watch "Method Overlaoding questions/doubt" New topic
Author

Method Overlaoding questions/doubt

Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1102
    
    1

Hi

I am not able to understand the behaviour of below mentioned codes

======================================
Code A -

class Parent {
String message = "parent";
void say() {
System.out.println(message);
}
}

class Child extends Parent {
String message = "child";
}

public class Test {
public static void main(String[] args) {
new Child().say();
}
}

Output is - parent

====================================

Code B -

class Parent {
Integer get() {
return 1;
}
}

class Child extends Parent {
Number get() {
return 2;
}
}

public class Test {
public static void main(String[] args) {
Parent yo = new Child();
System.out.println(yo.get());
}
}


Output is - 1

=============================

Code C

class Grandparent {
String name = "grandparent";
void act() {
System.out.println(name);
}
}

class Parent extends Grandparent {
String name = "parent";
}

public class Test {
public static void main(String[] args) {
Parent yo = new Parent();
yo.act();
}
}


Output is - grandparent


=======================

Thanks

Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
You didn't override say() in Code A, so polymorphism doesn't apply.
You didn't correctly override get() in Code B (return values must be the same or covariant), so polymorphism doesn't apply.
In code C, you are not handling a subtype with a super type this time. The method that will be called is dependent on the object, not the reference. In this case GrandParent.act() will be called.
Shashank Rudra
Ranch Hand

Joined: Mar 26, 2009
Posts: 131
Ryan can we say that Code C case is similar to Code A case.


Programmer Analyst || J2EE web development/design
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
There similar in that the subclass in each example doesn't override its inherited methods, so polymorphism isn't applied to either. So, yes.
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1102
    
    1

Hi Ryan

I am not clear about explanation given by you for for Code sample A and C.

For Code Sample B , we are saying that since ploymorphism does not work here so calling method will decide based on Reference type ?

Thanks
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
Can't explain it any better than that. In all the examples you either didn't override the method or you didn't do it correctly. If you didn't override the methods, then the method to be called will be based on the reference type.

Ahh in B, you having non-matching return types
In A and C, you didn't override and polymorphism is only applicable when you've overridden the methods.
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
Ah, you indicate that the output for 'code B' is 1--but the code under B doesn't compile
because as was mentioned it's an illegal override. It's illegal because all that has changed
is the return type, which renders it neither an override nor an overload.

An exception would be a legal covariant return type. B would have been legal under
this exception if the parent class return type for the method had been Number, and
the child class return type (the overriding return type) had been Integer. But it is the
reverse in the example.

With regard to A and C, in each case the subclass gets a method through "inheritance."
I put that in quotes because I think it is misleading. K&B actually says something to the
effect that a class that inherits a method gets it as though it had been declared in the
class, which is also misleading, I think. A subclass that has a method through inheritance
can invoke the method as if it had been declared in that class, but the method still runs
in the scope of the superclass, so that member vars used in the method will be found
in the superclass, not in the subclass.

So my point is that it is more like a subclass has access to [public, protected, and in
some cases default,] methods of the superclass, than that it has its own copies of superclass
methods as though they were declared in the subclass itself.

And so it doesn't matter whether the method is invoked on a superclass reference holding a
subclass object, or a subclass reference holding a subclass object...member variables
used in the method will always be located first in the superclass method, because the
method is running in the superclass scope.

Your subject is "Method Overlaoding [sic] questions/doubt." Easy--it's not an overload if
the argument list doesn't change.


SCJP 88% | SCWCD 84%
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
Yeah, I didn't even pay attention to the message subject. Your code seems to suggest your trying to use polymorphism. Which is it? You didn't overload any methods.
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1102
    
    1

Hi Ryan and Ken

Thanks for all your reply
I have now understood the concept.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method Overlaoding questions/doubt