File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Mock Exam Errata 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 » Mock Exam Errata
Bookmark "jtips question" Watch "jtips question" New topic
Author

jtips question

yuan wei
Greenhorn

Joined: Aug 08, 2001
Posts: 4
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

the answer is 7
i wanna to know that why char InheritanceTest(int i) in process
is invoked ?
and can any tell me where can i find the MindQ's answars?
Thank you
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Yuan,
The superclass method is being called as 'R' is seen by the compiler as an 'int'.
JLS §15.12.2.1

The type of each actual argument can be converted by method invocation conversion (�5.3) to the type of the corresponding
parameter. Method invocation conversion is the same as assignment
conversion (�5.2), except that constants of type int are
never implicitly narrowed to byte, short, or char.

JLS §5.3

Method invocation conversions specifically do not include the implicit narrowing of integer constants which is part of
assignment conversion (�5.2). The designers of the Java programming language felt that including these implicit
narrowing conversions would add additional complexity to the
overloaded method matching resolution process (�15.12.2). Thus,
the example:
class Test {
static int m(byte a, int b) { return a+b; }
static int m(short a, short b) { return a-b; }
public static void main(String[] args) {
System.out.println(m(12, 2));
// compile-time error
}
}
causes a compile-time error because the integer literals 12 and
2 have type int, so neither method m matches under the rules of
(�15.12.2). A language that included implicit narrowing of
integer constants would need additional rules to resolve cases
like this example.

Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
yuan wei
Greenhorn

Joined: Aug 08, 2001
Posts: 4
but i still do not understand why use the (int) version
because there is (char) version of there meathod?
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Yuan,
In the example, the argument 'R' is implicitly handled, by the compiler, as an <code>int</code>.
There are two methods <code>InheritanceTest(int i)</code> and <code>InheritanceTest(char c)</code>. Parameter invocations do not use narrowing conversion i.e. the compiler will not check if the argument can be narrowed to a <code>char</code>. As the <code>InheritanceTest(int i)</code> method can handle the invocation; it is selected.
Note that even if you add a line <code>char ch = 'R';</code> and pass the variable <code>ch</code> as the argument; the <code>InheritanceTest</code> method in <code>Process</code> will be still be used.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
Did a great job Jane, I have just one question, here in the subclass there was a constructor and a method with the same name, isn't it confusing (it looks like it is allowed).
===================================
InheritanceTest(char c) {
System.out.println(c);
}
char InheritanceTest(char c) {
c='V';
return (char)c;
}
==================================
--Farooq


Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Asma Zafar
Ranch Hand

Joined: May 11, 2001
Posts: 49
A simple procedure to remember is :
1) All methods are invoked according to the object type of the variable, but for that the same signature method has to exist in the reference type otherwise its a compiler error.
2) All variables (static, nonstatic) are invoked according to the reference type of the variable.
Here the reference type of p is Process and object type is InheretanceTest.
The program invokes with a char argument, and you see there is no char argument method in Process. But still compiler promotes char to int and thereby maintains its rule for method invoction.
if you add the follwoing method in Process, then the method invoked will be from InheretanceTest
char InheritanceTest(char c) {
c='V';
return (char)c;
}

I know its confusing but if you give it some time you'll understand. Hope it helps.
Asma Zafar.
Sun Certfied Programmer for Java2 Platform.


Asma Zafar,<BR>Sun Certified Programmer for Java2 Platform
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Muhammad,
Sorry for the late reply, just saw your question.
You can create ctors and methods with the same name. Java is able to differentiate between the two; a ctor has the same name as the class and a method has a return type.
It's not recommended though 'cause it is confusing.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
wood burning stoves
 
subject: jtips question
 
Similar Threads
Please Explain.
JTips Question
doubt to previously answered que
jtips ques
refrencing method not working