• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Akhilesh Yadav
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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
 
Sreeram Desigan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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.
 
Changchun Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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
 
Sandeep Singh Kunwar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • 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
 
Vishal Chawla
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic