Hi,
What precisely is "array covariance"? I've heard/read numerous things, but nothing conclusive.
My first encounter with this term was in regard to method overriding vs. method overloading. A method in a subclass will NOT override a method in a superclass if, say, its input parameter is a subclass of the corresponding method in the superclass. Rather, the method will be overloaded. Example:
In this admittedly basic example, c2 now has two speak methods (original plus overloaded). So, if my input parameter and return-type had been arrays instead (of types p1 and p2, respectively) would c2.speak have overridden the c1.speak? That doesn't seem to comport with what I get when I experiment... I hear a lot about "array covariance" in java - if someone could explain it, I'd be grateful.
Online, I found examples that lead me to the following generalization of covariance/contravariance...
assume we have a class DOG, and a class POODLE.
in a covariant type-system, since all poodles are dogs poodles would implement a dog interface. however, in a contravariant type system, dogs would implement interfaces of all things-that-are-dogs - poodles, schnauzers, etc. Is this correct? If not, what is?
Thank you - it will be nice to have this cleared up!
dora c.
What precisely is "array covariance"? I've heard/read numerous things, but nothing conclusive.
My first encounter with this term was in regard to method overriding vs. method overloading. A method in a subclass will NOT override a method in a superclass if, say, its input parameter is a subclass of the corresponding method in the superclass. Rather, the method will be overloaded. Example:
Code:
class p1
{
};
class p2 extends p1
{
};
class c1
{
public p1 speak( p1 pIn )
{
System.out.println( "in c1.speak" );
return new p1();
};
};
class c2 extends c1
{
public p2 speak( p2 pIn )
{
System.out.println( "in c2.speak" );
return new p2();
};
};
In this admittedly basic example, c2 now has two speak methods (original plus overloaded). So, if my input parameter and return-type had been arrays instead (of types p1 and p2, respectively) would c2.speak have overridden the c1.speak? That doesn't seem to comport with what I get when I experiment... I hear a lot about "array covariance" in java - if someone could explain it, I'd be grateful.
Online, I found examples that lead me to the following generalization of covariance/contravariance...
assume we have a class DOG, and a class POODLE.
in a covariant type-system, since all poodles are dogs poodles would implement a dog interface. however, in a contravariant type system, dogs would implement interfaces of all things-that-are-dogs - poodles, schnauzers, etc. Is this correct? If not, what is?
Thank you - it will be nice to have this cleared up!
dora c.