19 Constructors are an exception to the inheritance process in Java They are

19 constructors are an exception to the inheritance

This preview shows page 19 - 25 out of 35 pages.

19 Constructors are an exception to the inheritance process in Java. They are not inherited by the derived class even though they have public visibility. How do you invoke a superclass constructor? The Super Reference We often want to use the parent's constructor to set up the "parent's part" of the object The call to super() must be the first statement in each constructor. public class Animal private String name; public Animal(String theName) { name = theName; } public String getName() { return name; } public class MakeHippo { public static void main (String[] args) { Hippo h = new Hippo ("Buffy") ; System.out.println (h.getName ()) ; } public class Hippo extends Animal { public Hippo (String name) { super (name); } }
Image of page 19