• 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

Please Explain.

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain about the output.
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;
}
}
Options are:
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
And correct option is 7. but y?


------------------
Cheers
Tamanna :-)
 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tammana,
A class can have a memeber method with same name as constructor, so char InheritanceTest(char) is treated as method.
Now Process p = new InheritanceTest();
first executes the base class constuctor-
Starting process
Then derived class constructor, true^true will give-
false[b]
Then comes the println stmt...
p.InheritanceTest('R')
'R' gets converted to int implecitly. there is no method InheritanceTest(int) in derived class(remember it's InheritanceTest(char) not int!!!), so it calls base class InheritanceTest(int) method. So-
[b]s

Now because of p.x , being a base class ref. p does not know 'x' in derived class. So p.x will print base class value of 'x'. So-
9
I hope this'll help.
Rashmi

 
Tamanna Mittal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rashmi,
I very well got the constructor's concept. But the method thing is still not clear.
this statement
Process p = new InheritanceTest();
will initialize the variable p to type derived class
in derive class there is a method

char InheritanceTest(char c) {
c='V';
return (char)c;
}

then y would the char will implicitely convert to int and hence will call the method of the base class. Rest all the code i am clear with but this is the only part i am not clear with. Please help.
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tamanna,
I saw an excellent discussion on this very question here: http://www.javaranch.com/ubb/Forum24/HTML/008132.html

------------------
Percy Densmore
-SCJP2 Wannabee
 
Tamanna Mittal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks pal.
------------------
Cheers
Tamanna :-)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic