- Objects
- Classes
- Inheritance
-
Programming in Java on Your Computer - Classes and Objects in BlueJ
- Trying out Code in BlueJ
- The Code for Creating and Using Objects
- Source Code and Methods
- Accessors and Mutators
- Constructor Code
- Scope
- Inheritance
-
BlueJ Review - Class Code
- Simple Debugging
- Interactive Picture
- Refactoring Code with Inheritance
Inheritance
Optional NodePremium 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.
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
calo
Jan 27, 2:00 PMIs it ok that rocketCar is defined before Car ?
Learneroo
Jan 27, 2:13 PM@calo, doesn't matter. On a computer, each class is usually defined in its own file; the Java compiler can figure it all out.
calo
Jan 27, 2:49 PMI see now ...I made it eventually, although to public RocketCar(), I only type formula for rocket fuel and nothing else:
public RocketCar()
{
//constructor
And it worked (in accelerate I called the super accelerate and defined the rocketFuel similar as normal fuel and it worked). I wonder if u stated "contractor: It should call Car's constructor, but also set rocketFuel to 10.", if something more should be there or not.
jack
Mar 18, 11:28 AMI can't figure out why fuel stays at 20
jack
Mar 18, 4:59 PMIt is updated
Lukas Dancak
Mar 19, 8:07 AMHello.
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
Pierre Niau
Jun 16, 8:55 PMWhy, 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.
catypus
Oct 10, 6:16 PMI 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?
Raz Levi
Dec 1, 11:24 PMHi!
I didn't succeed resolve it..
Learneroo
Dec 2, 12:05 AM@Raz, you first need to address the compile errors. Don't call
super()
from within a regular method, instead callsuper.methodName()
. And don't try accessing a Car's private fuel, RocketCar only deals with rocketFuel.Raz Levi
Dec 2, 12:44 AMI'm didn't understand What am I being asked to do?
Learneroo
Dec 2, 10:37 AM@Raz, I added a hint that spells it out in order.
Raz Levi
Dec 3, 2:42 AMmy code - Didn't work:/
Learneroo
Dec 3, 9:53 AMYou still need to fix your accelerate method, it's not following the correct syntax. See the correct solution for how to fix it.
Asım
Jan 22, 4:32 PMso 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()) ;
Learneroo
Jan 22, 5:11 PMAs mentioned above, Java has a built-in shortcut that calls the
toString
method of an object when you print it. SoSystem.out.println(i +". "+ rc);
is equivalent toSystem.out.println(i +". "+ rc.toString() );
Harkamal Singh
Aug 6, 6:27 PMwhy does super.Car() not work?
Learneroo
Aug 7, 8:36 AMWhat method is .Car()?