Download presentation
Presentation is loading. Please wait.
1
Code reuse through subtyping
Inheritance Basics Code reuse through subtyping
2
Inheritance : What is It
Technical mechanism for code reuse Conceptual design mechanism : is-a relationship
3
Inheritance Terminology
Inheritance : derive a class from another class Terminology Parent or Base class Derived or Child class UML Arrow from derived to parent class
4
Conceptual Model How to visualize inheritance:
Apple consists of a Fruit object and an Apple wrapper Wrapper can add new features / hide existing ones Fruit Apple Other Code
5
Inheritance Heirarchies
Multiple classes can derive from one base Can derive from a class that is derived
6
Geometric Object Example
Function also available in Circle & Rectagle Circle & Rectangle could use toString() but chose to override
7
Circle Interactions Calls on a derived class handled by code there if possible, otherwise by parent class Geometric Object Circle
8
Circle Code State inheritance with : public ParentClass
Do not redeclare parent members unless overriding them
9
Using Inherited Members
Derived class does NOT have access to private members of parent:
10
Using Inherited Members
Derived class does NOT have access to private members of parent Must use public interface to access
11
Generic Programming A derived class can be treated as parent type:
12
Protected Access Private Only accessible in this class
13
Protected Access Private Only accessible in this class
Public Accessible anywhere
14
Protected Access Private Only accessible in this class
Public Accessible anywhere Protected Accessible in this class and any subclasses
15
Access Summary Private Protected Public Member variables
Helper functions no one else should use Protected Member variables it is reasonable for child classes to work with Functions you only want subclasses to use Public Functions Constants
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.