// 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 );
}
}
|