Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overiding constructors section of textbook not making sense

Tags:

java

The text in the book i am reading states in summary

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

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

"when your class's constuctor methods are called, the constructor methods with the same signature for all your superclasses are also called . Therefore, initialization can happen for all parts of a class you inherit"

What I don't understand is the same signature section.... It comes accross to me as though all the constructors must have the same signature, and then when you initialize a child class object all it's super classes will automatically be called instead of having to call super(arg1,arg2) for each sub class.... Is this what they are stating?

like image 219
Chris Okyen Avatar asked Jun 16 '12 15:06

Chris Okyen


3 Answers

All super classes need not to have same constructor signature.
When you create new object Java will invoke no parameter constructor of super class if no constructor is defined. No parameter constructor is provided by Java if you don't define any other constructor.
If you define any other constructor you must have to call it (on first line ) inside child class constructor.
This might help.

like image 31
Ajinkya Avatar answered Nov 14 '22 23:11

Ajinkya


That's not quite right. Your superclass's constructor is only called automatically if it's the default constructor (i.e. the no-arg constructor).

If your superclass has no default constructor then you must explicitly invoke a constructor of your choosing with super(args..).

For example, this is perfectly fine despite the fact that BearManPig's constructor has a different signature than Animal:

public class Animal {
  public Animal() {
  }
}

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

That's because there's a Java found the default no-arg constructor.

Now, when there is no default constructor, you must be explicit about which one you want called. For example, this doesn't work:

// 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) {
  }
}

But this does:

// 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
  }
}
like image 62
Tim Pote Avatar answered Nov 14 '22 23:11

Tim Pote


when your class's constuctor methods are called, the constructor methods with the same signature for all your superclasses are also called . Therefore, initialization can happen for all parts of a class you inherit

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

While it is true that a super class constructor is invoked, it needn't have the same signature. If a subclass constructor begins with a super class constructor invocation expression such as

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

the superclass constructor matching the argument count and types in invoked. You are completely free which arguments you pass to that constructor.

If the subclass constructor does not begin with a super constructor invocation expression, the compiler will insert a call to the accessible zero-argument constructor of the superclass for you - or raise a compilation error if there is no such constructor.

Sams Teach Yourself Java 2 in 21 Days for now

You are aware that Java 2 is a decade out of date? Any book this old will teach you obsolete stuff, that you would be best served to forget quickly. Why not use a more modern book?

like image 37
meriton Avatar answered Nov 15 '22 01:11

meriton