• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jtips question

 
yuan wei
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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

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
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
yuan wei
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i still do not understand why use the (int) version
because there is (char) version of there meathod?
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Asma Zafar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic