Separating constructors in a textbook does not make sense

The text in the book I am reading is brief

"Technically, constructors cannot be overridden because they have the same name as the current class. New constructors are created instead of inherited ones. This system works fine ..."

The part I don't understand is when they say this:

"when you call the methods of the constuctor class, constructor methods with the same signature are also called for all your superclasses. Therefore, initialization can be performed for all parts of the class that you inherit"

What I don’t understand is the same signature section .... It comes up to me as if all the constructors should have the same signature, and then when you initialize the object of the child class, all its superclasses will automatically be (arg1, arg2) for each subclass .... Is this what they state?

+5
source share
3 answers

when calling constuctor class methods, constructor methods with the same signature are also called for all your superclasses. Therefore, initialization can occur for all parts of the class inherited

This is not true. First, a note on terminology: constructors are not methods, so the term "constructor methods" does not make any sense.

, , . ,

class Student extends Person {
    public Student(String name) {
        super(name, Occupation.STUDENT);
    }
}

, . , .

-, - , .

Sams Teach Yourself Java 2 21

, Java 2 ? , . ?

+2

. , (.. no-arg).

, super(args..).

, , , BearManPig , Animal:

public class Animal {
  public Animal() {
  }
}

public class BearManPig extends Animal {
  public BearManPig(String string) {
  }
}

, Java no-arg .

, , , . , :

// DOESN'T COMPILE
public class Animal {
  public Animal(String string) {
  }

  public Animal(String string0, String string1) {
  }
}

public class BearManPig extends Animal {
   // There is no default constructor, stupid Java can't figure out what to do
  public BearManPig(String string) {
  }
}

:

// does compile
public class Animal {
  public Animal(String string) {
  }

  public Animal(String string0, String string1) {
  }
}

public class BearManPig extends Animal {
  public BearManPig(String string) {
    super(string); // I've told Java what to do
  }
}
+10

​​ .
, Java , . Java- , - . - , ( ) .
.

+4

All Articles