   Small Type casting query
  Just to help get my head around this, I started making a little program to see if I could get the gist of type casting using an interface and it works, but just some quick questions.Code:    Length larger = (CLength)findLargest(length1, length2);    public static Object findLargest( Object object1,Object object2 )    {   IRelatable relatableObj1 = (IRelatable)object1;   IRelatable relatableObj2 = (IRelatable)object2;   return relatableObj1.isLargerThan(relatableObj2);    }length1 and length2 are of type CLength, and I send it through to findLargest which is of type Object...the object of all objects. now from the tutorials i was following the function invocation above (findlargest) was explicitly type casted to the type we wanted object to be(CLength), but i found you didnt need to and i assume thats because it goes by the type that is returned at the end because the objects are casted to be IRelatable types, which is an interface, therefore we can call the "isLargerThan()" method and since Clength implements interface, polymorphism occurs and it finds the CLength version of isLargerThan through the CLength objects passed in, which returns a CLength object naturally therfore foregoing the need to type cast the findLargest function in the beggining(). So i can only assume type casting at that point on the part of the example is for clarity. If there is another reason please tell me?so i suppose what im asking is that is my little paragraph of whats occuring is correct? and any corrections would be appreciated.   
  Try to express yourself a bit clearer in the future: use the right terminology (findLargest does not have type Object; it accepts two arguments of type Object and has return type Object), make sure your explanation doesn't contain any errors (Length vs CLength vs Clength), mention which language you're using, use clearer English (it's not everyone's native tongue you know), etc.I'm assuming the language is java or C# and that your question is "is the cast to CLength mandatory on the first line of my code?". The answer is yes. If you drop the (CLength) bit, the compiler will only know you're assigning an Object (which could be anything, a string, a list, ...) to a variable with type CLength. So, without the cast, you get a compiler error.Another problem is that isLargerThan sounds like the name of a predicate, i.e. something that returns bool(ean), so the actual thing being returned is probably not a CLength object, but a true or a false. This means that when running your code, it will try to convert a bool(ean) to a CLength, which will result in a runtime error.The code I think you're looking for ispublic static IRelatable findLargest(IRelatable x, IRelatable y){  if ( x.isLargerThan(y) ) return x;  else return y;}CLength larger = (CLength) findLargest(length1, length2);Here, I changed the types of the arguments: why tell you only need Objects while in fact you want IRelatables? Then there is no need anymore for casting it inside your method, and the compiler will help you find more bugs. Then, assuming isLargerThan just checks if one object is larger than another, you need the if statement to return the correct object. The cast to CLength is still needed on the last line. (You could get rid of that cast by using generics, but that's a bit more advanced.)   ;  sorry for that I apologize for the lack of specifics, I am indeed using java. Also that last bit on changing my findLargest() function was helpful and insightful thank you. but there is one thing your not entirely correct about but that probably due to my lack of articulation. isLargerThan is just a signature for a function which I created, and its an interface, so it will do whatever I tell it too in the context of which class is defining its behaviour, and I ask it to return the object that is calculated as the largest. So no run time error is produced. HOWEVER what surprised me is your answer to whether i had to type cast and you said i did otherwise a runtime error would occur..yet my code runs fine. here.in main:   CLength length1 = new CLength( 27 );   CLength length2 = new CLength( 24 );   Object obj = (CLength)findLargest( length1,length2 );   System.out.println( obj );    public static Object findLargest( Object object1,Object object2 )    {   IRelatable relatableObj1 = (IRelatable)object1;   IRelatable relatableObj2 = (IRelatable)object2;   return relatableObj1.isLargerThan(relatableObj2);    }in CLength:    public Object isLargerThan(Object object)    {   return this.m_iLength > ( (CLength)object ).m_iLength ? this : object;    }Again when i dont cast the  Object obj = (CLength)findLargest( length1,length2 );and its like this.Object obj = findLargest( length1,length2 );it still works, so why is contrary to what you just told me?;Quote:Original post by homojediAgain when i dont cast the  Object obj = (CLength)findLargest( length1,length2 );and its like this.Object obj = findLargest( length1,length2 );it still works, so why is contrary to what you just told me?Because now you're assigning to a variable of type Object instead of type CLength.Object o1 = findLargest(length1, length2); // okObject o2 = (CLength) findLargest(length1, length2); // ok, unnecessary castCLength l1 = findLargest(length1, length2); // ErrorCLength l1 = (CLength) findLargest(length1, length2); // okWhat tutorial are you following?   ;  the java.sun tutorials i believe. here http://java.sun.com/docs/books/tutorial/java/IandI/interfaceAsType.htmlhowever the application im programmiong at the moment wasent explicitly asked form the tutorial it was for myself to get my head round using interfaces as types and what have you. It has also given me a deeper insight into how classes and inheritance work.for my own benefitCLength l1 = findLargest(length1, length2); // Errorproduces an error because findLargest returns an object, now even though pretty much every class you create derives from object and are inextricably linked somehow this does not work because CLength IS A object, however object is not NECESSARILY a CLength, therefore if we were to make it work we have to TypeCast the return type during the invocation of the function which you have done withCLength l1 = (CLength) findLargest(length1, length2); //okor if we wished too... type cast the return type of findLargest() to (CLength) right before it is returned. So.Clength lengthObj = relatableObj1.isLargerThan(relatableObj2);return lengthObj;although that would make it unflexible and would loose its robustness and would more than likely invoke the wrath of java and c++ deities alike.   ;  About the tutorial: I don't understand the reasoning behind its examples, to me, they're just ... crap. They're basically turning java into a dynamically typed language.Quote:Original post by homojedifor my own benefitCLength l1 = findLargest(length1, length2); // Errorproduces an error because findLargest returns an object, now even though pretty much every class you create derives from object and are inextricably linked somehow this does not work because CLength IS A object, however object is not NECESSARILY a CLength, therefore if we were to make it work we have to TypeCast the return type during the invocation of the function which you have done withCLength l1 = (CLength) findLargest(length1, length2); //okYes.Quote:or if we wished too... type cast the return type of findLargest() to (CLength) right before it is returned. So.Clength lengthObj = relatableObj1.isLargerThan(relatableObj2);return lengthObj;although that would make it unflexible and would loose its robustness and would more than likely invoke the wrath of java and c++ deities alike.It's hard to say anything sensible about this. The design choices made in the example are just plain weird and don't make any sense to me. My implementation would be:interface Relatable{  boolean isLargerThan(Relatable r);}class Length implements Relatable{  private int length;  boolean IsLargerThan(Relatable other)  {    return length > ((Length) other).length;  }}class SomewhereElse{  public static Relatable findLargest(Relatable x, Relatabe y)  {    return x.isLargerThan(y) ? x : y;  }}No superfluous casts and no information being thrown away (the least amount possible without using generics anyway).;  Ah yes, well this exercise was just to get me to understand what they were talking about as for using interfaces as types. And i suppose using the interface that is being implemented as the object rather than returning a generic object ( is this what you are referencing to about generics? ) since Interfaces can be of any type  provided that it is implementing it. And i certainly see the benefit of your example considering typecasting cause loss of information.   ;  I understand you just want to learn about interfaces, but the way that tutorial uses them is just horrible. Why the obsession with Object everywhere? Using Object is a last resort solution, where you just give up and say "no clue about what it could be", but that certainly isn't the case here.Using generics would look like this:public interface Relatable<T>{    boolean isLargerThan(T other);}public class Length implements Relatable<Length>{    private int _length;   public boolean isLargerThan(Length other)    {   return _length > other._length;    }}public class Somewhere{    public static <T extends Relatable<T>> T findLargest(T x, T y)    {   return x.isLargerThan(y ) ? x : y;    }}This way, no type information is lost anywhere and no casts are necessary. It imposes some limitations on the types you can compare however.   