aspose file tools*
The moose likes Beginning Java and the fly likes Is it possible to create a class like this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is it possible to create a class like this" Watch "Is it possible to create a class like this" New topic
Author

Is it possible to create a class like this

Dinesh Jayram
Greenhorn

Joined: Nov 26, 2008
Posts: 23
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

Joined: Oct 27, 2005
Posts: 19544
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
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

Joined: Nov 26, 2008
Posts: 23
ok hereafter ill do

now probably looking for reviews
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Don't forget to change your name according to the naming policy. We take the naming policy seriously on JavaRanch.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
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

Joined: Nov 26, 2008
Posts: 23
ill lookout for anonymous class
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to create a class like this
 
Similar Threads
castNotSupportedException
Implementation of Interfaces
Annonymous Inner class doesnt use final var
a method in a private class is accessible
Enumeration