Inheritance in Java

Only available on StudyMode
  • Download(s) : 344
  • Published : May 4, 2006
Open Document
Text Preview
In Java programming, inheritance plays an important role in a programmer's ability to reuse codes. Inheritance in basic terms allows a subclass to use the variables and methods of a superclass. When a programmer creates a subclass with new variables and methods, it can easily use the superclass's methods and variables by extending without having to rewrite the entire superclass over again. Therefore, the subclass inherits variables and methods from the superclass. In this sense, inheritance helps save time in writing codes because imagine how time-consuming it would be to work with a superclass which had thousands of methods and variables. Needless to say, the time spent by the programming would be very costly if inheritance was not available.

Not only does inheritance saves programming time, it helps maintain efficiency and accuracy. If a programmer did not have the ability to use inheritance, they would have to rewrite the classes to add variables or methods and would increase the chances of creating errors. With thousands of variables and methods in a program, it can be easy to lose track or be inconsistent when editing changes.

An example used by Richard Baldwin helps illustrate how inheritance works. A superclass RADIO has attributes and methods of components of a car radio. When a new class is created to add tape player, the subclass COMBO, will inherit certain attributes from the superclass RADIO when using the new methods to create a function. For instance, Baldwin added a new method called playTape in the program and extends to the superclass RADIO to pull specific methods to determine whether or not the tape is in or out before playing the radio. Knowing whether or not the radio is turned on is a method in the superclass RADIO and does not need to be written again in the subclass COMBO. The use of inheritance in this case is important because the subclass COMBO can basically retrieve methods in the superclass RADIO by easily...
tracking img