Inheritance.

Slides:



Advertisements
Similar presentations
Classes and SubClasses Super Parent Sub Child IS - A.
Advertisements

Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Lecture 10: Inheritance Subclasses and superclasses The inheritance chain Access control The Object cosmic superclass The super keyword Overriding methods.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Inheritance (Part 2) Notes Chapter KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed.
Inheritance and Access Control CS 162 (Summer 2009)
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Week 4 Object-Oriented Programming (1): Inheritance
Lecture 10: Inheritance Subclasses and superclasses
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Abstract Classes.
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Chapter 9 Object-Oriented Programming: Inheritance
MSIS 670 Object-Oriented Software Engineering
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
CIS 110: Introduction to computer programming
CS 240 – Advanced Programming Concepts
Presentation transcript:

Inheritance

Consider this…

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))

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.

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

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.

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.

“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.

“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;

“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.

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.

“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.

“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.

“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.

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.

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.

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.

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.

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)

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