Download presentation
Presentation is loading. Please wait.
Published byAmber Smith Modified over 9 years ago
1
Inheritance Doing More with Your Classes
2
Review of Classes Collection of instance variables and instance methods Treated like a data type Encapsulation and Data Hiding
3
Inheritance: An Intuitive Example Think about this school community... Everyone is a CommunityMember This can be broken into groups These groups can be broken into other groups
4
CommunityMember Employee Student Alumnus FacultyStaff AdministratorTeacher UnderClassmanUpperClassman PrivilegesNoPrivileges This is kind of chart is called an “inheritance hierarchy” or “class hierarchy”
5
Here’s the important part: Each subgroup has all of the elements from the group above it Add special features to each subgroup that other groups don’t have –More on this in a bit...
6
A few more everyday examples Vehicles Bank Accounts Polygons
7
Vehicle Car Automobile Train Helicopter Truck Van Commuter Freight Military
8
BankAccount CD Savings Checking Retirement Basic MoneyMarket Business NoInterest Interest 401(k) Personal IRA
9
Polygons Right Triangle Quadrilateral Hexagon Isosceles Scalene Parallelogram Rhombus Rectangle Square Trapezoid RegHex Multiple Inheritance
10
Some Terminology “is-a” –Indicates that a class “is a” subclass of another class –Teacher “is a” Faculty –Square “is a” Rectangle “has-a” –Indicates that an object is an instance variable of another class –Student “has a” Grades
11
More Terminology Direct Inheritance –Object only 1 “level” below superclass –Triangle and Polygon (direct subclass) Indirect Inheritance –Object connected to a superclass through one or more other subclasses –Rectangle and Polygon
12
Let's look back at our School Community example again
13
CommunityMember Employee Student Alumnus FacultyStaff AdministratorTeacher UnderClassmanUpperClassman PrivilegesNoPrivileges
14
All CommunityMembers have a first and last name All Employees are CommunityMembers, so they have first and last names –they also have an employee ID number and years of service All Faculty are Employees, so they are also CommunityMembers, and have all 4 previous parts –they also have a salary All Teachers are Faculty, so they are also Employees and CommunityMembers, so they have all 5 previous parts –they also have a class schedule, education level, and type of certificate
15
CommunityMember –firstName –lastName Employee –firstName –LastName –employeeID –yearsOfService Faculty –firstName –lastName –employeeID –yearsOfService –salary Teacher –firstName –lastName –employeeID –yearsOfService –salary –classSchedule –educationLevel –typeOfCertificate
16
Looking Ahead Tomorrow we will look at how this translates to code and objects Use our “School Community” hierarchy Packets with examples coming tomorrow –Don't lose them! –Make personal notes on them –Study them
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.