aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JTips Question 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 "JTips Question" Watch "JTips Question" New topic
Author

JTips Question

Mindy Hudson
Greenhorn

Joined: Dec 08, 2000
Posts: 28
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

Joined: Dec 08, 2000
Posts: 28
Someone please help..It is urgent
Sri Yamujala
Greenhorn

Joined: Dec 07, 2000
Posts: 18
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


Sri
Mindy Hudson
Greenhorn

Joined: Dec 08, 2000
Posts: 28
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.
Gaurav Mantro
Ranch Hand

Joined: Sep 08, 2000
Posts: 61
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).]


----------------------<br />regards.<br /><a href="http://www.mantrotech.com/editors/gaurav_mantro.asp" target="_blank" rel="nofollow">Gaurav Mantro</a><br /><a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">Java Information Portal</a> - <a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">http://java.mantrotech.com</a>
Bala Arul
Greenhorn

Joined: Feb 09, 2001
Posts: 29
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

Joined: Dec 07, 2000
Posts: 18
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

Joined: Dec 07, 2000
Posts: 18
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

Joined: Dec 07, 2000
Posts: 18
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

Joined: Sep 08, 2000
Posts: 61
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

Joined: Dec 07, 2000
Posts: 18
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 ?

seema ram
Greenhorn

Joined: Feb 15, 2001
Posts: 3
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

Joined: Dec 07, 2000
Posts: 18
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.

Sivalingam Sivasuthan
Ranch Hand

Joined: Jan 12, 2001
Posts: 75
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.


- Siva<br /> Sun Certified Programmer for the Java™2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTips Question