• 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

How can toplevel class be abstract?

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public abstract class AbstractTest {
public int getNum() {
return 45;
}
public abstract class Bar {
public int getNum() {
return 38;
}
}
public static void main (String [] args) {
AbstractTest t = new AbstractTest() {
public int getNum() {
return 22;
}
};
AbstractTest.Bar f = t.new Bar() {
public int getNum() {
return 57;
}
};
System.out.println(f.getNum() + " " + t.getNum());
}
}
what is the result?
A. 57 22
B. 45 38
C. 45 57
D. An exception occurs at runtime.
E. Compilation fails.

In the above code, we are having the top level class as abstract..but i was assuming that top level class cannot be abstract.
Can someone explain how can we have top level class as Abstract?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Though the code looks like we are instantiating and abstract class but this is not what is happening. It is an anonymous inner class.



The object referenced by the variable t is a instance of an anonymous subclass of AbstractTest.
[ September 04, 2008: Message edited by: Puneet Nahata ]
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well there are a few different concepts being looked at in this problem. If you don't understand the concept of an abstract class, maybe you should look at a simpler example first, before you go mixing it with anonymous inner classes and such. For example:

So what's happening here is that there is a toplevel class, called MyAbstractClass. It obviously is abstract. One of its methods is implemented (the getNum() method), and one of its methods is abstract (the getString() method).
So you can see here how this works, having a toplevel class abstract. In addition, if you have an abstract class, you MUST extend the class in order to use it, because you CANNOT implement an abstract class on its own. The point here is that whether the abstract class is the topmost class or somewhere down an inheritance tree, it is the parent of something, thus allowing it to be a toplevel class, just like what is demonstrated in this example.
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do undertstand the concept of anonymous inner class.

But in the code we are having an Abstract class(Top0level) AbstractTest and inside the main method we are creating an anonymous inner class which is a subclass of the Abstract class AbstractTest.

My doubt is that whether we can have a main method insde an abstract class. Or, is it we can have only if we are creating an anonymous inner class in it?
As abstract class mean it should be subclassed by class which provides implementation to its abstract method.

Kindly suggest.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My doubt is that whether we can have a main method insde an abstract class. Or, is it we can have only if we are creating an anonymous inner class in it?



Why not? The main() method is not abstract. And the main() method is static, which means that you don't actually need an instance to call it.

As abstract class mean it should be subclassed by class which provides implementation to its abstract method.



Which is what the anonymous inner classes are doing.

Henry
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I do undertstand the concept of anonymous inner class.


Take a look at inner classes.

Hope this helps
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
vidhya, when you copy a question from a book or mock exam, we require that you quote your sources. So, please tell us where you copied it from.
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is from the book Sun Certified Programmer & Developer for Java 2 Study Guide By Kathy Sierra, Bert Bates Chp 8- Pg 485/486.
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry for the explaination. I am now clear about it.
 
Ranch Hand
Posts: 686
Netbeans IDE Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so what is the correct answer just to be sure?
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a option is the correct one.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic