An Inheritance Example


In the following example of inheritance, class Bicycle is a base class, class MountainBike is a derived class which extends Bicycle class, and class Bike is a driver class to run program. When an object of MountainBike class is created, a copy of the all methods and fields of the superclass acquire memory in this object.

That is why, by using the object of the subclass we can also access the members of a superclass. During inheritance only object of subclass is created, not the superclass. In practice, inheritance and polymorphism are used together to achieve fast performance and readability of code.

Bike.java (an inheritance example)
// Java program to illustrate the concept of inheritance 
public class Bike {
  public static void main( String args[ ] ) {
    int gear   = Integer.parseInt( args[0] );
    int speed  = Integer.parseInt( args[1] );
    int height = Integer.parseInt( args[2] );
    int up     = Integer.parseInt( args[3] );
    int down   = Integer.parseInt( args[4] );
    int newH   = Integer.parseInt( args[5] );
    MountainBike mb = new MountainBike( gear, speed, height );
    mb.speedUp   ( up );
    mb.applyBrake( down );
    mb.setHeight ( newH );
    System.out.println( mb.toString( ) );
  }
}
// Base class 
class Bicycle { 
  // The Bicycle class has two fields. 
  public int gear; 
  public int speed; 
  
  // The Bicycle class has one constructor. 
  public Bicycle( int gear, int speed ) { 
    this.gear  = gear; 
    this.speed = speed; 
  }           
  // The Bicycle class has three methods. 
  public void applyBrake( int decrement ) { 
    speed -= decrement; 
  }          
  public void speedUp( int increment ) { 
    speed += increment; 
  }     
  // The toString() method to print info of Bicycle. 
  public String toString( ) { 
    return( "Number of gears is " + gear + "\n" +
            "Speed of bicycle is " + speed + "\n" ); 
  }  
} 
// Derived class 
class MountainBike extends Bicycle { 
  // The MountainBike subclass adds one more field. 
  public int seatHeight; 
  
  // The MountainBike subclass has one constructor. 
  public MountainBike( int gear, int speed, int startHeight ) { 
    // Invoking base-class (Bicycle) constructor 
    super( gear, speed ); 
    seatHeight = startHeight; 
  }       
  // The MountainBike subclass adds one more method. 
  public void setHeight( int newValue ) { 
    seatHeight = newValue; 
  }        
  // Override toString() method of Bicycle to print more info. 
  @Override
  public String toString( ) { 
    return( super.toString( ) + "Seat height is " + seatHeight ); 
  }
}
shell> java Bike   (gear)   (speed)   (height) 

(up)   (down)   (new height)

   




      My little brother wanted to know what happens after we die.    
      I explained that we get dumped in a hole under a pile of dirt, and then worms eat our bodies.    
      I probably should have told him the truth ―    
      that most people go to hell and burn in a lake of fire for all eternity ―    
      but I didn’t want to upset him.