• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it possible to create a class like this

 
Dinesh Jayram
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok hereafter ill do

now probably looking for reviews
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ill lookout for anonymous class
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic