Chapter 2 11/18/2015 © 1992-2011 by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.

Slides:



Advertisements
Similar presentations
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Advertisements

Advanced Object Oriented Concepts Tonga Institute of Higher Education.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Chapter 11: Implementing Inheritance and Association Visual Basic.NET Programming: From Problem Analysis to Program Design.
(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.
More about classes and objects Classes in Visual Basic.NET.
CS-2135 Object Oriented Programming
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Computer Science I Inheritance 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 in Visual Basic.NET. Overview Defining Classes Creating and Destroying Objects Inheritance Interfaces Working with Classes.
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.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Inheritance using Java
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
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.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Chapter 8 More Object Concepts
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Module 7: Object-Oriented Programming in Visual Basic .NET
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Classes and Objects noun A word used to denote or name a person, place, thing, quality, or act. verb That part of speech that expresses existence,
Programming Pillars Introduction to Object- Oriented Programming.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2 Base Classes and Derived.
1 Classes and Controls CE-105 Spring 2007 By: Engr. Faisal ur Rehman.
More on Object-Oriented Programming: Inheritance 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
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,
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Coming up: Inheritance
Topics Inheritance introduction
Object-Oriented Programming: Inheritance and Polymorphism.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
These materials where developed by Martin Schray
Object-Oriented Programming: Classes and Objects
Microsoft Visual Basic 2005: Reloaded Second Edition
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object-Oriented Programming: Classes and Objects
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Chapter 11 – Object-Oriented Programming
Chapter 9 Object-Oriented Programming: Inheritance
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
IS437: Fall 2004 Instructor: Dr. Boris Jukic
Advanced Programming Behnam Hatami Fall 2017.
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Fundaments of Game Design
Classes & Objects A deeper Look Chapter 10 & 11
Object-Oriented Programming: Inheritance
Presentation transcript:

Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011

 Inheritance is a form of software reuse in which a new class is created quickly and easily by absorbing an existing class’s members and customizing them with new or modified capabilities.  With inheritance, you can save time during program development and build better software by reusing proven, high-quality classes. 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2012

 When creating a class, rather than declaring completely new members, you can designate that the new class inherits the members of an existing class.  The existing class is called the base class, and the new class is the derived class. 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2013

 A derived class can add its own instance variables, Shared variables, properties and methods, and it can customize methods and properties it inherits.  Therefore, a derived class is more specific than its base class and represents a more specialized group of objects. 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2014

 Inheritance enables an is-a relationship.  In an is-a relationship, an object of a derived class also can be treated as an object of its base class.  For example, a car is a vehicle.  The next slide lists several simple examples of base classes and derived classes—base classes tend to be more general and derived classes tend to be more specific.  Base-class objects cannot be treated as objects of their derived classes—although all cars are vehicles, not all vehicles are cars (the other vehicles could be trucks, planes or bicycles, for example) 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2015

11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2016

11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2017

 Each arrow in the inheritance hierarchy represents an is-a relationship.  As we follow the arrows upward in this class hierarchy, we can state, for instance, that “an Employee is a CommunityMember ” and “a Teacher is a Faculty member.”  A direct base class is the class from which a derived class explicitly inherits.  An indirect base class is inherited from two or more levels up in the class hierarchy. 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2018

Public Class BaseClass End Class Public Class DerivedClass Inherits BaseClass End Class 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2019

Derived ClassBase Class Class Student Inherits Person Private m_ClassGroup As String P ublic Sub New(ByVal N As String, ByVal S As Integer, ByVal G As String ) MyBase.New(N, S) m_ClassGroup = G End Sub Public Property ClassGroup() As String Get ClassGroup = m_ClassGroup End Get Set(ByVal value As String) m_ClassGroup = ClassGroup End Set End Property End Class Class Person Private m_Name As String Private SSN As Integer P ublic Sub New(ByVal N As String, ByVal S As Integer) m_Name = N SSN = S End Sub Public Property Name() As String Get Name = m_Name End Get Set(ByVal value As String) m_Name = value End Set End Property End Class 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 20110

 By default, all classes are inheritable unless marked with the NotInheritable keyword.  Visual Basic allows only single inheritance in classes; that is, derived classes can have only one base class.  To prevent exposing restricted items in a base class, the access type of a derived class must be equal to or more restrictive than its base class. 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 20111

 Visual Basic introduces the following class-level statements and modifiers to support inheritance: ◦ Inherits statement — Specifies the base class. ◦ NotInheritable modifier — Prevents programmers from using the class as a base class. ◦ MustInherit modifier — Specifies that the class is intended for use as a base class only. Instances of MustInherit classes cannot be created directly; they can only be created as base class instances of a derived class. 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 20112

 Constructors are not inherited, so class Student does not inherit class Person ’s constructor.  In fact, the first task of any derived-class constructor is to call its direct base class’s constructor to ensure that the instance variables declared in the base class are initialized properly.  Ex. MyBase.New(N, S)  If the code does not include call to the base-class constructor, Visual Basic implicitly calls the base class’s default or parameterless constructor. 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 20113

 In inheritance, Public members of the base class become Public members of the derived class.  A base class’s Private members are not inherited by its derived classes.  Derived-class methods can refer to Public members inherited from the base class simply by using the member names.  Derived-class methods cannot directly access Private members of their base class.  A derived class can change the state of Private base-class instance variables only through Public methods provided in the base class and inherited by the derived class. 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 20114

 By default, a derived class inherits properties and methods from its base class.  If an inherited property or method has to behave differently in the derived class it can be overridden.  Overriding means define a new implementation of the method in the derived class. 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 20115

 The following modifiers are used to control how properties and methods are overridden: ◦ Overridable — Allows a property or method in a class to be overridden in a derived class. ◦ Overrides — Overrides an Overridable property or method defined in the base class. ◦ NotOverridable — Prevents a property or method from being overridden in an inheriting class. By default, Public methods are NotOverridable. ◦ MustOverride — Requires that a derived class override the property or method. When the MustOverride keyword is used, the method definition consists of just the Sub, Function, or Property statement. MustOverride methods must be declared in MustInherit classes. 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 20116

 The MyBase keyword behaves like an object variable that refers to the base class of the current instance of a class.  MyBase is frequently used to access base class members that are overridden or shadowed in a derived class. 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 20117

 The following list describes restrictions on using MyBase: ◦ MyBase refers to the immediate base class and its inherited members. It cannot be used to access Private members in the class. ◦ MyBase is a keyword, not a real object. ◦ The method that MyBase qualifies does not have to be defined in the immediate base class. ◦ You cannot use MyBase to call MustOverride base class methods. 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 20118

11/18/ © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 201 Output #1 Parent P1 Child C1

11/18/ © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 201 Output #2 Hello from SubClass

11/18/ © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 201 Output #3 I am a manager I am an employee. Managing... I am a manager

11/18/ © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 201 Output #4 Abstarct method #1 Abstarct method #2