Inheritance Doing More with Your Classes
Review of Classes Collection of instance variables and instance methods Treated like a data type Encapsulation and Data Hiding
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
CommunityMember Employee Student Alumnus FacultyStaff AdministratorTeacher UnderClassmanUpperClassman PrivilegesNoPrivileges This is kind of chart is called an “inheritance hierarchy” or “class hierarchy”
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...
A few more everyday examples Vehicles Bank Accounts Polygons
Vehicle Car Automobile Train Helicopter Truck Van Commuter Freight Military
BankAccount CD Savings Checking Retirement Basic MoneyMarket Business NoInterest Interest 401(k) Personal IRA
Polygons Right Triangle Quadrilateral Hexagon Isosceles Scalene Parallelogram Rhombus Rectangle Square Trapezoid RegHex Multiple Inheritance
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
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
Let's look back at our School Community example again
CommunityMember Employee Student Alumnus FacultyStaff AdministratorTeacher UnderClassmanUpperClassman PrivilegesNoPrivileges
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
CommunityMember –firstName –lastName Employee –firstName –LastName –employeeID –yearsOfService Faculty –firstName –lastName –employeeID –yearsOfService –salary Teacher –firstName –lastName –employeeID –yearsOfService –salary –classSchedule –educationLevel –typeOfCertificate
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