Can some give me code examples with explanations for the following last 2 rules.
4. If the parent doesn’t have a no-argument constructor and the child doesn’t define any
constructors, the compiler will throw an error and try to insert a default no-argument
constructor into the child class.
5. If the parent doesn’t have a no-argument constructor, the compiler requires an explicit
call to a parent constructor in each child constructor.
Those two rules basically mean the same thing. When a class doesn't explicitly define any constructors the Java compiler inserts a default constructor that looks like this:
That means that if you have a superclass that doesn't have a parameterless constructor (because it defined a constructor with parameters), the default constructor of the subclass will cause a compilation error, because super() refers to a constructor that doesn't exist. Therefore, subclasses of classes that don't have a parameterless constructor must explicitly define a constructor that calls the appropriate superclass constructor:
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop