1 / 18

Java Unit 11: Inheritance I

Java Unit 11: Inheritance I. What is Inheritance?. Inheritance. All classes in Java are related to each other in a hierarchical way. Except for the class at the top of the hierarchy, which has nothing above it, a class has exactly one class immediately above it in the hierarchy.

thimba
Download Presentation

Java Unit 11: Inheritance I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Unit 11: Inheritance I What is Inheritance?

  2. Inheritance All classes in Java are related to each other in a hierarchical way. Except for the class at the top of the hierarchy, which has nothing above it, a class has exactly one class immediately above it in the hierarchy. Above that class may be another one, and so on. In general, any given class may have more than one class above it, at different levels in the hierarchy.

  3. Inheritance The class immediately above a given class may be called the parent class or superclass. All classes above it are known generally as superclasses. A class can have more than one class immediately below it. Below that may be others, and so on. In general any given class may have more than one class below it, at different levels in the hierarchy. A class immediately below a given class may be called a child class or a subclass. All classes below it are known generally as subclasses.

  4. Inheritance The type of hierarchy described above is a tree. There is a single class at the top of the hierarchy (the root of the tree). In Java, this class is named Object. All system supplied classes are arranged in a hierarchy beneath this class. All classes written and used by programmers in Java also fit into this hierarchy. Up to this point the code for several difference classes has been introduced, such as the Cup class or the Seed class. When a parent class is not specified, such classes become children, or immediate subclasses, of the Object class.

  5. Inheritance

  6. Inheritance There is a syntax allowing programmers to specifically make one class a subclass of another. This makes it possible for programmers to create their own hierarchies of classes or insert classes into an existing hierarchy at any level below the Object class.

  7. Inheritance • Classes in the hierarchy are related by inheritance. • Any class that has classes above it in the hierarchy inherits characteristics of those superclasses. • These characteristics include variables and methods. • Constructors are not inherited. • It is natural, and possible, for subclasses to add components which do not exist in the superclasses. This is the main way in which subclasses distinguish themselves from their superclasses.

  8. Inheritance There may also be situations where it is undesirable to inherit characteristics. In such cases it is possible to override or replace something in a subclass which would otherwise be inherited from a superclass. Inheritance is a complicated subject because of Java’s flexibility in handling all of the different possibilities.

  9. Inheritance An inheritance hierarchy may at first seem to be an unfamiliar concept. In fact, it is a common way of classifying things. The relationship between items in such a hierarchy can be described as an “is a” or “is a kind of” relationship. In other words, any class that appears lower down in the hierarchy must be an example of, or a more specific kind of, whatever classes appear above it in the hierarchy.

  10. Hierarchy Example The phylum Chordata is a subdivision of the kingdom Animalia. There are some animals with spinal cords and some without. However, if a creature is classified as a member of the phylum Chordata, it is certainly an animal and has all of the general characteristics of the kingdom Animalia. • Kingdom: Animalia • Phylum: Chordata • Class: Mammalia • Order: Primates • Family: Hominidae • Genus: Homo • Species: sapiens

  11. Hierarchy Example • Kingdom: Animalia • Phylum: Chordata • Class: Mammalia • Order: Primates • Family: Hominidae • Genus: Homo • Species: sapiens This kind of relationship holds throughout the hierarchy. If a creature is classified as a mammal, it has all of the characteristics of the phylum Chordata and the kingdom Animalia. Each step down the hierarchy leads to a more specific classification.

  12. Inheritance Different uses of the term inheritance can potentially lead to confusion. Inheritance can be used to refer to the characteristics an individual child receives from a particular parent, for example. The taxonomic example is based on the characteristics of categories, not individuals. It is possible to have instances of Homo sapiens, that is, individual people. Homo would be the parent class of sapiens in an inheritance hierarchy, but it would not represent the biological parent of an instance of Homo sapiens.

  13. Inheritance in Java • Examples of inheritance hierarchies exist throughout the Java API documentation. If you find the class Ellipse2D in the documentation, at the top you’ll see this information: • java.lang.Object • java.awt.geom.RectangularShape • Java.awt.geom.Ellipse2D

  14. Inheritance in Java As usual, the first letters of class names are capitalized. Ignoring the package names, java.lang and java.awt.geom, the information to be derived from this is the following: Object is the parent class of RectangularShape and RectangularShape is the parent class of Ellipse2D.

  15. Inheritance in Java It may seem a little odd that ellipses are descended from rectangular shapes. This is based on the fact that the location, size, and shape of an ellipse are specified by parameters that define a bounding rectangle. An elliptical shape can inherit instance variables for these parameters from a rectangular shape.

  16. Inheritance in Java • A class also inherits methods from classes above it. • In the documentation of the Ellipse2D class, after the specification of the variables, constructors, and methods that belong to it, you will find this section: • Methods inherited from class java.awt.geom.RectangularShape • Among other methods listed here, you will find these examples: getCenterX() and getCenterY(). • The center of an ellipse can be found by finding the center of its bounding rectangle. • Since such methods already exist for the RectangularShape class, it is not necessary to write them again for the Ellipse2D class. They can be inherited. • This is one of the advantages of an object-oriented language: code reusability.

  17. Inheritance in Java You will also find this section in the documentation of the Ellipse2D class: Methods inherited from class java.lang.Object. Among other methods listed here, you will find: equals(). If a class does not implement an equals() method itself, it inherits that method from the Object class. This inherited method tests for equality of reference, not equality of contents. If it is desired to test equality of contents, a method of the same name which does this has to be implemented in the subclass.

  18. Inheritance in Java In the naming convention of the section of documentation shown above we also encountered full package names, such as java.lang.Object and java.awt.geom.RectangularShape. The classes in Java are grouped together and stored in different packages. If you go to the hypertext version of the documentation, in the upper left hand corner you can choose to look at classes grouped by packages. In general, classes that are closely related in some way are packaged together. However, it is important to realize that these packages are merely a practical convenience and do not represent the subclass and superclass relationships among the classes.

More Related