• 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

Question Frm k&B chp 5 Self test Qno :- 8

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class A {
public int baz(int x ) {
System.out.println("A");
return x*2;
}
}


public class B extends A{
public static void main(String [] args){
B a = new B();
long x = a.doStuff(7);
System.out.println(x);
}

public long doStuff (int x){
return x*3;
}
}

As per K&B Answer is Compliatiopn Fail at line

public long doStuff(intx)

but when i wrote the prog and complied it and run it its giving out put 21
iam bit confused which answer is right 21 or compilre time error
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are correct there is no error in the about code.
I guess compiler error would have occured if the method baz(int) in base class (A) was actually dostuff(int). In that case the derived class B is trying to override dostuff method in A with a long return type. Which is an error.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe you will complie and run it again

you should notice the methods doStuff() in class A and CLASS Bit is wrong way to override the method
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are correct there is no error in the about code.
I guess compiler error would have occured if the method baz(int) in base class (A) was actually dostuff(int). In that case the derived class B is trying to override dostuff method in A with a long return type. Which is an error.


Exactly
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Akhilesh,

I'm really confused, because the code you showed us is different than the code in the book?

BTW, this question is from the older 1.4 book, not the 1.5 book.

Can you take another look and let us know what's going on?

Thanks,

Bert
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now if a question like this is on the exam, do we assume that the class A and B are in different files or do we assume that they are in the same file. If they are in the same file, then wont we get compile time error for having two public classes in the same file.
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the real exam all the code listings in the multiple choice questions will have line numbers. If more than one class is listed, and the line numbers don't repeat, then the classes are in the same file.
 
Vishal Chawla
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bert Bates:
If more than one class is listed, and the line numbers don't repeat, then the classes are in the same file.



wait.. if more than one class is listed and the line numbers dont repeat, then they should be in a different file right.. I mean if line numbers repeat, then they should be in the same file..
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's look at two examples:

Example 1: two classes in one file



Example 2: two classes in two files



hth,

Bert

p.s. BTW we still don't really know what the original question on this thread was all about
[ April 11, 2006: Message edited by: Bert Bates ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic