Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java classes inheritance

Here, it says that:

This gives MountainBike all the same fields and methods as Bicycle, yet allows its code to focus exclusively on the features that make it unique. This makes code for your subclasses easy to read. However, you must take care to properly document the state and behavior that each superclass defines, since that code will not appear in the source file of each subclass.

However in my code the inheritance doesn't work properly, probably due to a flaw in my code?

package java1;

class bicycle {
    int speed = 0;
    int gear = 1;

    void accelerate(int incr) {
        speed = speed + incr;
    }

    void decelerate(int incr) {
        speed = speed - incr;
    }

    void changeGear(int val) {
        gear = val;
    }

    void printInfo() {
        System.out.println("Speed: " + speed + " Gear: " + gear);
    }
}

class mountainbike extends bicycle {
    int speed = 10;
}

public class Main {

    public static void main(String[] args) {

        mountainbike mb1 = new mountainbike();

        mb1.accelerate(20);
        mb1.accelerate(10);

        mb1.printInfo();

    }

}

The mountainbike class should inherit all the characteristics of the bicycle class, right? I added a unique property which is int speed = 10, so starting speed should be 10. However on running the compiler treats starting speed as 0.

Any ideas?


2 Answers

No, your Mountainbike class should not redefine a speed variable. You need to pass the initial speed (10) to the constructor of its super class using super as part of the constructor call:

package java1;

class Bicycle
{

  Bicycle(int speed, int gear)
  {
    this.speed = speed;
    this.gear = gear;
  }

  public void accelerate(int incr)
  {
    speed = speed + incr;
  }

  public void decelerate(int incr)
  {
    speed = speed - incr;
  }

  void changeGear(int val)
  {
    gear = val;
  }

  public String toString()
  {
    return "Speed: " + speed + " Gear: " + gear;
  }

  private int speed = 0;
  private int gear = 1;

}

class Mountainbike extends Bicycle
{
  public Mountainbike()
  {
    super(10, 1);
  }
}

public class Main {

  public static void main(String[] args)
  {    
    Mountainbike mb1 = new Mountainbike();

    mb1.accelerate(20);
    mb1.accelerate(10);

    System.out.println(mb1);
  }
}

I would recommend against using protected variables for speed and gear. It is good practice to enforce encapsulation by declaring the speed and gear variables private in the base class.


EDIT:

Apparently, you're beginning with Java programming. I suggest you have a look at this free online book written by Bruce Eckel: Thinking in Java.

like image 145
Gregory Pakosz Avatar answered Mar 19 '26 00:03

Gregory Pakosz


public class Bicycle {
   private int speed;

   public Bicycle (int speed) {
      this.speed = speed;
   }
}


public class Mountainbike extends Bicycle {
   public Mountainbike() {
      super(10);
   }
}
like image 39
oliver31 Avatar answered Mar 19 '26 00:03

oliver31