Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © 1992-2011 by Pearson Education, Inc. All Rights Reserved.

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Advertisements

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.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Dale Roberts Object Oriented Programming using Java - Inheritance Overview Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Derived Classes in C++CS-2303, C-Term Derived Classes in C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
(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.
Object-Oriented Programming: Inheritance Deitel &Deitel Java SE 8.
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.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
 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.
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: Inheritance
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
 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.
Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.
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.
 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.
Lecture 9 Polymorphism Richard Gesick.
Module 7: Object-Oriented Programming in Visual Basic .NET
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2 Base Classes and Derived.
More on Object-Oriented Programming: Inheritance 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Inheritance.
Object Oriented Programming
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.
Topics Inheritance introduction
© 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.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
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.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
 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 and Polymorphism
Object-Oriented Programming: Classes and Objects
Object-Oriented Programming: Inheritance
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object-Oriented Programming: Inheritance
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.
Classes & Objects A deeper Look Chapter 10 & 11
Object-Oriented Programming: Inheritance
Presentation transcript:

Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.

Introduction This chapter continues our discussion of object-oriented programming by introducing inheritance-, 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. © by Pearson Education, Inc. All Rights Reserved.

Introduction 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. A derived class can add its own instance variables, Shared variables, properties and methods, and it can customize methods and properties it inherits. © by Pearson Education, Inc. All Rights Reserved.

Introduction Therefore, a derived class is more specific than its base class and represents a more specialized group of objects. We explain and demonstrate polymorphism, which enables you to conveniently program “in the general” rather than “in the specific.” As we send method calls in this general way, the specific objects “do the right thing.” You’ll see that polymorphism simplifies programming with classes and makes it easy to extend systems with new capabilities. © by Pearson Education, Inc. All Rights Reserved.

Base Classes and Derived Classes 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. Figure 10.1 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) © by Pearson Education, Inc. All Rights Reserved.

Base Classes and Derived Classes Base Classes and Derived Classes

© by Pearson Education, Inc. All Rights Reserved. Example: CommunityMember Inheritance Hierarchy Example: CommunityMember Inheritance Hierarchy

Base Classes and Derived Classes 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. © by Pearson Education, Inc. All Rights Reserved.

Declaration of Base Class and Derived Class Public Class BaseClass End Class Public Class DerivedClass Inherits BaseClass End Class © by Pearson Education, Inc. All Rights Reserved.

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 Example © by Pearson Education, Inc. All Rights Reserved.

Derived Class Student inherits the methods of Base Class Person.  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. Constructer © by Pearson Education, Inc. All Rights Reserved.

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. Private member © by Pearson Education, Inc. All Rights Reserved.

If you want to override (redefine) method in the base class to be customize in the derived class, you must declare it by the keyword Overridable – Ex. Overridable Sub show() The sub which overrides the method of the base class must have the keyword Overrides – Ex. Override Sub show() Overriding © by Pearson Education, Inc. All Rights Reserved. Class BaseClass Overridable Sub show() console.writeline(“Base Class hi") End Sub End Class Class DerivedClass Inherets BaseClass Override Sub show() console.writeline(“Derived Class hi") End Sub End Class