Ol�.

Entendi pelo trecho abaixo do The Java Tutorial, que construtores n�o podem ser herdados:

What Members Does a Subclass Inherit?


Rule:  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. Note that constructors are not members and are not inherited by subclasses.
 
 
 
O trecho a seguir faz parte do cap�tulo 6 do livro Thinking in Jana, 2nd edition, Revision 12 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):
 
 
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:
//: 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() {
    System.out.println("Cartoon constructor");
  }
  public static void main(String[] args) {
    Cartoon x = new Cartoon();
  }
} ///:~

The output for this program shows the automatic calls:

Art constructor
Drawing constructor
Cartoon constructor

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. [ Add Comment ]

Even if you don�t create a constructor for Cartoon( ), the compiler will synthesize a default constructor for you that calls the base class constructor. [ Add Comment ]

 

O que entendi errado? � interessante colocar os membros da super classe a ser herdada nos construtores ou � melhor evitar? Por qu�?

Obrigado,

Airton

Responder a