Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract class problem

 
Pankaja Shinde
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abstract class Transaction implements Runnable { }

class Deposit extends Transaction
{
protected void process()
{
addAmount();
}
void undo(int i)
{
System.out.println("Undo");
}
}


What will happen if we attempted to compile the code?

Select the one right answer.

1. This code will not compile because parameter i is not used in undo().
2. This code will not compile because there is no main() method.
3. This code will not compile because Deposit must be an abstract class.
4. This code will not compile because Deposit is not declared public.
5. Everything will compile fine.

Correct answer : 3

Why ?

pankaj
 
Srinivas Kumar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. The answer is correct because the class Deposit does not implement public void run() method which is declared in Runnable interface.
Any nonabstract class implementing directly/indirectly the Runnable interface should implement the run() method.
 
Antonio Trapero
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2 reasons:

-->First, it has to be abstract. Not implements public void run().
-->Second, look at call to addAmount(); the method is not define. Even class
was abstract , you will still have compilation error.

Oh, dude for me, in third person of conditional, is "class was", or "class were" sorry for my english
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pankaj,

First you have not given definition to the addAmount() method so it yields to compilation error and if you missed it anyway the answer is obviously 3,
the class also must be abstract to compile successfully because it does not implement the run() method. To compile it successfully either implement the run() method properply (public void run(){}) or make your class abstract to leave the burden of implemetation of interface and abstract class's abstract methods on the first concrete class.

Thanks and regards,
cmbhatt
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic