Ok, vamos por partes...
Primeiro, o Tutorial estah certo. Construtores nao sao herdados.
Isso fica claro no exemplo abaixo.
class C1 {
C1 (int i){}
}
class C2 extends C1 {
C2 () {}
}
class Caller {
public static vois main(String[] arg) {
C1 c1 = new C1(10); // Funciona
C2 c2 = new C2(10); // Nao funciona, pq nao existe esse construtor em C2, apenas
em C1. Nao ha heranca
}
}
Segundo, o TIJ estah certo tbem. No livro do Bruce Eckel nao estah escrito que existe
heranca de construtor,
mas sim que existe chamada automatica ao construtor default (e APENAS ao default) em
qualquer construtor
da classe derivada.
Exemplo 1:
//: c06:Cartoon.java
// Constructor calls during inheritance.
class Art {
Art() {
System.out.println("Art constructor");
}
}
class Drawing extends Art {
Drawing() {
System.out.println("Drawing constructor");
}
}
public class Cartoon extends Drawing {
Cartoon(int i) {
System.out.println("Cartoon constructor com parametro " + i);
}
public static void main(String[] args) {
Cartoon x = new Cartoon(2); // mesmo chamando um construtor com parametro, o
construtor default da classe base eh chamado
}
} ///:~
Exemplo 2:
//: c06:Cartoon.java
// Constructor calls during inheritance.
class Art {
Art() {
System.out.println("Art constructor");
}
}
class Drawing extends Art {
Drawing() {
System.out.println("Drawing constructor");
}
Drawing(int i) {
System.out.println("Drawing constructor com parametro " + i);
}
}
public class Cartoon extends Drawing {
Cartoon(int i) {
super(i); // se quiser chamar outro construtor da classe base que nao o default,
deve ser uma chamada explicita
System.out.println("Cartoon constructor com parametro " + i);
}
public static void main(String[] args) {
Cartoon x = new Cartoon(2);
}
} ///:~
Silvio
On Thu, 01 November 2001, "Airton" wrote:
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <HTML><HEAD>
> <META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
> <META content="MSHTML 6.00.2600.0" name=GENERATOR>
> <STYLE></STYLE>
> </HEAD>
> <BODY bgColor=#ffffff>
> <DIV><FONT face=Arial size=2><FONT face="Times New Roman" size=3>
> <H3><FONT face=Arial size=2>Ol�.</FONT></H3>
> <DIV><FONT face=Arial size=2>Entendi pelo trecho abaixo do <EM>The Java
> Tutorial</EM>, que construtores n�o podem ser herdados:</FONT></DIV>
> <H3>What Members Does a Subclass Inherit?</H3>
> <BLOCKQUOTE>
> <BLOCKQUOTE>
> <HR>
> <STRONG>Rule:</STRONG> A subclass inherits all of the members in its
> superclass that are accessible to that subclass unless the subclass
> explicitly hides a member variable or overrides a method. <STRONG><EM>Note
> that constructors are not members and are not inherited by
> subclasses.</EM></STRONG>
> <HR>
> </BLOCKQUOTE></BLOCKQUOTE></FONT></FONT></DIV>
> <DIV><FONT face=Arial size=2><FONT face=Arial size=2></FONT></FONT> </DIV>
> <DIV><FONT face=Arial size=2></FONT> </DIV>
> <DIV><FONT face=Arial size=2></FONT> </DIV>
> <DIV><FONT face=Arial size=2><FONT face=Arial size=2>O trecho a seguir faz parte
> do cap�tulo 6 do livro <EM>Thinking in Jana, 2nd edition, Revision 12</EM> de
> Bruce Eckel, mostrando que os construtores s�o herdados das classes bases, at�
> mesmo se vc n�o criar um (ver �ltima linha do trecho):</FONT></FONT></DIV>
> <DIV><FONT face=Arial size=2></FONT> </DIV>
> <DIV><FONT face=Arial size=2><FONT face=Arial size=2></FONT></FONT> </DIV>
> <DIV><FONT face=Arial size=2><FONT face="Times New Roman" size=3>Java
> automatically inserts calls to the base-class constructor in the derived-class
> constructor. The following example shows this working with three levels of
> inheritance:<BR></DIV></FONT>
> <BLOCKQUOTE><FONT size=+1><PRE><FONT size=3><FONT color=#009900>//:
>c06:Cartoon.java</FONT>
> <FONT color=#009900>// Constructor calls during inheritance.</FONT>
>
> <FONT color=#0000ff>class</FONT> Art {
> Art() {
> System.out.println(<FONT color=#004488>"Art constructor"</FONT>);
> }
> }
>
> <FONT color=#0000ff>class</FONT> Drawing <FONT color=#0000ff>extends</FONT> Art {
> Drawing() {
> System.out.println(<FONT color=#004488>"Drawing constructor"</FONT>);
> }
> }
>
> <FONT color=#0000ff>public</FONT> <FONT color=#0000ff>class</FONT> Cartoon <FONT
>color=#0000ff>extends</FONT> Drawing {
> Cartoon() {
> System.out.println(<FONT color=#004488>"Cartoon constructor"</FONT>);
> }
> <FONT color=#0000ff>public</FONT> <FONT color=#0000ff>static</FONT> <FONT
>color=#0000ff>void</FONT> main(String[] args) {
> Cartoon x = <FONT color=#0000ff>new</FONT> Cartoon();
> }
> } <FONT color=#009900>///:~</FONT></FONT></PRE></FONT></BLOCKQUOTE>
> <DIV align=left>
> <P><FONT face=Georgia>The output for this program shows the automatic
> calls:</FONT><BR></P></DIV>
> <BLOCKQUOTE><FONT size=+1><PRE>Art constructor
> Drawing constructor
> Cartoon constructor</PRE></FONT></BLOCKQUOTE>
> <DIV align=left>
> <P><FONT face=Georgia>You can see that the construction happens from the base
> �outward,� so the base class is initialized before the derived-class
> constructors can access it. [ <A
> href="http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER6_I24"
> target=_blank>Add Comment</A> ] <?XML:NAMESPACE PREFIX = BACKTALK
> /><BACKTALK:DISPLAY id=TIJ3_CHAPTER6_I25></FONT><BR></P></DIV>
> <DIV align=left>
> <P><FONT face=Georgia>Even if you don�t create a constructor for
> <B>Cartoon( )</B>, the compiler will <A name=Index574></A>synthesize a
> default constructor for you that calls the base class constructor.
> </BACKTALK:DISPLAY>[ <A
> href="http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER6_I25"
> target=_blank>Add Comment</A> ]</FONT></P>
> <P> </P>
> <P><STRONG>O que entendi errado? � interessante colocar os membros da super
> classe a ser herdada nos construtores ou � melhor evitar? Por
> qu�?</STRONG></P>
> <P><STRONG>Obrigado,</STRONG></P>
> <P><STRONG>Airton</STRONG></P></DIV></FONT></BODY></HTML>
------------------------------ 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
para sair da lista: envie email para [EMAIL PROTECTED]
-------------------------------------------------------------------------