1

Say I extend a Java class like java.util.List to create a custom class called MyList... Is there any way if my peers write code using List, I can convert it into MyList during compilation/runtime?

i.e. If they have something like:

List groceryList = new List();

it should be compiled/run like:

MyList groceryList = new MyList();

I know annotations can do something of this sort. Is it possible to use them in this case? If yes, how?

Rajshri Mohan K S
  • 1,557
  • 17
  • 30
  • Why don't you just copy all the stuff from `List` to a new `MyList`? – Sweeper Jul 22 '15 at 10:48
  • @Sweeper: I don't want to modify code written by others. MyList is not going to change List in any functional way. Except that it has some additional convenience tweaks. – Rajshri Mohan K S Jul 22 '15 at 10:50
  • Well as is in my answer, you dont have to modify their code, just use their lists to create new MyLists in your own code. – milez Jul 22 '15 at 10:54

2 Answers2

0

This is known as downcasting.

The short answer is that if they created the object with the new command, such as in your example, then no, you will not be able to cast it.

What you can do, is define a constructor in MyList class that accepts the object-to-be-converted as a parameter and uses it to create a new object. The list is a bit special because you can create new lists from existing ones . For example:

public MyList (List oldList)
{
    super(oldList); //this will copy the objects of the list to your custom list
    //But you could do any other operations to create the new object from the parameter(s)
    ...
}

and use it as

MyList groceryList = new MyList(oldList);
Community
  • 1
  • 1
milez
  • 2,201
  • 12
  • 31
  • But this will mean that I've to use MyList everywhere. What I want is if my peers use List, I want it to be compiled as MyList. I have done certain enhancements in MyList which my peers aren't going to use directly. But I'd like their code to be compiled as such. Downcasting doesn't satisfy that need. – Rajshri Mohan K S Jul 22 '15 at 10:48
  • 1
    But there would be compile errors from the enhancements or new methods since the compiler does not recognize them. But I think I got the idea.. I'm not aware of a compiler capable of this. You would somehow like to annotate the List objects to be MyLists.. – milez Jul 22 '15 at 10:52
  • And also you would not have to use MyLists everywhere, since you can save a MyList to a List variable. – milez Jul 22 '15 at 10:56
  • But what if I don't want any List objects in the bytecode at all? I want only MyList objects in the end result. The example I gave here is with List. In real usage it could be any custom class which I may not want to expose to others. – Rajshri Mohan K S Jul 22 '15 at 10:58
  • Then we will have to wait for someone smarter than me to answer :) – milez Jul 22 '15 at 10:59
  • I don't know if what I'm asking is possible even. But thanks to you, I learned about downcasting. :) I didn't know it existed before. – Rajshri Mohan K S Jul 22 '15 at 10:59
0

While upcasting in java is mostly safe, Downcasting in Java is not something you should frequently use, unless really needed, since the subclass may not support some attributes or methods that the superclass can.

Community
  • 1
  • 1
Maslor
  • 1,821
  • 3
  • 20
  • 44