• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTips Question

 
Greenhorn
Posts: 28
  • 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) {
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;
}
}
What is the Output?
1.Prints Starting Process �, false, �S� and 18
2.Prints false, �V� and 9
3.Prints true, �V� and 9
4.Prints Starting Process � , true, �V� and 9
5.Prints Starting Process �, false, �V� and 9
6.Prints Starting Process �, false, �V� and 18
7.Prints Starting Process �, false, �S� and 9
8.Prints Starting Process �, true, �R�, and 18
9.Prints Starting Process �, true, �V� and 18
I think the answer should be 5.But the correct answer is 7.Since p has its class as InheritanceTest shouldnt the method in InheritanceTest be called.Can you explain it to me.
 
Mindy Hudson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Someone please help..It is urgent
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I actually think that the answer should be
Starting Process....., false, V, 18
Althoug p is an object of Process type, since it has a reference to InheritanceTest type, unless you use 'super', nothing should be called from Process class. Isn't that right? I tried to compile it and I got Starting process..., false, S and 9 - why? Can someone explain please?
Thanks,
Sri
 
Mindy Hudson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sri..I can explain to u what I understand.As p is declared as
Process p = new InheritanceTest();
p is of type Process but class InheritanceTest.ie the constructor of InheritanceTest gets called.And for a subclass when there is always an implicit call from the subclass constructor to superclass noarg constructor if we dont specify any super(arg) calls.
Then methods are called by class and variables by type.So according to that I think char InheritanceTest(char c) in InheritanceTest shouldbe executed instead of char InheritanceTest(int i) in Process.Anyway when I compile it I am getting S.Howcome.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmn there are many questions here let me put the code here first and then try to explain what's happenning. If I am wrong or something needs more explaination then I have written do mention.

Ok it took me some amount of time to put the line numbers here, can JavaRanch provide similar functionlaity in as it would be pretty helpful.
At line 7 an object of InheritanceTest is created but refernced as object of Process P.
At line8 p.InheritanceTest('R') Should have invoked InheritanceTest() method of InheritanceTest object instead of Process object b'coz of overriding. But the trick is how Java handles it. Java internally converts it to int first and will look for a function matching argument list.
So at line 8 java prefers a function InheritanceTest(int x) over a function InheritanceTest (char c). It finds a InheritanceTest(int) in Process class which is not being overridden. You can test the same. Change the signature of InheritanceTest at line 22 and make it InheritanceTest(int c). Check the results.
Sri
p is not an object of Process type it is an object of InheritanceTest and is being type cast or referred as object of Process type.
Due to being casted as object of Process type p.x at line 9 means x of Process because overriding works on methods only.
Correct me if I am wrong.
regards
Gaurav Mantro
------------------
http://www.mantrotech.com
[This message has been edited by Gaurav Mantro (edited February 15, 2001).]
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
'p' is a reference type of process. In order to polymerphism work, the particular method should be over-riden. But,
'char InheritanceTest(char c)' in subclass is different from 'char InheritanceTest(int i)' in the super. That means the super method is not over-riden. Also a character can be passed into an 'int' argument.
If you wanted to have the desired result, the argument in the both methods should be same, no matter 'int' or 'char'.
Arul
 
Sri Yamujala
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gaurav and Mindy,
Thank you for the clarification. My bad - you are right, over-riding works only for methods. That explains.
I've got a question on a totally different thing, though. If I want to reply to a post, how do I get the code from that post to be included in my reply, other than cut and paste method and how do I get indenting for this code.
Thanks Again,
Sri
 
Sri Yamujala
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gaurav,
Alright, I reversed both the InheritanceTest methods in the subclass and super class i.e., the InheritanceTest method in the subclass now takes an int argument and the same method in the super class Process takes a char argument.
The code:

If I execute, from your explanation, the answer now should be
Starting Process...., false, V and 9, but the answer still is Starting Process...., false, S and 9 - why?
Thanks,
Sri

