150 likes | 254 Views
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.
E N D
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
Inheritance is the capability to derive one class from another.
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
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
Simple Inheritance • In simple inheritance, each derived type has only one immediate base type Circle (base class) Sphere (derived class) Cylinder (derived class)
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
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
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 };
//********************************************************************//******************************************************************** // 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(); } }
//********************************************************************//******************************************************************** // 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); } }
//********************************************************************//******************************************************************** // 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); } }
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