1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.

Slides:



Advertisements
Similar presentations
Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
Advertisements

Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
LOGO Lecturer: Abdullahi Salad Abdi May 1, Object Oriented Programming in C++
Inheritance. Today: Inheritance and derived classes Is-A relationship class hierarchies proper inheritance (pure) polymorphism virtual functions protected.
Inheritance Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
C++ Review. User Defined Types Built-in data types are simple. Specific problems may demand aggregate/more complex data types. – Ex: polygons, matrices,
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Object-Oriented PHP (1)
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,
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
OOP Etgar 2008 – Recitation 61 Object Oriented Programming Etgar 2008 Recitation 6.
OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.
UML Class Diagram: class Rectangle
Computer Science I Inheritance Professor Evan Korth New York University.
CSSE501 Object-Oriented Development
Object Oriented Software Development
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Object-Oriented Programming with C++ Yingcai Xiao.
CMSC 202 Inheritance.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and 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.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
Chapter 10 Inheritance and Polymorphism
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
CSCI-383 Object-Oriented Programming & Design Lecture 10.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
Object-Oriented Programming Chapter Chapter
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
More Design Patterns From: Shalloway & Trott, Design Patterns Explained, 2 nd ed.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
April 27, 1998CS102-02Lecture 5-1 Object-Oriented Programming Revisited CS Lecture 5-1 The Wonderful World of Objects.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
29-July-2002cse Inheritance © 2002 University of Washington1 Inheritance CSE 142, Summer 2002 Computer Programming 1
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
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:
OO in Context Lecture 13: Dolores Zage. Confused about OO Not alone, there is much confusion about OO many programs are claimed to be OO but are not really.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS 5JA Introduction to Java Pop Quiz Define/Explain encapsulation. In object-oriented programming, encapsulation refers to the grouping of data and the.
OBJECT ORIENTED PROGRAMMING. Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
 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.
Object Oriented Programming: Inheritance Chapter 9.
Inheritance CMSC 202, Version 4/02.
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Adapter Design Pattern
BCA-II Object Oriented Programming using C++
Object Oriented Analysis and Design
Object-Oriented Programming
Presentation transcript:

1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is an extension of the other. Examples: Vehicle - Car Shape - Circle In these examples, the first class provides a rather general, abstract description while the second class is more detailed. a circle has all the characteristics and functions of a shape, plus some circle-specific characteristics/functions.

2 Inheritance You may have several types of objects that are all extensions of the same type: The key is to correctly identify the similarities and differences between the types. A circle is a shape, a square is a shape, but a square is not a circle. Every circle is a shape but not every shape is a circle. Shape CircleSquareTriangle

3 Inheritance Advantages of inheritance: Code reuse A function that operates on a Shape object, can be used on any of the Circle, Square or Triangle objects. Structural simplicity By modeling the world in this way, we reduce its complexity

4 Inheritance The main class (e.g. Shape) is called the base class The subclasses (e.g. Circle, Square) are called derived classes The derived classes inherit attributes and operations from the main class. However, not everything that is inherited can be accessed directly. Anything that is private in the base class cannot be accessed in the derived class. However, we do not want to have to make certain data members public only for the benefit of a derived class. Instead, we can make them protected.

5 Inheritance A class member (data or function) may be public, protected, or private Furthermore, the type of inheritance may be public, protected, or private Depending on the inheritance type, there are different access rules for each kind of class member

6 Public inheritance If A is the base class and B is the derived class: In B, the public parts of A are public the protected parts of A are protected the private parts of A are inaccessible Public inheritance is the most common type of inheritance. It defines an is-a relationship

7 Protected inheritance If A is the base class and B is the derived class: In B, the public parts of A are protected the protected parts of A are protected the private parts of A are inaccessible Not very common...

8 Private inheritance If A is the base class and B is the derived class: In B, the public parts of A are private the protected parts of A are private the private parts of A are inaccessible Private inheritance is generally used to define a has-a relationship, as an alternative to composition.

9 Public inheritance We will concentrate on public inheritance. Syntax: class Shape { private:... protected:... public:... }; class Square : public Shape { private:... public:... };

10 Public inheritance class Shape { private:... public:... void returnPosition(); }; class Square : public Shape { private:... public:... }; int main () { Square s; s.returnPosition();... } If there is no returnPosition() defined in Square, the one from Shape is called.

11 Public inheritance class Shape { private:... public:... void printAttributes(); }; class Square : public Shape { private:... public:... void printAttributes(); }; int main () { Square s; s.printAttributes();... } This member hides the one defined in Shape

12 Public inheritance Shape::Shape() { cout << “Shape constructor\n”; } Square::Square() { cout << “Square constructor\n”; } int main () { Square s;... } this calls the Square constructor which automatically calls the Shape constructor. The program will print: Shape constructor Square constructor

13 Public inheritance int main () { Shape someshape; Square box; someshape = box; // OK (a box is a shape) // However, only the shape part // of the box is assigned to someshape box = someshape; // ERROR (a shape is not a box)... }