BEgInSlIdE OOP-1 Inheritance for Code Reuse uAn Array Example uImproving the Array Example.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
ITEC200 – Week03 Inheritance and Class Hierarchies.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter 10 Classes Continued
BEgInSlIdE Copyright C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion 1 The C++ Programming Language Object Oriented Programming (inheritance)
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
OOP Languages: Java vs C++
Inheritance using Java
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Templates and Polymorphism Generic functions and classes
BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
CS 11 C++ track: lecture 7 Today: Templates!. Templates: motivation (1) Lots of code is generic over some type Container data types: List of integers,
Programming Languages and Paradigms Object-Oriented Programming (Part II)
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
Object Oriented Software Development
Dynamic Binding Object-Oriented Programming Spring
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object-Oriented Programming Chapter Chapter
BEgInSlIdE OOP-1 Simple Containers in C++ uList: simple container class uIterators uMemory Allocation Responsibility uMixed type collections uAbstract.
ISBN Object-Oriented Programming Chapter Chapter
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 (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
OO terminology & objectives §Encapsulation l don’t touch my private data l get/set it using my public/package methods §Inheritance l a parent provides.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Variations on Inheritance Object-Oriented Programming Spring
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Motivation for Generic Programming in C++
Modern Programming Tools And Techniques-I
Sections Inheritance and Abstract Classes
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
Inheritance and Polymorphism
Interfaces and Inheritance
C++ Classes C++ Interlude 1.
Inheritance, Polymorphism, and Interfaces. Oh My
Java Programming Language
Chapter 11 Inheritance and Polymorphism Part 1
Lecture 10 Concepts of Programming Languages
Presentation transcript:

bEgInSlIdE OOP-1 Inheritance for Code Reuse uAn Array Example uImproving the Array Example

bEgInSlIdE OOP-2 Strict Inheritance and Class Facets uType: (the class interface, how it can be used from outside) lExtended by inheritance. Subtyping! uImplementation: method bodies. lExtended, only for the added methods. uMold: structure, i.e., list of fields. lExtended, to add new fields. uFactory: The interface for creating objects. lNot inherited at all uMill: The implementation of constructors. lExtended

bEgInSlIdE OOP-3 "Implementation Inheritance" vs. Strict Inheritance uType: (the class interface, how it can be used from outside) lExtended by inheritance. Subtyping! lUnrelated / new type uImplementation: method bodies. lExtended, only for the added methods. lChange by overriding uMold: structure, i.e., list of fields. lExtended, to add new fields. uFactory: The interface for creating objects. lNot inherited at all uMill: The implementation of constructors. lExtended

bEgInSlIdE OOP-4 A C++ Array Class Here is a simple version of an Array class that implements an array of integers with no bound checking. class Array { public: explicit Array(int len_): len(len_), buff(new int[len]){} ~Array(void) { delete[] buff; } int size(void) const { return len; } int& operator[](int i) { return buff[i]; } int operator[](int i) const { return buff[i]; } private: const int len; int * const buff; Array(const Array &); Array & operator =(const Array &); }; class Array { public: explicit Array(int len_): len(len_), buff(new int[len]){} ~Array(void) { delete[] buff; } int size(void) const { return len; } int& operator[](int i) { return buff[i]; } int operator[](int i) const { return buff[i]; } private: const int len; int * const buff; Array(const Array &); Array & operator =(const Array &); };

bEgInSlIdE OOP-5 A Checked Array uWe may also need an array whose bounds are checked in every reference. uCode reuse: define a new derived class CheckedArray that inherits the characteristics of the base class Array : class CheckedArray: private Array { class RangeError{}; public: explicit CheckedArray(int len): Array(len) {} int& operator[](int) throw (RangeError); int operator[](int) const throw (RangeError); using Array::size(); }; int& CheckedArray :: operator[](int i) throw (CheckedArray :: RangeError) { if (0 > i || i >= size()) throw RangeError(); return Array :: operator[] (i); } class CheckedArray: private Array { class RangeError{}; public: explicit CheckedArray(int len): Array(len) {} int& operator[](int) throw (RangeError); int operator[](int) const throw (RangeError); using Array::size(); }; int& CheckedArray :: operator[](int i) throw (CheckedArray :: RangeError) { if (0 > i || i >= size()) throw RangeError(); return Array :: operator[] (i); }

bEgInSlIdE OOP-6 An Entirely New Type uWhen a class inherits from a class / struct, an access level definition must be used Class CheckedArray has an anonymous private sub- object of type Array ; the fact that CheckedArray inherits from Array is private. class CheckedArray: private Array {... class CheckedArray: private Array {... Inherited methods cannot be used from outside, but a using clause makes it possible to reuse these. class CheckedArray: private Array { public: using Array::size()... class CheckedArray: private Array { public: using Array::size()...

bEgInSlIdE OOP-7 Factory and Mill are Not Inherited! uConstructors are not inherited. If we haven’t defined a constructor for CheckedArray, then the compiler would try to generate an empty default constructor and fail in doing so since there is no default constructor of the base class Array uThe construction (initialization) of the inherited class Array must be done in the header of the constructor of the derived class CheckedArray... explicit CheckedArray(int len): Array(len) {}... explicit CheckedArray(int len): Array(len) {}...

bEgInSlIdE OOP-8 Overriding... return Array :: operator[] (i); return Array :: operator[] (i); if (0 = size()) if (0 = size()) int& CheckedArray :: operator[](int i) int& CheckedArray :: operator[](int i).... uSince CheckedArray doesn’t override the size function of Array, the following calls the inherited function u CheckedArray overrides the array reference function of Array. uAn overridden function can be called as follows:

bEgInSlIdE OOP-9 Non-Strict Inheritance uAKA Inheritance uMore Powerful Abstraction Mechanism: a subclass is just like the superclass, except for an explicit list of changes: lAdditional operations. lAdditional structure elements. lOperations implemented differently, also called overridden operations. uCan be used for lReimplementation lType extension. uNote: there is usually no way to “re-implement” structure elements.