|
O problema � que Sub � filha de
Base, o erro ocorre porque o compilador n�o consegue fazer a convers�o para um
n�vel acima na hieraquia. Serial algo assim. A classe-filha pode ser usada onde
a classe-pai � usada. A classe-filha apenas tem especializa��es a mais que a
distinguem da classe-pai, mas ainda tem as caracter�sticas da classe pai. J� a
utiliza��o da classe-pai onde � esperada a classe-filha d� problemas pois a
implementa��o � diferente.
Espero ter ajudado,
----- Original Message -----
Sent: Sunday, March 10, 2002 2:23
PM
Subject: Re: [java-list] Casting
Nesta linha
"public static void main(String argv[])"
deve ser String args[] e nao String
argv
Ok!?
Hudson
Costa
----- Original Message -----
Sent: Tuesday, March 05, 2002 4:34
PM
Subject: [java-list] Casting
Algu�m poderia me ajudar nesta quest�o? Estou
enviando como anexo uma classe que faz a mesma coisa e n�o ocorre erro
nenhum na linha 18.
Java Programmer Certification Mock Exam
No 1
Question 17)
What will happen if you attempt to compile and run the following code?
class Base {}
class Sub extends Base {}
class Sub2 extends Base {}
public class CEx{
public static void main(String argv[]){
Base b=new Base();
Sub s=(Sub) b;
}
}
1) Compile and run without error 2) Compile time Exception 3)
Runtime Exception
Answer 17)
3) Runtime Exception
Without the cast to sub you would get a
compile time error. The cast tells the compiler that you really mean to do
this and the actual type of b does not get resolved until runtime. Casting
down the object hierarchy as the compiler cannot be sure what has been
implemented in descendent classes. Casting up is not a problem because sub
classes will have the features of the base classes. This can feel counter
intuitive if you are aware that with primitives casting is allowed for
widening operations (ie byte to int).
------------------------------ LISTA SOUJAVA
---------------------------- http://www.soujava.org.br -
Sociedade de Usu�rios Java da Sucesu-SP d�vidas mais comuns:
http://www.soujava.org.br/faq.htm regras da lista:
http://www.soujava.org.br/regras.htm historico:
http://www.mail-archive.com/java-list%40soujava.org.br para sair da
lista: envie email para [EMAIL PROTECTED]
-------------------------------------------------------------------------
|