CMSC 202 Lesson 16 Inheritance. Warmup Identify which constructor each of the following use (default, non-default, copy) MyClass a; MyClass b(a); MyClass.

Slides:



Advertisements
Similar presentations
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Advertisements

Chapter 1 Inheritance University Of Ha’il.
Reusable Classes.  Motivation: Write less code!
LOGO Lecturer: Abdullahi Salad Abdi May 1, Object Oriented Programming in C++
CS 211 Inheritance AAA.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
09 Inheritance. 2 Contents Defining Inheritance Relationships of Inheritance Rules of Inheritance super and this references super() and this() methods.
Inheritance Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors.
Inheritance Inheritance Reserved word protected Reserved word super
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
CS 106 Introduction to Computer Science I 11 / 13 / 2006 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,
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.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
Computer Science I Inheritance Professor Evan Korth New York University.
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance
LECTURE 07 Programming using C# Inheritance
Inheritance using Java
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
CMSC 202 Lesson 19 Polymorphism 2. Warmup What is wrong with the following code? What error will it produce? (Hint: it already compiles) for (unsigned.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
CMSC 202 Lesson 14 Copy and Assignment. Warmup Write the constructor for the following class: class CellPhone { public: CellPhone(int number, const string&
Peyman Dodangeh Sharif University of Technology Fall 2014.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
CMSC 202 Lesson 18 Polymorphism 1. Warmup What errors are present in the following hierarchy? Assume GetCurrentTime() and RingBell() are defined elsewhere.
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.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.
A Introduction to Computing II Lecture 3: Interfaces and Inheritance Fall Session 2000.
OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a type of’ e.g. a revolver is a type of gun  Aggregation.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Advanced Programming in Java
Sections Inheritance and Abstract Classes
Lecture 12 Inheritance.
Inheritance CMSC 202, Version 4/02.
Objects as a programming concept
Interface, Subclass, and Abstract Class Review
Inheritance and Polymorphism
One class is an extension of another.
Inheritance II CMSC 202.
Computing with C# and the .NET Framework
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object-Oriented Programming: Inheritance
Inheritance I CMSC 202.
CMSC 202 Lesson 16 Inheritance.
Chapter 10 Thinking in Objects
Module 4: Implementing Object-Oriented Programming Techniques in C#
CSC 205 Java Programming II
Object-Oriented Programming: Inheritance
Lecture 22 Inheritance Richard Gesick.
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object Oriented Analysis and Design
CMSC202 Computer Science II for Majors Lecture 10 and 11 – Inheritance
CMSC 202 Lesson 19 Polymorphism 2.
Object-Oriented Programming: Inheritance
Programming in C# CHAPTER 5 & 6
CMSC 202 Lesson 17 Inheritance II.
Presentation transcript:

CMSC 202 Lesson 16 Inheritance

Warmup Identify which constructor each of the following use (default, non-default, copy) MyClass a; MyClass b(a); MyClass c(2); MyClass* d = new MyClass; MyClass* e = new MyClass(*d); MyClass* f = new MyClass(4);

Code Reuse How have we seen Code Reuse so far? Functions Function Libraries  Ex: math -> pow, sqrt Classes Class Libraries  Ex: vector, string Aggregation Customer “has-a” DVD RentalSystem “has-a” Customer

Object Relationships “Uses a” Object_A “uses a” Object_B Ex: Student sits in a chair “Has a” Object_A “has a” Object_B Ex: Student has a name “Is a” or “Is a kind of” Object_A “is a” Object_B Ex: Student is a kind of Person

Inheritance What is Inheritance? Unfortunately – not what your parents/grandparents will be giving you… Inheritance “is a” or “is a kind of” relationship Code reuse by sharing related methods Specific classes “inherit” methods from general classes Examples A student is a person A professor is a faculty member A lecturer is a faculty member

Inheritance Hierarchy

Why Inheritance? Abstraction for sharing similarities while retaining differences Group classes into related families Share common operations and data Multiple inheritance is possible Inherit from multiple base classes Not advisable Promotes code reuse Design general class once Extend implementation through inheritance

Inheritance and Classes Base class (or superclass) More general class Contains common data Contains common operations Derived class (or subclass) More specific class Inherits data from Base class Inherits operations from Base class Uses, modifies, extends, or replaces Base class behaviors

Inheritance Example University Member Name Address Course Schedule Faculty Area of Research Advisees Student Major GPA

Inheritance Assume the hierarchy on the right… A is Base class B is derived class B derives from A Every B is an A Every A is NOT a B Some A’s are B’s Class A Class B A objects B objects

Inheritance Assume the hierarchy on the right… Everywhere an A can be used, a B can be used Parameters Return values Items in vectors Items in arrays Reverse is not true… Inheritance so far? ifstream is an istream ofstream is an ostream istream ifstream istream objects ifstream objects

Trip to the Zoo Animal eat() sleep() reproduce() Mammal giveBirth() Reptile layEggs() Lion roar() Dolphin doTrick() Rattlesnake rattle() Gecko loseTail()

Inheritance class BaseClass { public: // operations private: // data }; class DerivedClass : public BaseClass { public: // operations private: // data }; Indicates that this derived class inherits data and operations from this base class

Inheritance in Action class Animal { }; class Mammal : public Animal { }; class Lion : public Mammal { }; class Dolphin : public Mammal { }; class Reptile : public Animal { }; class Gecko : public Reptile { }; class Rattlesnake : public Reptile { }; Animal eat() sleep() reproduce() Mammal giveBirth() Reptile layEggs() Lion roar() Dolphin doTrick() Rattlesnake rattle() Gecko loseTail()

Challenge Draw the hierarchy for a Vehicle class What kinds of vehicles are there? Personal, Commercial, etc. What kinds of personal vehicles are there? Cars, Motorcycles, Trucks, etc. What kinds of commercial vehicles are there? Planes, Trains, etc.