Copyright © 1999-2015 Curt Hill Inheritance and Polymorphism A Powerful Technique.

Slides:



Advertisements
Similar presentations
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Chapter 10: Inheritance and Polymorphism
Chapter 10 Classes Continued
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
WHAT IS INHERITANCE? Java Unit 11: Inheritance I.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Copyright © Curt Hill Structured Data What this course is about.
Copyright Curt Hill Variables What are they? Why do we need them?
Copyright © Curt Hill Inheritance in C++ How to do it.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Object Oriented Programming
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Coming up: Inheritance
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Class Inheritance. The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance ndex.html ndex.htmland “Java.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
1 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance in C++ How to do it Copyright © Curt Hill
Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism
Inheritance and Polymorphism
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Multiple Inheritance in C++
Presentation transcript:

Copyright © Curt Hill Inheritance and Polymorphism A Powerful Technique

Copyright © Curt Hill Biology Example Class is a loan word from biology –Means group of related orders, families and species Animals (a kingdom) –Vertebrates (a phylum) –Mammals (a class) –Rodents (an order) –Squirrels (a family) –Thirteen line ground squirrel (a specie)

Copyright © Curt Hill Relationship The prior list shows an is a relationship A thirteen line ground squirrel is a squirrel A squirrel is a rodent A thirteen line ground has all the characteristics of any of the above

Copyright © Curt Hill Composition and Inheritance Composition expresses a has a relationship –Fraction has a numerator (integer) Inheritance expresses an is a relationship –A student is a person

Copyright © Curt Hill Inheritance Deriving new classes from old classes New class may retain any properties or methods of old New class may add new properties, new methods New class may replace old properties or methods

Copyright © Curt Hill Inheritance example Person has a name and age Student is a person with GPA and major Employee is a person with a wage Grad is a student with degree

Copyright © Curt Hill person employeestudent grad set_name set_wageset_major set_degree

Copyright © Curt Hill Syntax details The Java extends keyword indicates subclass: class student extends person { The person constructor will be called as part of the student constructor If we want a particular constructor from person we call with super

Copyright © Curt Hill Super Constructor Example class student extends person{... public student (String nme, char m_or_f, date d){ super(nme,m_or_f,d); hours=0;... }

Copyright © Curt Hill Properties and methods All classes have properties (eg. name and age) and methods (eg. set_name) –Person explicitly declares it –All the rest inherit it –Can access if public or protected

Copyright © Curt Hill Using superclass methods Use super as a class name to get the immediate super class method super.x(y)

Copyright © Curt Hill The print routine In person: public void print(){ System.out.print(name); System.out.print(' ');... In student: public void print() { super.print(); System.out.print(" hrs: "); System.out.print(hours);

Copyright © Curt Hill Polymorphism Literally: many shapes Classes that are related are interchangable In Java any container, such as an array can have related contents An array of person can also contain students

Copyright © Curt Hill Polymorphism Example person ptr[] = new person[max]; // person must be super class ptr[0] = new person(); ptr[1] = new student(); ptr[2] = new employee();

Copyright © Curt Hill Polymorphic methods What can be done to an array of some super class? Anything that can be done to all of them –Methods defined only in super class Such as get_name –Methods redefined in subclasses Such as print

Copyright © Curt Hill Inherited functions Execute the set_name function against any variable of these four types –The same effect occurs –Defined in the base type –Variable is upcast, that is converted to a base type

Copyright © Curt Hill Polymorphism Execute the print function against any variable of these four types –The different effects occur based on the actual type of the variable –It just calls print knowing that all person derived classes have a print function A function can accept a person type and receive either a person, student, employee or grad without problem

Copyright © Curt Hill Polymorphic Example for (int i=0;i<max;i++) ptr[i].print(); Prints them without knowing in advance which are students and which are persons Not knowing which routine to call until runtime is dynamic binding

Copyright © Curt Hill Polymorhism Again Polymorphism requires no effort by the programmer All that needs to be done is point a handle at a method and execute The system determines what the pointer that it has points at From a type perspective it could be what it is declared or any descendent

Copyright © Curt Hill Object Every class is a subclass of Object Thus: class Item {… is the same as: class Item extends Object { … Thus all classes are polymorphic All may use Object methods –Object has no properties

Copyright © Curt Hill Object There are a variety of methods: –equals –getClass –hashcode –notify –toString –wait These are often overridden in descendent classes

Copyright © Curt Hill The Object Hierarchy Since every class is a subclass of Object then Object obarr[]; can hold anything but a primitive –It can hold a wrapper such as Integer These can be determined at run-time Versions of Java prior to 5 had no generics –Polymorphism substituted

Copyright © Curt Hill Containers and Polymorphism An array is not the only container Java also has a variety of container classes such as LinkList, Stack, TreeSet, Vector etc. These are dynamic data structures that may contain objects of certain types The types only have to be polymorphically related

Conclusion Inheritance requires the extends reserved word –Interfaces uses implements Java objects are handle based so always available –The handles are always the same size Object is the root of a single inheritance tree, so a container class could contain any object Copyright © Curt Hill