Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

FUCK JAVA

Name: CHUMPY CS STUDENT 2010-12-07 20:53

public class Player extends Piece { ... }
/* ... */
public static Piece inputPiece() { ... }
/* ... */

Player playa = inputPiece();

ChumpyProgram.java:43: incompatible types
found   : Piece
required: Player


So what the fuck is the point of inheritance if you can't do that?

Name: Anonymous 2010-12-07 21:14

>>1
Barring any other mistakes in the code, you have to cast it.

Name: Anonymous 2010-12-07 21:18

your using the same name for the class and a function, dumb ass.

Name: Anonymous 2010-12-07 21:20

It compiles (by the way, why does it use -Wall -Werror?)
But then!
Exception in thread "main" java.lang.ClassCastException: Piece cannot be cast to Player

for line 43.

Name: IHBTBM 2010-12-07 21:24

>>2
Actually, screw me.  I type too quickly when I'm in a rush.
Why are you doing that? what makes you think that would even work?

That's not how inheritance works and you're just being dumber than the Java compiler out of spite.

Name: Anonymous 2010-12-07 21:29

>>3
Read closer.  The class Player extends the class Piece; somewhere else there is a static method named inputPiece() that returns an object of type Piece.

Name: Anonymous 2010-12-07 21:31

>>5
Well, a Player has all the properties and methods of a Piece. Why shouldn't a cast work? I know it's adding extra shit but it shouldn't pussy out at that. It's not "C with memory protection".
Upcasting works apparently.
I just wanted a generic input method so I didn't have to write identical code for both Piece and Treasure (that also inherits from Piece).

Name: Anonymous 2010-12-07 21:31

>>7
Player and Treasure

Name: Anonymous 2010-12-07 21:34

>>7
Oh screw it I'll just put it in Piece and go to bed.

Name: Anonymous 2010-12-07 21:44

You can only cast up (into parents), not down.
IHBT

Name: Anonymous 2010-12-07 22:04

>>9
What >>10 said.  Inheritance means "child inherits from parent."  The child can mask itself as the parent, e.g, Piece playa = new Player(); - very useful for accessing a data structure containing multiple types objects inherited from a same base class), but you can't take a Piece and promote it into a Player with casting.

You can do this, if you want:
//EXAMPLE
public static Piece inputPiece(boolean isPlayer)
{
   if(isPlayer) return new Player();
   return new Treasure();
}
/* ... */
Piece thing = inputPiece( ... );
/* Player thing = (Player)inputPiece( ... ); */
/* Treasure thing = (Treasure)inputPiece( ... ); */


Of course, if you do one and inputPiece tries to return a Treasure object, it will fail at runtime.  Over-all, unless you can guarantee that you already know what inputPiece will return on any given input, you should not do this.

Name: Anonymous 2010-12-07 22:05

javAIDS

Name: Anonymous 2010-12-07 22:59

when you think it won't get worse: Java

Name: Anonymous 2010-12-08 1:25

Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T.

Name: Barbrbra L. 2010-12-08 6:51

>>14
For example, it can be easily shown that for any object x of type java.lang.Object, the expression x.getClass() == java.lang.Object evaluates to true.

Name: Anonymous 2011-02-04 13:03

Name: Anonymous 2011-02-04 13:31

Name: Anonymous 2011-02-04 15:53


Don't change these.
Name: Email:
Entire Thread Thread List