what is inheritance? java unit 11: inheritance i

18
WHAT IS INHERITANCE? Java Unit 11: Inheritance I

Upload: horatio-shields

Post on 22-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: WHAT IS INHERITANCE? Java Unit 11: Inheritance I

WHAT IS INHERITANCE?

Java Unit 11: Inheritance I

Page 2: WHAT IS INHERITANCE? Java Unit 11: Inheritance I

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.

Page 3: WHAT IS INHERITANCE? Java Unit 11: Inheritance I

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.

Page 4: WHAT IS INHERITANCE? Java Unit 11: Inheritance I

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.

Page 5: WHAT IS INHERITANCE? Java Unit 11: Inheritance I

Inheritance

Object

Cup Seed

Page 6: WHAT IS INHERITANCE? Java Unit 11: Inheritance I

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.

Page 7: WHAT IS INHERITANCE? Java Unit 11: Inheritance I

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.

Page 8: WHAT IS INHERITANCE? Java Unit 11: Inheritance I

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.

Page 9: WHAT IS INHERITANCE? Java Unit 11: Inheritance I

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.

Page 10: WHAT IS INHERITANCE? Java Unit 11: Inheritance I

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: HominidaeGenus: Homo

• Species: sapiens

Page 11: WHAT IS INHERITANCE? Java Unit 11: Inheritance I

Hierarchy Example

Kingdom: Animalia Phylum: Chordata

Class: Mammalia Order: Primates

• Family: HominidaeGenus: 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.

Page 12: WHAT IS INHERITANCE? Java Unit 11: Inheritance I

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.

Page 13: WHAT IS INHERITANCE? Java Unit 11: Inheritance I

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

Page 14: WHAT IS INHERITANCE? Java Unit 11: Inheritance I

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.

Page 15: WHAT IS INHERITANCE? Java Unit 11: Inheritance I

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.

Page 16: WHAT IS INHERITANCE? Java Unit 11: Inheritance I

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.

Page 17: WHAT IS INHERITANCE? Java Unit 11: Inheritance I

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.

Page 18: WHAT IS INHERITANCE? Java Unit 11: Inheritance I

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.