-2

I would like to use the following code from this question in order to sort Properties alphabetically:

Properties tmp = new Properties() {
    @Override
    public synchronized Enumeration<Object> keys() {
        return Collections.enumeration(new TreeSet<Object>(super.keySet()));
    }
};

However, instead of instantiating a Properties object and every time indicating that I want it to be alphabetical, how could I make a class that inherits Properties, for the sole purpose of explicitly overriding this method?

public class MyProperties extends Properties
{
    @Override
    public synchronized Enumeration<Object> keys() {
        return Collections.enumeration(new TreeSet<Object>(super.keySet()));
    }
}

This is in hopes of being able to do this:

MyProperties p = new MyProperties();

... and have it work exactly like a Properties object whose keys are sorted!

Somehow I don't feel this is that easy, is it? Do I need to do things like call the super's constructor? It's just a little unclear to me but I have a feeling it's possible to do. I'd rather not override a method every time I make a Properties object!

Thanks!

Community
  • 1
  • 1
Drifter64
  • 1,103
  • 3
  • 11
  • 30
  • 6
    You've written a very complete question, which is nice to see, however you could have just tried it ;-) Your code will compile because `Properties` has a default constructor; had this not been the case you'd have got an error at compilation time (or in your IDE) – Nick Holt Jul 15 '14 at 15:24
  • You raise a decent point. I guess i am just not knowledgeable in this particular use of java, and a little hesitant to jump into coding it without knowing what I'm doing. – Drifter64 Jul 15 '14 at 15:27
  • 1
    Always give it a go - what's the worse that can happen? You fail, work out what's wrong and learn a lot of interesting things along the way :-) – Nick Holt Jul 15 '14 at 15:27
  • I'm voting to close this question as off-topic because the code posted is the solution itself, so the question does not help users of this site. – Drifter64 Jan 23 '15 at 07:51

2 Answers2

0

You can do exactly what you've written above; the example class you wrote will compile and do what you're looking for. Defining types with one or two slight differences from some superclass is one of the most common uses of inheritance![Citation Needed]

Henry Keiter
  • 16,863
  • 7
  • 51
  • 80
0

It seems my thinking was entirely correct! How lovely Java is!

The code will compile because all the methods and constructor of the Properties class are automatically inherited and enacted by extending that class. I did not need to write anything special to dictate that these methods are there, and any further methods written in the sub class either override or append to the original parent class.

Cheers to Nick Holt for reminding me that i wont break anything by trying out an idea. Sometimes its hard to take that leap and some programmers are hesitant about just trying out a piece of code if they aren't sure.

Drifter64
  • 1,103
  • 3
  • 11
  • 30