Inheritance Building one object from another. Background Object-oriented programming is normally described has offering three capabilities Encapsulation:

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

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Inheritance Reserved word protected Reserved word super
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
More about classes and objects Classes in Visual Basic.NET.
Object-Oriented PHP (1)
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Advanced Object-Oriented Programming Features
1 Inheritance and Polymorphism Inheritance (Continued) Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1.11 Introduction to OOP academy.zariba.com 1. Lecture Content 1.What is OOP and why use it? 2.Classes and objects 3.Static classes 4.Properties, fields.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
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.
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.
Module 7: Object-Oriented Programming in Visual Basic .NET
An Object-Oriented Approach to Programming Logic and Design
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
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. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
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.
21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Object-Oriented Programming: Inheritance and Polymorphism.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
A First Book of C++ Chapter 12 Extending Your Classes.
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.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Inheritance.
Lecture 12 Inheritance.
CHAPTER 5 GENERAL OOP CONCEPTS.
One class is an extension of another.
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Analysis and Design
Module 4: Implementing Object-Oriented Programming Techniques in C#
One class is an extension of another.
Lecture 22 Inheritance Richard Gesick.
Inheritance, Polymorphism, and Interfaces. Oh My
Code reuse through subtyping
Chapter 11 Inheritance and Polymorphism
Object-Oriented Programming
Object-Oriented PHP (1)
ADVANCED OBJECT-ORIENTED PROGRAMMING
CIS 110: Introduction to computer programming
Presentation transcript:

Inheritance Building one object from another

Background Object-oriented programming is normally described has offering three capabilities Encapsulation: creating self-contained objects consisting of data and functions Inheritance: building new objects that extend and modify the features of existing objects Polymorphism: changing the behavior of members in inherited objects We will now turn to the second of these. The third will wait until Module 3.

Overview of Inheritance Three steps: 1. Start with an existing class 2. Create a new class, using the original class as a starting point 3. Add members (data, properties, functions) to extend the original class Benefits: Allows code reuse without “making a copy” of original code Helps manage code complexity

UML Representation Comments: Class diagram can be used to specify inheritance (generalization) relationships Child class shows those members that have been added In this example: At some branch offices, we are selling products at the retail level (from a store front) For these offices, we want to hold the sales tax we have paid, and compute a profit after tax

Implementing Inheritance Implementing inheritance is easy in C# Add the class you are inheriting from (a.k.a., base class) to the definition of the new class (a.k.a., child or sub-class) public class SubClass : BaseClass { // SubClass definition }

Inheriting Constructors The sub-class must define its own constructor functions—these don’t inherit If your base class has useful constructors, the base keyword can be used with arguments, e.g., public class ChildClass : BaseClass { public ChildClass(int x, double y) : base(y) { // Assumes BaseClass(double val) constructor exists }

Example class

Using inherited object Comments: All of the BranchOffice members can be used in the RetailBranchOffic class The 3 argument constructor is also called If we added capabilities to our BranchOffice class, they would become available in our new class as soon as we recompiled

protected Access So far, we’ve introduced two access specifiers public: accessible in child classes private: not accessible in child classes—not even in member functions! protected is the third option Like private, it can only be used in member functions Like public, it inherits—meaning protected member are available in member functions

Base Class: object In C#, all classes inherit from object, including classes you create object includes a number of members: ToString(), which creates a string representation of the object A comparison function, to see if one object is the same object as another A hash coding function useful for lookup Etc. Implication: any object you create will support the these members

Parting words… Inheritance in C# is very powerful, and very simple to implement The base keyword can be used to inherit constructors Additional capabilities not yet covered: Overriding and polymorphism Multiple inheritance using interfaces