Inheritance

Optional Node
Premium Content - Free Preview

One of the principles of OOP is creating Classes that inherit from other Classes. This lets you re-use code, avoid duplication, and helps keep your code more maintainable and extendable. However, it is not essential for beginners, so you can skip this node and return to it at a later point.

Student-Class

Extends keyword
In Java, you use the extends keyword to make a Class a subclass of a superclass. In this case, Student extends Person, which means that Student will "inherit" all of Person's methods and variables. This is why each instance of Student has access to the getName() method, which was defined in Person. Student also defines its own variables and methods, such as SID and getStudentID().

super()
In Java, super is the keyword used in a subclass to refer to the superclass. When used in the constructor, it will call the superclass's constructor. This must be the first method called in the constructor. Java will actually call super() on its own, but it is a good idea to explicitly include it. Also, you will need to call super explicitly if you want to pass parameters to the superclass's constructor (since Java cannot figure out the parameters on its own).

For example, look at the Student's constructors. In each constructor, Student starts off by calling super(), which will invoke Person's constructor:

 Person(String name, int yearOfBirth)
 {
     this.name = name;
     this.yearOfBirth = yearOfBirth;
 }

Student is able to pass in the parameters for name and yearOfBirth to super() for Person to set up. In addition to avoiding code duplication, this is a way of setting up variables that the subclass may not have access to.

Overriding methods
Sometimes the subclass may need to implement a method differently than the way it was implemented by the superclass. In this situation, the subclass can override the method by implementing a method with the same name and header. As a simple example, let's say you have a Class Car with a method accelerate():


End of Free Content Preview. Please Sign in or Sign up to buy premium content.

Comments

  • Is it ok that rocketCar is defined before Car ?

  • @calo, doesn't matter. On a computer, each class is usually defined in its own file; the Java compiler can figure it all out.

  • I see now ...I made it eventually, although to public RocketCar(), I only type formula for rocket fuel and nothing else:
    public RocketCar()
    {
    //constructor

    cont...
  • I can't figure out why fuel stays at 20

  • It is updated

  • Hello.
    I have correct output, but I changed private variables in class car to non-private.
    Is it OK ?
    my code: http://www.learneroo.com/user_answers/5096985764

  • Why, in the Student & Staff example, student1.super.toString() doesn't work? I get Error: cannot find symbol - class student1. I've created an instance of Student called student1.

  • I have:
    if(speed <= maxSpeed && rocketFuel != 0)
    {
    speed+=2;
    rocketFuel--;
    }

    But its saying it doesn't like my maxSpeed...
    Main.java:14: error: maxSpeed has private access in Car
    if(speed <= maxSpeed && rocketFuel != 0)
    ^

    Why?

  • Hi!
    I didn't succeed resolve it..

  • @Raz, you first need to address the compile errors. Don't call super() from within a regular method, instead call super.methodName(). And don't try accessing a Car's private fuel, RocketCar only deals with rocketFuel.

  • I'm didn't understand What am I being asked to do?

  • @Raz, I added a hint that spells it out in order.

  • my code - Didn't work:/

  • You still need to fix your accelerate method, it's not following the correct syntax. See the correct solution for how to fix it.

  • so with this line of code:
    System.out.println(i +". "+ rc);

    when we say + rc it calls the toString() method of class Car?
    can you please explain it why? should't it be something like
    +rc.toString()) ;

  • As mentioned above, Java has a built-in shortcut that calls the toString method of an object when you print it. So System.out.println(i +". "+ rc); is equivalent to System.out.println(i +". "+ rc.toString() );

  • why does super.Car() not work?

  • What method is .Car()?

All Node Comments
Contact Us
Sign in or email us at [email protected]