• 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

Is it possible to create a class like this

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to create a class like this?

class RetuEnum$1 extends java.lang.Object implements java.util.Enumeration {
RetuEnum$1(RetuEnum); //constructor should not be declared
public boolean hasMoreElements(); //this method should be defined
public java.lang.Object nextElement();//this method should be defined

}

actually it(RetuEnum$1) is a auto compiled class; created during retruning of a Interface. I created a class/method that returns Enumeration(interface) class. The code is

return new Enumeration(){
public boolean hasMoreElements(){
return true;
}
public Object nextElement(){
return new Object();
}
};

If we compiled the above code, RetuEnum$1 class is automatically created, it has methods with lot of doubts. See above RetuEnum$1 class
1. how a class can have constructor declared?
2. how a class can implement a interface but does not defined inside?

thanks
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the concept of anonymous classes. Also, every class has a constructor; if you don't define one the compiler does it for you.

And while you're at it, please check our naming policy. That's one of the few rules that we really enforce.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch Rob has already told you about the naming policy. Please find the code button and maintain indentation; it makes your posts much easier to read.
 
Dinesh Jayram
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok hereafter ill do

now probably looking for reviews
 
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
Don't forget to change your name according to the naming policy. We take the naming policy seriously on JavaRanch.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Beware of a hasNext() method which always returns true, and a next() method which creates a new Object() each call. Just imagine how that will behave in a while loop

What you have is an anonymous class; we discuss them every now and again, and you can find the older threads by searching for "anonymous class" with the link above. You can take a class (abstract or concrete) or an interface and implement/override a method and then write new Foo(). They are most commonly used for Runnables and for Listeners in Swing. You can override any method, but you can't write a constructor. So you have to use the same constructor as the original class/interface; if there is no written constructor then you get a default constructor.

There will be lots more information in the old threads; please search the forum.
 
Dinesh Jayram
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ill lookout for anonymous class
 
reply
    Bookmark Topic Watch Topic
  • New Topic