• 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

abstract class problem

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic