Java Inheritance - Classes Hierarchy
Inheritance in Java establishes a hierarchical relationship among classes, where each class can have superclasses and subclasses. This hierarchy is structured like a tree, with the root class being "Object". Inheritance allows subclasses to inherit characteristics from superclasses while also enabling customization through adding unique components. Programmers can create their own class hierarchies by defining subclasses. Inheritance also supports overriding inherited characteristics when needed.
Uploaded on Apr 16, 2025 | 0 Views
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
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. 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.
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.
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.
Inheritance Object Cup Seed
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.
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.
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.
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.
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
Hierarchy Example Kingdom: Animalia Phylum: Chordata Class: Mammalia Order: Primates Family: Hominidae Genus: Homo 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. Species: sapiens
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.
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
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.
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.
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.
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.
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.