chapter_12_beginning_javabook - These are sample pages from...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CHAPTER 12 INHERITANCE AND CLASS HIERARCHIES The concept of inheritance is at the heart of object-oriented programming and object-ori- ented thinking. Just as a person in real life can inherit the features of his or her mother’s face, a Java class can inherit the features of another class. In the computer world, the inheritance mechanism is more accurate and predictable than in real life. Inheritance makes it easier to declare new classes. That’s why it is so important in object-oriented pro- gramming. In this chapter, we shall first study some simple cases of inheritance. Later, we shall study some more complex cases and a special kind of methods called polymorphic methods. We shall see that polymorphic methods are a kind of exception to the general inheritance mechanism. These are sample pages from Kari Laitinen’s book "A Natural Introduction to Computer Programming with Java". For more information, please visit http://www.naturalprogramming.com/javabook.html
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
384 Chapter 12: Inheritance and class hierarchies 12.1 Base classes and derived classes In the previous chapter, we studied a class named Date . Objects of the class Date contain the necessary data fields to store information about dates, and the class provides many useful methods to do various things with the information inside Date objects. With those methods we can compare Date objects, print Date objects, increment Date objects, etc. One might think that class Date is so complete that we do not need other classes to be concerned with date information. Unfortunately things are not usually so simple. In dif- ferent applications, slightly different date classes might be the most suitable classes to handle date information. In a large software project, for example, some people would like to modify class Date by adding new methods to it, while others would like to make it behave so that it automatically copies the computer system’s date into the Date object being created. If different groups of people tried to modify class Date for their own pur- poses, there would most likely be different versions of the class. That might be dangerous in a software project because people might end up using the wrong class versions. Inheritance is a mechanism that helps to manage classes which produce closely related objects. By using inheritance, it is easy to make slightly different versions of an existing class without having to modify the existing class. Inheritance is one of the key concepts in object-oriented programming. With inheritance it is possible to derive a new class from an existing class. Inheritance extends an existing class and produces a new class. If a source program contains a class declaration that begins class UpperClass { ... it is possible to write later in the same program or in some other program file class LowerClass extends UpperClass { ...
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern