• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

refrencing method not working

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

class InheritanceTest extends Process {
int x=18;

public static void main(String [] args) {
Process p = new InheritanceTest();
System.out.println(p.InheritanceTest('R'));
System.out.println(p.x);
}

InheritanceTest() {
System.out.println(true ^ true);
}

InheritanceTest(char c) {
System.out.println(c);
}

char InheritanceTest(char c) { // WHY NOT THIS METHOD CALLED
c='V';
return (char)c;
}
}

class Process {
int x=9;

Process() {
System.out.println("Starting Process...");
}

char InheritanceTest(int i) {
i='S';
return (char)i;
}
}

The object is refrencing to inheritanceTest object so its method char InheritanceTest(char c) should be called which is perfect for call why method of process its parent class call.
Thanks in advance
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there!
The answer for

is that you instantiate p masked as the super class:

However if you should

You would get the char arg version in�the InheritanceTest running which would then break the inheritance by overload. Should now have line

and you would be dealing w/ the inherited int arg version of Process.
NB: It is not very wise to mix constructor with regular method names!
------------------
Antti Barck
It Solutions Consultant, NSD Oy
[This message has been edited by Antti Barck (edited July 26, 2001).]
[This message has been edited by Antti Barck (edited July 26, 2001).]
 
payal sharma
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class InheritanceTest extends Process {
int x=18;

public static void main(String [] args) {
Process p = new InheritanceTest();
System.out.println(p.InheritanceTest('R'));
System.out.println(p.x);
}

InheritanceTest() {
System.out.println(true ^ true);
}

InheritanceTest(char c) {
System.out.println(c);
}

char InheritanceTest(int i) { //Now this methos get call why?
i='V';
return (char)i;
}
}

class Process {
int x=9;

Process() {
System.out.println("Starting Process...");
}

char InheritanceTest(int i) {
i='S';
return (char)i;
}
}

Thanks in advance
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Payal,
In the code snipplet in the immediate previous post, you have defined a method with a name same as that of a class.It looks like a Constructor but actually it is a method.
Also the method that would be invoked would be dependent on the instance you have created.Since you have created an instance of InheritanceTest class , the method of this class (if overriden) would be invoked.
Hence, the output would be 'V', indicating that the method of InheritanceTest is invoked.
Hope this helps,
Sandeep
SCJP2,OCSD(Oracle JDeveloper),OCED(Oracle Internet Platform)
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
In Payal's first code, the method InheritenceTest is overloaded.


In the second code, it is about overriding, so it is clear.
Thanks,
Vanitha.

[This message has been edited by Vanitha Sugumaran (edited July 26, 2001).]
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think I understand it little bit.
Process p = new InheritanceTest(); Here the reference p is of type Process, in class Process it has only one method and the param type is int. In this case (1st code) it is about overloading, the method which has param of char is not visible for p since it is type of Process. so it is selecting the int method.
if you change it to
InheritanceTest p = new InheritanceTest(), then it will result in compiler error (ambiguous method) there are 2 methods available for p IT(char c) & IT(int i).
Am I right?
Vanitha.
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vanitha,
We get an ambiguous error, if the compiler is not able to understand which method to invoke.The compiler applies the method resolution principle for this purpose.The compiler tries to invoke a specific method instead of a general method.

Consider the following code snipplet.

In the above code the compiler has to decide which method to invoke.
The compiler would do the following analysis

  1. Where are the overloaded methods defined?If all the overloaded methods are in the same class (superclass or subclass) the compiler will not see any ambiguity, as the class where the methods have been defined becomes more specific.
  2. If the method is defined in the subclass and the superclass, then subclass becomes more specific than the superclass.Hence AmbSub(and its methods) are more specific than AmbSuper(and its methods).
  3. Based on the argument you are passing decide on which method is more specific.For example sub.method('A') would treat AmbSuper - method(char) to be more specific than AmbSub - method(int).

  4. Clearly this means Statement 2 is contradicting Statement 3!This confuses the compiler and thus you see an ambiguity error.
    You can get rid of the ambiguity error by placing a *more generic* method in the superclass and the *more specific method* in the subclass.
    The following code snipplet shows this:

    In this case, the compiler would not find any contradictions for the Statements 2 and 3 mentioned above, during analysis.
    Also, note that the following code snipplets will not give any ambiguity errors , as per Statement 1 mentioned above.



    Hope this makes some sense,
    Sandeep
    SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
    [This message has been edited by Desai Sandeep (edited July 26, 2001).]
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sandeep for the detailed explanation. Those 3 steps are helpful.
Vanitha.
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vanitha,
Try out some examples with Object type overloading and see if you are comfortable with method resolution stuff.I believe, this would be out of scope of SCJP2, though!However, it will keep the discussion interesting
-- Sandeep
PS : You may post your post here.We will try and have a creak at it.
-- Sandeep
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wish I have enough time, working on I/O chapter. I feel it is very vague (I/O chapter). When I start revising overloading I will try some more examples.
Thanks,
Vanitha.
 
payal sharma
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sandeep for your detail explaination with code as example.It really help me .As I have problem in expressing my question so vinitha has raised it perfectly well.

payal
 
Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic