Inheritance in Java

Inheritance in Java

Inheritance in Java property

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:

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;

In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes.

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.

Related concepts

Inheritance in Java — Structure map

Clickable & Draggable!

Inheritance in Java — Related pages: