Discuss an example of an inheritance hierarchy of Java super
Discuss an example of an inheritance hierarchy of Java superclasses and subclasses,and describe the method overriding and overloading you would use to maximize code reuse
Solution
A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).
Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.
The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
 A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members:
The inherited fields can be used directly, just like any other fields.
 You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended).
 You can declare new fields in the subclass that are not in the superclass.
 The inherited methods can be used directly as they are.
 You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
 You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
 You can declare new methods in the subclass that are not in the superclass.
 You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.
A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
 Example:
 public class Bicycle {
   
 // the Bicycle class has three fields
 public int cadence;
 public int gear;
 public int speed;
   
 // the Bicycle class has one constructor
 public Bicycle(int startCadence, int startSpeed, int startGear) {
 gear = startGear;
 cadence = startCadence;
 speed = startSpeed;
 }
   
 // the Bicycle class has four methods
 public void setCadence(int newValue) {
 cadence = newValue;
 }
   
 public void setGear(int newValue) {
 gear = newValue;
 }
   
 public void applyBrake(int decrement) {
 speed -= decrement;
 }
   
 public void speedUp(int increment) {
 speed += increment;
 }
   
 }
 A class declaration for a MountainBike class that is a subclass of Bicycle might look like this:
public class MountainBike extends Bicycle {
   
 // the MountainBike subclass adds one field
 public int seatHeight;
// the MountainBike subclass has one constructor
 public MountainBike(int startHeight,
 int startCadence,
 int startSpeed,
 int startGear) {
 super(startCadence, startSpeed, startGear);
 seatHeight = startHeight;
 }   
   
 // the MountainBike subclass adds one method
 public void setHeight(int newValue) {
 seatHeight = newValue;
 }   
 }
 MountainBike inherits all the fields and methods of Bicycle and adds the field seatHeight and a method to set it. Except for the constructor, it is as if you had written a new MountainBike class entirely from scratch, with four fields and five methods. However, you didn\'t have to do all the work. This would be especially valuable if the methods in the Bicycle class were complex and had taken substantial time to debug
 #########################################################################################################################################
Overloading occurs when two or more methods in one class have the same method name but different parameters.
Overriding means having two methods with the same method name and parameters (i.e., method signature). One of the methods is in the parent class and the other is in the child class. Overriding allows a child class to provide a specific implementation of a method that is already provided its parent class.
 Here are some important facts about Overriding and Overloading:
1). The real object type in the run-time, not the reference variable\'s type, determines which overridden method is used at runtime. In contrast, reference type determines which overloaded method will be used at compile time.
 2). Polymorphism applies to overriding, not to overloading.
 3). Overriding is a run-time concept while overloading is a compile-time concept.
Example:
 class Dog{
 public void bark(){
 System.out.println(\"woof \");
 }
 }
 class Hound extends Dog{
 public void sniff(){
 System.out.println(\"sniff \");
 }
 
     // this is overriding
 public void bark(){
 System.out.println(\"bowl\");
 }
//overloading method
 public void bark(int num){
    for(int i=0; i<num; i++)
        System.out.println(\"woof \");
 }
 }
 
 public class OverridingTest{
 public static void main(String [] args){
 Dog dog = new Hound();
 dog.bark();
 }
 }



