Which of the following reasons for using an inheritance hierarchy are valid?

In this section we will discuss inherited methods, new methods, and finally overridden methods (which are the most interesting and powerful). First, a subclass inherits all methods that are available in its superclass. We can call such methods on any variable whose type is specified by the subclass. The IntCounter superclass declares the reset, getValue, and toString methods. The ModularCounter subclass inherits all these methods. So, if we declare ModularCounter mc = new ModularCounter(0,3); then we can call mc.reset(); and mc.getValue() and mc.toString() - which, remember, is called implicitly in System.out.println("mc = " + mc);

In all these cases Java executes the methods defined in the IntCounter superclass. Such a method, being in the superclass and not the subclass, can refer only to instance variables declared in the IntCounter superclass; it is not really aware that it is being called via a ModularCounter object.

Second, subclasses can also define new methods: ones that are not defined in the superclass (either with a different name, OR WITH THE SAME NAME AND A DIFFERENT SIGNATURE (parameter structure) - both such methods are considered new methods). The ModularCounter subclass defines the public getModulus method simply as

  public int getModulus()
  {return modulus;}
There is no method with this name defined in the IntCounter superclass; it wouldn't make sense to define such a method there because IntCounters don't store a modulus value! They represent only simpler objects. So if we declare ModularCounter mc = new ModularCounte(0,3); then we can call mc.getModulus() and Java executes the getModulus method defined in the ModularCounter subclass.

Finally, and most interestingly, a subclass can also override a method (note the word is override NOT overwrite- the words are hard to differentiate when you hear them). In this case, the subclass defines its own method (with the same name and signature as a method that it inherits). When we call that method (by its name, with the correct number/type of arguments), Java executes the METHOD DEFINED IN THE SUBCLASS, not the one it inherited from the superclass. Thus, the subclass "particularizes" that method: it can access all the instance variables declared in the subclass. Because the IntCounter superclass declares a public increment method (with no parameters), and because the ModularCounter class inherits and overrides this method by defining

  public void increment()
  {
    if (getValue() == modulus-1)
      reset();
    else
      super.increment();
  }
if we declare ModularCounter mc = new ModularCounter(0,3); then we can call mc.increment(); and Java executes the increment method defined in the ModularCounter subclass (the one shown above), which overrides the method in the superclass.

We will now examine HOW Java executes this method. The if statement calls the inherited getValue method and checks it for equality against one less than the modulus instance variable (defined private inside the ModularCounter class, and thus directly accessible in this method, which is also defined there). If the test is true, the inherited reset method is called; if false, the inherited increment method is called.

In the else part, if we wrote only the call increment(); in this method, Java would try to recursively call the same increment method that it is executing: the one defined in the ModularCounter subclass; but, instead we wrote super.increment(); which tells Java to call the overridden method that was inherited.

Our model for objects -the one showing a superclass inside a subclass- can help us to understand the process that Java uses to decide which method to call. Think of the following general process happening whenever a method is called on an object that comes from a subclass. Java starts at the outermost subclass. If Java finds the method defined there (right name, right signature), it calls the method defined in that subclass. If Java cannot find the method, it goes inward, to its direct superclass, and repeats this process. So, whenever Java cannot find a method in a subclass, it moves inward to its superclass repeating the process until it finds the right method to call.

For new methods in the subclass, this is trivial, because such methods are always found at the outer level. Java finds the definitions of inherited methods when it moves inward, to the superclass that first defines them. For overridden methods, Java finds their definition in the (outer) subclass. Although the actual process that Java uses to find a method is much more efficient (a fast table lookup), this model is a good one to understand, because it is simpler to explain and the result is the same.

Finally, when a subclass method overrides a superclass method, and that method name is called (it will be immediately found in the subclass), the subclass method can call the superclass method that it overrode by prefixing the method's name using the keyword super. Often the superclass method helps the subclass method get the job done: the increment method in ModularCounter sometimes needs just to increment the private instance variable value defined in the IntCounter class, and the only way it can do so is by calling the increment method defined in this superclass, hence the call super.increment(); Concretely, if we declare ModularCounter mc = new ModularCounter(0,2); in the first call to mc.increment(); Java finds and executes increment in the subclass, which explicitly executes the inherited method; in the second call (with value now set to 1) Java finds and executes increment in the subclass, which executes the inherited reset method

Note that IntCounter class defined toString, which the ModularCounter class overrides by defnining

  public String toString()
  {return super.toString()+"("+modulus+")";}
It works by first calling the toString method defined in the IntCounter class, getting a String representation of the value instance variable, and then catenating it with the modulus instance variable available in this subclass.

Thus, just as we can use this as an explicit reference to the current object, and we can use super as a reference to the current object as well (but pretending that it is constructed from its superclass, when accessing any member; forcing Java to find the method one level deeper in the object pictures).

What is the hierarchy of inheritance?

Hierarchical inheritance describes a situation in which a parent class is inherited by multiple subclasses. A type of inheritance in which more than one class is inherited from a single parent or base class is known as hierarchical inheritance.

What is the use of hierarchical inheritance in Java?

Hierarchical inheritance is one of the types of inheritance where multiple child classes inherit the methods and properties of the same parent class. Hierarchical inheritance not only reduces the code length but also increases the code modularity.

Which of the following statements about inheritance in Java is true?

Which of the following statements are true for inheritance in java? "extend" keyword is used to extend a class in java. You can extend multiple classes in java. Private members of superclass are accessible to subclass.

What is the first step that should be taken when developing an inheritance hierarchy?

What is the first step that should be taken when developing an inheritance hierarchy? List the classes that will be part of the hierarchy.