ruby - How to avoid implicit call to parent class's constructor in Java? -


I have this code which is "a tree" twice and "oak tree Prints " Once I think that it's saying the tree class constructor inherently extends the class oak tree . Anyway, can I call the parent's constructor?

  public class app {public static zero main (string [] args) {tree p = new tree (); Oak m = new oak (); }} Public square tree {the name of the protected string; Public tree () {name = "A tree"; Println (name); }} Public square oak tree {public string name = "an oak tree" is extensively; Public Oak () {System.out.println (name); }}   

I compare Java with ruby ​​due to my small risk to do this. In Ruby, the parent's constructor is called only when the child is not one, therefore, I am trying to achieve my basic rights.

  class CDEF starts "C class" and end class D & LT; "D Class" End End # Class E does not have its own Constructor Class of CDR initialization E & lt; C end CNU # = & gt; C Class D.N. # = & gt; Dee Class E.new # = & gt; C class    

The only way to avoid an inherent call to a parent's creator A clear call to a parent creator You can not refrain from calling the Constructor for each class.

BTW does not require you to store name twice a simple approach is to do the following.

  public square tree {name of protected string; Public tree (this ("a tree");} protected tree (string name) {this.name = name; Println (name);}} public square oak tree (public oak) (super "(an oak tree" ) Is spread;}} In Java, it is believed that there is no sensible reason to call the constructor for a given class. If you think that this is a The problem is, you are doing things in your constructor that is more than just making and Ncniy side effects.  

object creation object should be avoided should be about to start. Side Effects.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -