Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.

Slides:



Advertisements
Similar presentations
Outline 1 Introduction 2 Base Classes and Derived Classes 3 protected Members 4 Relationship between Base Classes and Derived Classes 5 Case Study: Three-Level.
Advertisements

CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
C++ Inheritance Systems Programming.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Object Oriented Programming: Inheritance Chapter 9.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
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.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Chapter 8 More Object Concepts
Object Oriented Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Object Oriented Concepts Classes II. Object Oriented concepts Encapsulation Composition Inheritance Polymorphism.
Lecture 9 Polymorphism Richard Gesick.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
More on Object-Oriented Programming: Inheritance 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Object Oriented Programming
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance ndex.html ndex.htmland “Java.
Object-Oriented Programming: Inheritance and Polymorphism.
Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance
Polymorphism, Interfaces & Operator Overloading
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Week 4 Object-Oriented Programming (1): Inheritance
Lecture 23 Polymorphism Richard Gesick.
Object-Oriented Programming: Inheritance
Chapter 9 Object-Oriented Programming: Inheritance
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Fundaments of Game Design
Lecture 6 Inheritance CSE /26/2018.
CIS 199 Final Review.
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Lecture 6 Inheritance CSE /26/2018.
Presentation transcript:

Lecture 8 Inheritance Richard Gesick

2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To create a derived class that inherits attributes and behaviors from a base class. To use access modifier protected to give derived-class methods access to base-class members. To access base-class members with base. How constructors are used in inheritance hierarchies. The methods of class object, the direct or indirect base class of all classes.

3 Introduction Inheritance allows a new class to absorb an existing class’s members. A derived class normally adds its own fields and methods to represent a more specialized group of objects. Inheritance saves time by reusing proven and debugged high-quality software.

4 Introduction The direct base class is the base class which the derived class explicitly inherits. An indirect base class is any class above the direct base class in the class hierarchy. The class hierarchy begins with class object.

5 Introduction The is-a relationship represents inheritance. For example, a car is a vehicle, and a truck is a vehicle. New classes can inherit from thousands of pre-built classes in class libraries.

6 Base Classes and Derived Classes The figure lists several simple examples of base classes and derived classes. Note that base classes are “more general,” and derived classes are “more specific.”

7 Base Classes and Derived Classes The UML class diagram of Fig shows an inheritance hierarchy representing a university community. Each arrow represents an is-a relationship.

8 Base Classes and Derived Classes Now consider the Shape inheritance hierarchy. We can follow the arrows to identify several is-a relationships.

9 Base Classes and Derived Classes Objects of all classes that extend a common base class can be treated as objects of that base class. However, base-class objects cannot be treated as objects of their derived classes. When a derived class needs a customized version of an inherited method, the derived class can override the base-class method.

10 protected Members A base class’s private members are inherited by derived classes, but are not directly accessible by derived-class methods and properties. A base class’s protected members can be accessed by members of that base class and by members of its derived classes. A base class’s protected internal members can be accessed by members of a base class, the derived classes and by any class in the same assembly.

11 protected Members Properties and methods of a derived class cannot directly access private members of the base class. A derived class can change the state of private base-class fields only through non -private methods and properties provided in the base class. If a derived class can access its base class’s private fields, classes that inherit from that base class could access the fields as well. This would propagate access to what should be private fields, and the benefits of information hiding would be lost.

12 Base Classes and Derived Classes A colon ( : ) followed a class name at the end of the class declaration header indicates that the class extends the class to the right of the colon. Every C# class directly or indirectly inherits object ’s methods. If a class does not specify that it inherits from another class, it implicitly inherits from object. The compiler sets the base class of a class to object when the class declaration does not explicitly extend a base class.

13 Base Classes and Derived Classes Declaring instance variables as private and providing public properties to manipulate and validate them helps enforce good design. Constructors are not inherited. Either explicitly or implicitly, a call to the base-class constructor is made. Class object ’s default (empty) constructor does nothing. Note that even if a class does not have constructors, the default constructor will call the base class’s default or parameterless constructor.

