language. This makes it much easier to create complex
applications and significantly less code is required to do so thanks to OOP.
Although there are many concepts that are important to understand in Java, one of the most important is
known as inheritance.
If you are not familiar with the concept of inheritance, be sure
to check out Java Fundamentals I & II. Not only does this course teach you these basic OOP concepts,
but it also teaches you how to create your own Java programs by example.
In this tutorial, you’ll learn what inheritance is and how it
can benefit you as a Java
programmer. You will also see examples of inheritance in action and learn
exactly how this can be applied to unique programs that you create on your own
in the future.
What is Inheritance?
In the Java
programming language, classes can be derived from other classes. In other
words, they inherit the fields and methods from those classes. A class that is
derived from another class is known as a subclass. In some circles, it is also
known as a child class or an extended class.
Except for the Object class, every class has one direct
superclass. This is known as single inheritance. In the absence of any other
superclass, every class is automatically a subclass of Object. Even though a
class has only one superclass, it can be derived from a class that is derived
from a class that is derived from a class that is – well, you get the picture.
The possibilities are endless when it comes to object-oriented programming and
that is what makes it such a powerful programming technique. Especially with
super complex applications that may contain millions of lines of code,
inheritance plays a massive role in the manageability of these applications.
Believe it or not, inheritance is a relatively simple concept.
When you want to create a new class for your job application and there is
already a class that includes some of the code your new class needs, you can
simply derive your new class from the existing class. This allows you to reuse
the fields and methods of the parent class without having to write any code
yourself.
This can save you countless hours when creating your own
applications because you do not have to reinvent the wheel. Many classes have
already been provided within the Java
API and you simply need to extend those classes to access the tried-and-true
code within them.
Inheritance Example
In case you’re lost after that explanation of inheritance, below
you will find an example of inheritance in action using something that most
everyone can relate to – a bicycle.
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;
}
}
If you have any experience with Java programming, this looks like a pretty standard class. The
bicycle class has three fields (cadence, gear, and speed), one constructor, and
four methods. Of course, bicycle is a pretty broad term. You probably realize
that there are many different types of bicycles from mountain bikes to road
bikes to BMX bikes. Interestingly enough, all of these types of bikes share the
same fields and methods as the bicycle superclass.
If your program needs a specific type of bicycle, such as a
mountain bike, you do not have to create a separate class defining cadence,
gear, speed and the four methods shown above. Instead, all you have to do is
extend the Bicycle class like this:
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;
}
}
Hopefully this example demonstrates the advantages of
inheritance. The MountainBike subclass inherits all of the fields and methods
of Bicycle automatically. The only difference is that the new subclass adds a
new field (seatHeight), but other than that, it is exactly the same as Bicycle
but without you having to rewrite all of that code.
Obviously, this is a simplified example. If your Bicycle class
was 3,000 lines long, the value of inheritance becomes even more apparent. That
is 3,000 less lines of code you have to write and you can reuse that code as
much as you need to within your program. You could just as easily create a
subclass called RoadBike and it too would inherit the fields and methods of the
Bicycle class.
The best part about as inheritance is how easy it is to implement.
If you look at the example above, the only thing that really had to be done was
to extend Bicycle in the class declaration. It doesn’t get much simpler than
that, does it? Java – Make It Your Cup of Coffee is an excellent course that teaches practical Java programming for everyone and
includes a detailed section about inheritance.
Hopefully you see the value of inheritance in Java and other object oriented
programming languages. It is an extremely useful tool that saves time and
builds reliability into your code automatically. The more lines of code you
have to write, the greater the chance you will make a mistake that could leave
your program in compiling error limbo. Remember – keep it simple and take
advantage of inheritance whenever possible.
No comments:
Post a Comment