Inheritance I CMSC 202.

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

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.
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.
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,
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
Inheritance using Java
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 16 Inheritance. Warmup Identify which constructor each of the following use (default, non-default, copy) MyClass a; MyClass b(a); MyClass.
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.
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.
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
Object-Oriented Programming
Inheritance II CMSC 202.
Advanced Programming in Java
Computing with C# and the .NET Framework
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object-Oriented Programming: Inheritance
ATS Application Programming: Java Programming
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
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Advanced Programming Behnam Hatami Fall 2017.
Class Reuse with Inheritance
Two big words Inheritance Polymorphism
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Fundaments of Game Design
Adapter Design Pattern
Object Oriented Analysis and Design
CMSC202 Computer Science II for Majors Lecture 10 and 11 – Inheritance
Java Inheritance.
Inheritance Lakshmish Ramaswamy.
Object-Oriented Programming: Inheritance
Lecture 6 Inheritance CSE /26/2018.
Programming in C# CHAPTER 5 & 6
CMSC 202 Lesson 17 Inheritance II.
Presentation transcript:

Inheritance I CMSC 202

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 Classes 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” “Has a” “Is a” or “Is a kind of” 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? Inheritance Examples 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 Student ------------------------- Major GPA Faculty ------------------------- Area of Research Advisees

Inheritance Assume the hierarchy on the right… A is Base class Class A 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 B A objects B objects

Inheritance Assume the hierarchy on the right… Inheritance so far? istream 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 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 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.