Inheritance CS 2302 SPRING 2015. Inheritance In this part we introduce a new relationship between...
-
Upload
maryann-fox -
Category
Documents
-
view
216 -
download
0
Transcript of Inheritance CS 2302 SPRING 2015. Inheritance In this part we introduce a new relationship between...
InheritanceCS 2302 SPRING 2015
CS 2302-SPRING 2015 2
Inheritance In this part we introduce a new relationship between classes: inheritance. This is the fundamental feature of object-oriented programming.
The basic concepts of inheritance are discussed, and the first example from the book is recreated.
1/12/2015
CS 2302-SPRING 2015 3
Motivation When modeling objects, we group similar objects into *classes*
We may notice that some classes of objects have common features
Or, we may decide that it would be useful to subdivide and existing class into smaller groups with more specific characteristics
In either case we have the idea of *subclasses*: classes that are contained in larger classes
1/12/2015
CS 2302-SPRING 2015 4
Taxonomy "the branch of science concerned with classification, especially of organisms; systematics." (Google)
Most of us have studied taxonomy in biology: phyla, genera, species and other groups of living things
The various levels of biological taxonomy show the subclass relationship
1/12/2015
CS 2302-SPRING 2015 5
Classification of Humans Here are the various levels of biological classes/taxa to which we human beings are assigned. Animalia (Kingdom)
◦ Chordata (Phylum)◦ Mammalia (Class)◦ Primates (Order)◦ Hominidae (Family)◦ Hominini (Tribe)◦ Homo (Genus)◦ Sapiens (Species)
The parenthesized names are the names for that level of classification in biological taxonomy.
1/12/2015
CS 2302-SPRING 2015 6
Subclass Relationships All mammals (those in class mammalia) are also chordates (members of the phylum chordata)
◦ Chordates have, among other features, a hollow dorsal nerve cord, at least at some stage of development
◦ Mammals all have a hollow dorsal nerve cord, early in embryonic development◦ Mammals have other characteristics that are not shared by all chordates, such as three middle ear
bones and mammary glands◦ There are chordates that are not mammals. For example, bony fish
All primates (those in order primate) are also mammals (those in class mammalia)◦ Primates share all the characteristics that mammals have◦ Primates have other characteristics, not shared by non-primate mammals, such as nails instead of claws
1/12/2015
CS 2302-SPRING 2015 7
Remarks on Human Taxonomy
We belong to various groups based on common anatomical characteristics
We humans are mammals, along with bears, dogs, elephants and whales. ◦ Hair, three middle ear bones, mammary glands
We are primates, along with lemurs and monkeys ◦ Forward facing eyes, keratin nails, domed cranium
We are hominidae, along with chimpanzees and gorillas ◦ Genetic similarities are the most recent basis for classification
1/12/2015
CS 2302-SPRING 2015 8
GeometricObject Example One important and flexible way to create pictures is as a composition of simple figures
◦ One scheme, SVG, is used to create sophisticated images from simple components.◦ The clock is a nice example:http://tavmjong.free.fr/INKSCAPE/DRAWINGS/clock2.svg
The GeometricObject example in this chapter shows how inheritance can be used to organize a 'family' of related classes.
This example will be elaborated upon in later chapters and, eventually, used to create actual pictures.
1/12/2015
CS 2302-SPRING 2015 9
Implementation Setup We will change the book example in a couple of ways
◦ The example will use packages◦ Because the packages take care of 'name collision', the names of the classes will be simplified
Create package cs2302sp15.chap11
Create the basic class files
1/12/2015
CS 2302-SPRING 2015 10
Analyzing Geometry Circles and rectangles make an easy first step
Common characteristics of these two are ◦ Color to use ◦ Whether to fill in the figure or just an outline ◦ The date on which the object was created
Characteristics unique to the figures are ◦ Circle
◦ radius
◦ Rectangle ◦ height◦ width
1/12/2015
CS 2302-SPRING 2015 11
Design Three classes will be created in Java
◦ GeometricObject◦ Rectangle◦ Circle
GeometricObject represents the common features of Rectangle and Circle
The Rectangle and Circle classes represent the features specific to those shapes
1/12/2015
12
UML Diagram
CS 2302-SPRING 2015 13
UML Diagram Explained Each class is a rectangle
◦ Name at the top◦ Instance variables in the middle◦ Instance methods at the bottom
+ indicates public, - indicates private
The arrows with open triangular heads represent the inheritance relationships ◦ All circles are geometric objects: the circle class is a subclass of the geometric object class ◦ All rectangles are geometric objects: the rectangle class is a subclass of the geometric object class
1/12/2015
CS 2302-SPRING 2015 14
Other Terminology The circle class extends the geometric object class
The geometric object class is a superclass of the circle class
The geometric object class generalizes the circle class
1/12/2015
CS 2302-SPRING 2015 15
Representing Inheritance in Java
The extends keyword indicates that the class being defined is a subclass of another class
The name of the other class follows the keyword
A class may extend only one other class
If no explicit superclass is designated, then Object is the implicit superclass
1/12/2015
public class Circle extends GeometricObject
CS 2302-SPRING 2015 16
Filling in the Classes Attributes
◦ GeometricObject: color, filled, dateCreated◦ Circle: radius◦ Rectangle: width, height
Methods◦ Getters and setters (note isFilled rather than getFilled)◦ Get area and perimeter in Circle and Rectangle◦ printcircle◦ toString in GeometricObject◦ Various constructors
1/12/2015
CS 2302-SPRING 2015 17
A Program We see that we can use the methods defined in GeometricObject in classes Circle and Rectangle
Create a Circle, print some attributes
Create a Rectangle, print some attributes
1/12/2015