From e425924ed403cd5d35cb66fcf37eb920dc598452 Mon Sep 17 00:00:00 2001 From: sagsango <34793802+sagsango@users.noreply.github.com> Date: Wed, 4 Sep 2019 19:41:56 +0530 Subject: [PATCH] Hello --- Inharitance/Example .java | 81 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 Inharitance/Example .java diff --git a/Inharitance/Example .java b/Inharitance/Example .java new file mode 100644 index 0000000..598081f --- /dev/null +++ b/Inharitance/Example .java @@ -0,0 +1,81 @@ +//Java program to illustrate the +// concept of inheritance + +// 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; + } + + // toString() method to print info of Bicycle + public String toString() + { + return("No of gears are "+gear + +"\n" + + "speed of bicycle is "+speed); + } +} + +// 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; + } + + // overriding toString() method + // of Bicycle to print more info + @Override + public String toString() + { + return (super.toString()+ + "\nseat height is "+seatHeight); + } + +} + +// driver class +public class Test +{ + public static void main(String args[]) + { + + MountainBike mb = new MountainBike(3, 100, 25); + System.out.println(mb.toString()); + + } +}