• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

WindowAdapter class is abstract then how can you instantiate it?

 
Greenhorn
Posts: 22
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public abstract class WindowAdapter is abstract class then how can you instantiate it.
the code given below works fine with all my program
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
});
this code works,how it can work when WindowAdapter class is abract and still we instantiate it (abract calsses can't be instantiates)
please help i am really confused.
bye

 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bhakti,
In your example, you're instantiating an anonymous class from the WindowAdapter class and you provide an override method for the windowClosing method. This is perfectly fine, because you are not keeping a reference of the WindowAdpater class around.
E.g.

The second example is fine, because I'm creating a reference to an anonymous class. Remember, that this anonymous class is a concrete class because I'm providing an implementation for one of the methods in the WindowAdapter class.
I hope this makes sense. I realize the concept is confusing, and takes some getting use to.
-Peter
 
bhakti soman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hi thanks for the help but as you said this concept is confusing
you are saying that we are not instanciating object of abract class WindowAdapter but we are instantiating an anonymous class from the WindowAdapter class ???
if we are saying new WindowAdapter that means we are creating object of that class right then ???
please explain
thanks

Originally posted by Peter Tran:
[B]Bhakti,
In your example, you're instantiating an anonymous class from the WindowAdapter class and you provide an override method for the windowClosing method. This is perfectly fine, because you are not keeping a reference of the WindowAdpater class around.
E.g.

The second example is fine, because I'm creating a reference to an anonymous class. Remember, that this anonymous class is a concrete class because I'm providing an implementation for one of the methods in the WindowAdapter class.
I hope this makes sense. I realize the concept is confusing, and takes some getting use to.
-Peter[/B]


 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Bhakti..
You are not creating a n instance of abstract class but
you r extending that abstract class n creating an object of that
on the fly ..I mean instantiating it..
ie anonymous class object..
Refer Khalid book for inner class concepts...
ViraL...
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think WindowAdapter class is not an abstract class. It implements WindowListener and overrides all the methods in WindowListener. These methods just have no statements and thus do nothing when invoked.
Let me know if I am wrong.
 
Weigang Gu
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I just checked API doc and you are right WindowAdapter is an abstract class. However, there is no abstract method in this class. I am wondering why they declare it as abstract.
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The class is abstract, but there are no abstract methods in the class. It is abstract because all of the methods are blank, so if it wasn't abstract, it would do nothing. They made it abstract to force you to extend it.
The most common way of doing this is with anonymous classes like in bhakti's example.
Bill
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The WindowAdapter class is abstract, but the implementation here is based on the anonymous inner classes. It just extended the WindowAdapter to have a new class.
So basically the WindowAdapter is extended, not instantiated.
Hope this helps.
Ji
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In your example:
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
});
// new WindowAdapter(). It says to compiler" Create an instance
of a new, unamed class which implements the WindowListener
interface...
Look here you are making an instance using new but without any object referece name . So it is unnamed class. So abstract
can be anonymous class.
Whatever you say after the "new" as in "new Something()", if Something is an interface, then the anonymous
class implements that interface (and must define all the
methods of that interface). But if Something is a class,
then your anonymous class automatically becomes a subclass of that class. This is perfect for the event adapter
classes like WindowAdapter.
- Golam Newaz
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
abstract classes can have 0 abstract methods. abstract classes merely say that we eventually will extend the class. the fact that we have fully implemented methods in an abstract class does not conflict with this notion. however, even 1 abstract method makes the entire class abstract because an unimplmented method implies that the class is unfinished...so it must be abstract.
as far as new WindowAdapter(), you're not instantiating WindowAdapter class at all...you're creating an anonymous inner class (an extension of the abstract WindowAdapter) and immediately instantiating THAT child class, which of course, is itself not abstract at all.
 
bhakti soman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
thanks for the reply
i shall say what i have understood correct me if i am wrong
i feel that by saying addWindowListener(new WindowAdapter(){}); you are just creating a anoymonous class and by using new your anoymous class just extends this WindowAdapter class
hope this is right
bye

Originally posted by Golam Newaz:
Hi,
In your example:
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
});
// new WindowAdapter(). It says to compiler" Create an instance
of a new, unamed class which implements the WindowListener
interface...
Look here you are making an instance using new but without any object referece name . So it is unnamed class. So abstract
can be anonymous class.
Whatever you say after the "new" as in "new Something()", if Something is an interface, then the anonymous
class implements that interface (and must define all the
methods of that interface). But if Something is a class,
then your anonymous class automatically becomes a subclass of that class. This is perfect for the event adapter
classes like WindowAdapter.
- Golam Newaz


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic