1 / 15

Inheritance

Inheritance. It’s generally not a good idea for programmers to work hard. I often tell my students that they should strive to be successfully lazy. - David Conger. Object-Oriented Language. Encapsulation Inheritance Polymorphism.

pierce
Télécharger la présentation

Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance

  2. It’s generally not a good idea for programmers to work hard. I often tell my students that they should strive to be successfully lazy. - David Conger

  3. Object-Oriented Language • Encapsulation • Inheritance • Polymorphism

  4. Inheritance is the capability to derive one class from another.

  5. Inheritance • The initial class used as the basis for the derived class is referred to as either the base class, parent class, or superclass. • The derived class is referred to as either the derived class, child class, or subclass

  6. The Derived Class • Is a completely new class • Incorporates all of the behaviors and characteristics of its base class • Typically adds its own additional new data and methods • Can override any base class function

  7. Simple Inheritance • In simple inheritance, each derived type has only one immediate base type Circle (base class) Sphere (derived class) Cylinder (derived class)

  8. The Derivation Process • Establishes a specific kind of relationship between two classes • This relationship is called an: is-arelationship • This type of relationship means that the derived class is a more specific version of its parent class

  9. A Derived Class • Has the same form as any other class, consisting of a declaration and implementation • However, has a distinguishing addition to the first line of its declaration: public class DerivedClassNameextends BaseClassName

  10. A Derived Class If Circle is an existing class, a new class, Cylinder can be derived as follows: public class Cylinder extends Circle { // add additional members here };

  11. //********************************************************************//******************************************************************** // Words2.java Author: Lewis/Loftus // Demonstrates the use of the super reference. //******************************************************************** public class Words2 { //--------------------------------------------------- // Instantiates a derived class and invokes its inherited and // local methods. //--------------------------------------------------- public static void main (String[] args) { Dictionary2 webster = new Dictionary2 (1500, 52500); webster.pageMessage(); webster.definitionMessage(); } }

  12. //********************************************************************//******************************************************************** // Book2.java Author: Lewis/Loftus //******************************************************************** public class Book2 { protected int pages; // Sets up the book with the specified number of pages. public Book2 (int numPages) { pages = numPages; } // Prints a message about the pages of this book. public void pageMessage () { System.out.println ("Number of pages: " + pages); } }

  13. //********************************************************************//******************************************************************** // Dictionary2.java Author: Lewis/Loftus public class Dictionary2 extends Book2 { private int definitions; // Sets up the dictionary with the specified number of pages public Dictionary2 (int numPages, int numDefinitions) { super (numPages); definitions = numDefinitions; } // Prints a message using both local and inherited values. public void definitionMessage () { System.out.println ("Number of definitions: " + definitions); System.out.println ("Definitions per page: " + definitions/pages); } }

  14. Access Specifications • Private and public access specifiers have been used to restrict access within a class • As private access prevents access by any non-class member, it also precludes access by any derived class member as well • To retain a restricted type of access across derived classes, C++ provides a third access specification, protected

  15. End Inheritance

More Related