Ok it took me some amount of time to put the line numbers here, can JavaRanch provide similar functionlaity in as it would be pretty helpful.
At line 7 an object of InheritanceTest is created but refernced as object of Process P.
At line8 p.InheritanceTest('R') Should have invoked InheritanceTest() method of InheritanceTest object instead of Process object b'coz of overriding. But the trick is how Java handles it. Java internally converts it to int first and will look for a function matching argument list.
So at line 8 java prefers a function InheritanceTest(int x) over a function InheritanceTest (char c). It finds a InheritanceTest(int) in Process class which is not being overridden. You can test the same. Change the signature of InheritanceTest at line 22 and make it InheritanceTest(int c). Check the results.
Sri
p is not an object of Process type it is an object of InheritanceTest and is being type cast or referred as object of Process type.
Due to being casted as object of Process type p.x at line 9 means x of Process because overriding works on methods only.
Correct me if I am wrong.
regards
Gaurav Mantro[/b]
</BLOCKQUOTE>

[This message has been edited by Sri Yamujala (edited February 15, 2001).]
 
Sri Yamujala
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys, any thoughts ???

Originally posted by Mindy Hudson:
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) {
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;
}
}
What is the Output?
1.Prints Starting Process �, false, �S� and 18
2.Prints false, �V� and 9
3.Prints true, �V� and 9
4.Prints Starting Process � , true, �V� and 9
5.Prints Starting Process �, false, �V� and 9
6.Prints Starting Process �, false, �V� and 18
7.Prints Starting Process �, false, �S� and 9
8.Prints Starting Process �, true, �R�, and 18
9.Prints Starting Process �, true, �V� and 18
I think the answer should be 5.But the correct answer is 7.Since p has its class as InheritanceTest shouldnt the method in InheritanceTest be called.Can you explain it to me.


 
Gaurav Mantro
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I was partially wrong in my answer earlier. I overlooked some of the things, my bad.
Bala gave the right answer.
Since InhertitanceTest() in Process has int argument and InheritanceTest() in InheritanceTest has char argument, there would be no overriding. Since char can be converted to int the InheritanceTest method of Process is invoked.
So as long as InheritanceTest() in the process has argument to which char can be converted implicitly and that method is not overridden in InheritanceTest class, InheritanceTest of Process class will be invoked.
Sri did that help ?

------------------
http://www.mantrotech.com
[This message has been edited by Gaurav Mantro (edited February 15, 2001).]
 
Sri Yamujala
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep - thank you very much, Gaurav

Originally posted by Gaurav Mantro:
OK, I was partially wrong in my answer earlier. I overlooked some of the things, my bad.
Bala gave the right answer.
Since InhertitanceTest() in Process has int argument and InheritanceTest() in InheritanceTest has char argument, there would be no overriding. Since char can be converted to int the InheritanceTest method of Process is invoked.
So as long as InheritanceTest() in the process has argument to which char can be converted implicitly and that method is not overridden in InheritanceTest class, InheritanceTest of Process class will be invoked.
Sri did that help ?


 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dint completely understand the explanation.As the class is InheritanceTest I thought it will first look into InheritanceTest for the corresponding method.And since InheritanceTest(char c) was there would execute it.So the question of overriding never comes in.Can somebody correct me.
 
Sri Yamujala
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question of over-riding never comes in, in this case because both the methods in the subclass and superclass have different parameter types. This is actually a case of overloading, where the subclass is providing the same method as in its super class, but with a different type of parameter.
Hope this helps.
Sri

Originally posted by seema ram:
I dint completely understand the explanation.As the class is InheritanceTest I thought it will first look into InheritanceTest for the corresponding method.And since InheritanceTest(char c) was there would execute it.So the question of overriding never comes in.Can somebody correct me.


 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!
I think Like this ..............
At the compile time Compiler can't see the InheritanceTest's 'char InheritanceTest(char c)' method(Process doesnt have that method). So It binds the Process's 'char InheritanceTest(int i)' method. Then at the Runtime There won't be any Dynamic binding because there were no Overriding (Only OverLoading). So Simply You will get the 'S' when you call p.InheritanceTest('R');
Siva.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic