Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance.

Similar presentations


Presentation on theme: "Inheritance."— Presentation transcript:

1 Inheritance

2 Consider this…

3 What is inheritance? Inheritance is the process of creating new classes that are based on existing classes The new classes extend the capabilities of the classes that they inherit from The existing class that gets extended is called the parent class (or super class (or base class))

4 What is Inheritance? (cont’d)
The child inherits the fields and methods of the parent. Code that you want to be common to both the parent and the child class only needs to be defined once in the parent class.

5 Employee Support Staff Teacher Contract Teacher EOT Custodial Admin
E/one working at the school is an employee with common characterisitcs: first name, last name, DOB, SIN, etc. Illustrating Inheritance Employee Employees fall into 2 broad categoies: teaching staff or support staff Support Staff Teacher Contract Teacher EOT Custodial Admin Supply Support staff usually work in Admin or as Custodians Teachers can be dept. heads, permanent board teachers, supply teachers or EOTs

6 Employee Teacher Contract Teacher EOT Supply
Let’s just look at the Teacher strand of the Employee hierarchy. Inheritance lets us simplify the structure and reduce the amount of code needed to represent the teachers in a school. name, address and DOB could go in the employee class since all types of employees would have this data Teacher The Teacher class might need a subject and/or room field. Note that there’s no need to include name, address and DOB fields since these are inherited from the parent class (Employee). Contract Teacher EOT Supply EOTs only work for 1 semester at a time so their class might include a termination date field. EOT would still inherit the name, address and DOB fields from its grandparent as well as a subject and room field from its parent.

7 Employee Hierarchy in Code
public class Employee { String firstName; String lastName; String address; String DOB; } public class Teacher extends Employee { String subject; String roomNum; The Employee parent class contains the fields that will be common to all the children. The child class, Teacher, shows its relationship to the parent by using the keyword extends. The child class only contains the fields that are unique to it.

8 “Grandchildren” We extended the Employee class to create the Teacher class. We can extend the Teacher class to create further sub-classes: EOT and Contract.

9 “Grandchildren” Perhaps EOT includes a termination date.
public class EOT extends Teacher { String termDate; } Perhaps Contract includes a retirement date. public class Contract extends Teacher{ String retireDate;

10 “GrandChildren” It’s an easy mistake to extend the Employee class instead of the Teacher class when creating EOT or Contract. The code would compile but attempting to access Teacher class fields from an EOT or Contract object would result in a “Cannot Resolve Symbol” error.

11 The Value of Inheritance
Without inheritance we would have to repeatedly add the same fields to our new classes even though the classes were closely related to each other. Changing the design of our fields would require changes in every class that used them. With inheritance we make one change in the appropriate parent class.

12 “is a” and “has a” It can be confusing to figure out the natural or appropriate hierarchy of classes when designing. “is a” and “has a” relationships are mental tools to help clarify the relationships between classes.

13 “is a” If we can say that some “x” is a “y” then that means that x is a subclass of y. For instance Teacher “is a(n)” Employee and EOT “is a” Teacher.

14 “has a” If we can say that class “x” has a “y” then y should be a field of class x. Teacher “has a” subject so subject is a proper field of Teacher.

15 You do Design and implement a 3 level class hierarchy modelling a real world context or set of objects. Create fields within subclasses that distinguish them from their parents. Create get and set accessor methods in each class Create a main class that instantiates the grandchild. Show that you can get and set each of the fields of the parent and grandparent class.

16 The Ultimate Parent Class
The Object class is the superclass of all classes. Every other class already defined or yet to be defined is a subclass of the Object class.

17 equals() and toString()
equals() and toString() are two methods defined in the Object class and which all subclasses inherit equals(Object obj) returns true if the object passed in (obj) is equal to the object calling the method. toString() returns some kind of string representing the object.

18 equals() and toString()
Neither of these methods does anything interesting… …as inherited Subclasses typically implement their own versions of these two methods. equals() as implemented in the Circle class might check to see if the radii of the two circles being compared are the same.

19 equals() and toString()- overriding
If a subclass redefines a method inherited from a parent class it is said to override the superclass method. Note that the method declaration remains constant. See Circle’s implementation of the equals() method on P186 of the text. equals() on P186 has the same method declaration/signature as the method in the Object class: public boolean equals(Object c)

20 Udoo Pg 187: Review: Circle Part 4 of 4 Review: Rectangle Part 3 of 5
Review: Coin Part 2 of 2


Download ppt "Inheritance."

Similar presentations


Ads by Google