javasyntax

Class overloading in Java - is it possible?


Instead of using traditional polymorphism ... extends LinkedList<T>, is there a way to overload on a class such as String, List or a custom MyClass class, in Java?.

theList.doRandomShuffle(123); or theList.persistOnFile("fileName");

Mixing C code to achieve this is on the table if possible. Any further suggestions will be useful.

UPDATE: OK. String class was not a good example. The purpose was to extend a class without declaring a new list MyList extends LinkedList<T> or creating a utility class for the wanted function. I am eliminating two possibilities of extension to achieve and asking if there is more option even if it is hardcore.

The reason beyond this is to question if possible to extend classes especially for jar libraries without adapting to the new extended classes of LinkedList impl. whatsoever. I am really disappointed by the community that no one knows for sure, thinks even questioning should be out of the box


Solution

  • In Java, you cannot add a method to an already defined class such as String. However, if the class is not final, you can extend it to create custom methods.

    (Technically, you can add methods to defined classes, but it's a hassle and would be a breach of Java's terms and conditions of use.)