WHAT IS INHERITANCE? Java Unit 11: Inheritance I.

Slides:



Advertisements
Similar presentations
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
Advertisements

Classification CP Biology.
Unit 1: Java and Eclipse UML. Depending on the source, the acronym UML is said to stand for “unified modeling language” or “universal modeling language”.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Systems Analysis and Design 8th Edition
Classification of Organisms Chapter 18 What is an Organism? An organism is generally referred to any living thing. More specifically any thing that has.
Classification of Organisms. Categories of Biological Classification Scientists Assign Organisms Two-Word Names 2,000 yrs ago, Aristotle grouped plants.
Classification Biology History Carolus Linnaeus (1707–1778) was born. His great work, the Systema Naturae, ran through twelve editions during his lifetime.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Lecture 32 Inheritance COMP1681 / SE15 Introduction to Programming.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Object-oriented Programming Concepts
Chapter 10 Classes Continued
Week 9 Generalization, inheritance, class hierarchy and graphics.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
1 Object-Oriented Programming (Java), Unit 13 Kirk Scott.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Tree.
CLASSIFICATION “Grouping together items into categories based on a set criteria”
17.1 THE HISTORY OF CLASSIFICATION
Protege OWL Plugin Short Tutorial. OWL Usage The world wide web is a natural application area of ontologies, because ontologies could be used to describe.
1 Object-Oriented Programming (Java), Unit 12 Kirk Scott.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
4.1 Instance Variables, Constructors, and Methods.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Inheritance CS 2302 SPRING Inheritance In this part we introduce a new relationship between classes: inheritance. This is the fundamental feature.
1 Object-Oriented Programming (Java), Unit 12 Kirk Scott.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Systems Analysis and Design 8 th Edition Chapter 6 Object Modeling.
UNIT 6 - Evolution SWBAT compare the relatedness of various species by applying taxonomic principles (cladistics, phylogeny, morphology and DNA.
Copyright © Curt Hill Inheritance and Polymorphism A Powerful Technique.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
1 Object-Oriented Programming (Java), Unit 12 Kirk Scott.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
Coming up: Inheritance
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Class Inheritance. The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers,
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
The Importance of Classification I. The need for systems A. Taxonomy 1. The practice of naming & classifying organisms 1. The practice of naming.
Java Unit 11: Inheritance I
Java Unit 11: Inheritance I
Inheritance Inheritance allows a software developer to derive a new class by extending an existing one. The existing class is called the parent class or.
Object-Oriented Programming (Java), Unit 12
Object-Oriented Programming (Java), Unit 13
Java Unit 11: Inheritance I
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Taxonomy Biology 8(A).
CLASSIFICATION.
CLASSIFICATION.
Taxonomy Ch (p ) Taxonomy = grouping organisms according to their characteristics and evolution •People like to classify things; these classifications.
Let’s Classify!.
Review of Previous Lesson
TAXONOMY.
Chapter 7 Inheritance.
Presentation transcript:

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.

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 ObjectCupSeed

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 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.

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.