14 ToString ToString is special - it is one of the methods that every class inherits directly or indirectly from class object. Method ToString returns a string representing an object. object ’s ToString method is primarily a placeholder that typically should be overridden by a derived class. To override a base-class method, a derived class must declare a method with keyword override. The method must have the same signature (method name, number of parameters and parameter types) and return type as the base-class method.

Base Classes and Derived Classes To override a base-class method, a derived class must declare a method with keyword override. The method must have the same signature (method name, number of parameters and parameter types) and return type as the base-class method 1-15

16 Base Classes and Derived Classes It is a compilation error to override a method with a different access modifier. If a public method could be overridden as a protected or private method, the derived-class objects would not respond to the same method calls as base-class objects.

17 Base Classes and Derived Classes Copying and pasting code from one class to another can spread errors across multiple source-code files. Use inheritance rather than the “copy-and-paste” approach. With inheritance, the common members of all the classes in the hierarchy are declared in a base class. When changes are required for these common features, you need to make the changes only in the base class—derived classes then inherit the changes.

18 Base Classes and Derived Classes A compilation error occurs if a derived-class constructor calls one of its base-class constructors with arguments that do not match the number and types of parameters specified in one of the base-class constructor declarations.

19 Base Classes and Derived Classes The virtual and abstract keywords indicate that a base-class method can be overridden in derived classes. The override modifier declares that a derived- class method overrides a virtual or abstract base-class method. This modifier also implicitly declares the derived-class method virtual.

20 Base Classes and Derived Classes Using protected instance variables creates several potential problems. The derived-class object can set an inherited variable’s value directly without validity checking. Derived-class methods would need to be written to depend on the base class’s data implementation. You should be able to change the base-class implementation while still providing the same services to the derived classes. Declaring base-class instance variables private enables the base- class implementation of these instance variables to change without affecting derived-class implementations.

21 Base Classes and Derived Classes When a base-class method is overridden in a derived class, the derived-class version often calls the base-class version to do a portion of the work. Failure to prefix the base-class method name with the keyword base when referencing the base class’s method causes the derived-class method to call itself. The use of “chained” base references to refer to a member several levels up the hierarchy—as in base.base.Earnings () —is a compilation error.

22 Constructors in Derived Classes The derived-class constructor, before performing its own tasks, invokes its direct base class’s constructor. This is done either explicitly or implicitly. If the base class is derived from another class, the base-class constructor invokes the constructor of the next class up in the hierarchy, and so on.

23 Constructors in Derived Classes When an application creates a derived-class object, the derived-class constructor calls the base-class constructor (explicitly, via base, or implicitly). The base-class constructor’s body executes to initialize the base class’s instance variables that are part of the derived-class object, then the derived class constructor’s body executes to initialize the derived-class-only instance variables. Even if a constructor does not assign a value to an instance variable, the variable is still initialized to its default value.

24 Programming with Inheritance When a new class extends an existing class, the new class inherits the members of the existing class. We can customize the new class to meet our needs by including additional members and by overriding base-class members. Independent software vendors (ISVs) can develop and sell proprietary classes. Users then can derive new classes from these library classes without accessing the source code.

25 Programming with Inheritance Although inheriting from a class does not require access to the class’s source code, developers often insist on seeing the source code to understand how the class is implemented. They may, for example, want to ensure that they are extending a class that performs well and is implemented securely. Effective software reuse greatly improves the software- development process. Object-oriented programming facilitates software reuse. The availability of class libraries delivers the maximum benefits of software reuse.

26 Programming with Inheritance At the design stage in an object-oriented system, the designer often finds that certain classes are closely related. The designer should “factor out” common members and place them in a base class. Declaring a derived class does not affect its base class’s source code. Inheritance preserves the in­tegrity of the base class.

27 Programming with Inheritance Designers of object-oriented systems should avoid class proliferation. Such proliferation creates management problems and can hinder software reusability, because in a huge class library it becomes difficult for a client to locate the most appropriate classes. If derived classes are larger than they need to be (i.e., contain too much functionality), memory and pro­cessing resources might be wasted. Extend the base class containing the functionality that is closest to what is needed.

28 Class object All classes inherit directly or indirectly from the object class.