<< Chapter < Page Chapter >> Page >

I agree with that assessment.

Encapsulation

The first module in this series provided an explanation of encapsulation.

Inheritance

This module (and some modules to follow) will provide an explanation of inheritance. I will use another simple program to explain the concept of inheritance.

Polymorphism

Polymorphism is the most complex of the three, and will be explained in future modules.

A new data type

Whenever you define a class in Java, you cause a new data type to become available to the program. Therefore, whenever you need a new data type, you can define a new class to make that type available.

Extending a class

Defining a new class (to create a new type) can involve a lot of effort. Sometimes you have an option that can greatly reduce the effort required to create your new type. If a class (type) already exists that is close to what you need, you can often extend that class to produce a new class that is closer to what you need.

In many cases, this will require much less effort than that required to start from scratch and define a new class to establish a new type. The ability to extend one class into another new class is the essence of inheritance .

According to the current jargon, the new class is called the subclass and the class that is extended is called the superclass .

Although not explicitly demonstrated in this module, whenever you declare a reference variable whose type is defined by a class, that variable can be usedto store references to objects instantiated from that class or instantiated from any subclass of that class. Furthermore, that reference can be used to callmethods defined in or inherited into that class with no requirement for a cast. If the method is defined in that class and overridden in the subclass, and ifthe object is actually of the subclass type, the overridden version will be executed. This is polymorphism.

What is inherited?

The subclass inherits all of the variables and all of the methods defined in (or inherited into) the superclass, almost as if you had completely defined the new class from scratch, and had reproduced all of the code already defined in the existing superclasses.

Code reuse

Therefore, inheritance often makes it possible to define a new class with a minimum requirement to write new code by formally reusing the code that was previously written into the superclasses. Sometimes you can get by with simply extending the existing class.

Sometimes, however, it is also necessary to make changes to the existing class to improve its ability to be extended in a meaningful way. (That is the case with the sample program discussed in this module, but the next module will show you how to avoid that issue.) It all depends on how the existing class was designed in the first place. Ideally the original design of the class will be such that modification of theoriginal class is not necessary.

The Radio class

A previous program defined a class named Radio . Objects instantiated from the Radio class (see the previous modules for a discussion of instantiating objects) were intended to simulate car radios. (Note that the car radios simulated by objects of the Radio class didn't have built-in tape players.)

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask