170 likes | 362 Views
Inheritance. CS 2302 Spring 2015. 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.
E N D
Inheritance CS 2302 Spring 2015
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. CS 2302-Spring 2015
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 CS 2302-Spring 2015
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 CS 2302-Spring 2015
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. CS 2302-Spring 2015
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 CS 2302-Spring 2015
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 CS 2302-Spring 2015
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. CS 2302-Spring 2015
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 CS 2302-Spring 2015
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 CS 2302-Spring 2015
Design • Three classes will be created in Java • GeometricObject • Rectangle • Circle • GeometricObjectrepresents the common features of Rectangle and Circle • The Rectangle and Circle classes represent the features specific to those shapes CS 2302-Spring 2015
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 CS 2302-Spring 2015
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 CS 2302-Spring 2015
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 public class Circle extends GeometricObject CS 2302-Spring 2015
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 • toStringin GeometricObject • Various constructors CS 2302-Spring 2015
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 CS 2302-Spring 2015