Implementation of Inheritence
package practice.test;
public class Bicycle {
public int gear;
public int speed;
// Constructor
public Bicycle(int gear,int speed) {
this.gear=gear;
this.speed=speed;
}
// the Bycycle class has three methods
public void applyBreak(int decrement) {
speed-=decrement;
}
public void speedUp(int increment) {
speed+=increment;
}
public String toString() {
return ("No of gears are "+gear+"\n"+"speed of bicycle is"+speed);
}
}
// derived class
class MountainBike extends Bicycle
{
public int seatHeight;
public MountainBike(int gear,int speed,int seatHeight) {
super(gear,speed);
this.seatHeight=seatHeight;
}
// method
public void setSeatHeight(int newValue) {
seatHeight=newValue;
}
@Override
public String toString() {
return (super.toString() + "\n height is \t"+seatHeight);
}
}
package practice.test;
public class Tests {
public static void main(String[] args) {
// TODO Auto-generated method stub
MountainBike mBike=new MountainBike(3,100, 25);
System.out.println(mBike.toString());
}
}
-- Output
No of gears are 3
speed of bicycle is100
height is 25
No comments:
Post a Comment