Inheritance Chapter 14. What is inheritance?  The ability to create a class from another class, the “parent” class, extending the functionality and state.

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

Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
OOP / VB.NET (Chp4) DR. JOHN ABRAHAM UTPA Credit: Partial slides from startvbdotnet.com.
Chapter 11: Implementing Inheritance and Association Visual Basic.NET Programming: From Problem Analysis to Program Design.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
12-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter 10: Introduction to Inheritance
More about classes and objects Classes in Visual Basic.NET.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
1 Inheritance & Interface. 2 Why Inheritance? Common properties? Methods to send congratulations to Employees and Customers Public Sub SendCongratulationsE(ByVal.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Advanced Object-Oriented Programming Features
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
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
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance using Java
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Module 7: Object-Oriented Programming in Visual Basic .NET
An Object-Oriented Approach to Programming Logic and Design
Intro to OOP with Java, C. Thomas Wu
Programming Pillars Introduction to Object- Oriented Programming.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Inheritance Building one object from another. Background Object-oriented programming is normally described has offering three capabilities Encapsulation:
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
CSCI 3327 Visual Basic Chapter 3: Classes and Objects UTPA – Fall 2011.
1 Working with Objects. 2 Defining a Class in VB.NET A class is a user-defined data type You can declare it as part of a module, but usually in a separate.
John Byrne Inheritance Bank account examples. John Byrne Example account hierarchy F account relationships: ACCOUNT SAVINGSCHEQUE TIME_ACCT.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge
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.
© 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.
Class Inheritance SWE 344 Internet Protocols & Client Server Programming.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Object-Oriented Programming: Inheritance and Polymorphism.
Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Section 3: Component/Object Oriented Programming, Events, and Delegates in.NET Jim Fiddelke, Greenbrier & Russel.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
These materials where developed by Martin Schray
Lecture 12 Inheritance.
One class is an extension of another.
Microsoft Visual Basic 2005: Reloaded Second Edition
Inheritance AKEEL AHMED.
Object-Oriented Programming: Classes and Objects
Object-Oriented Programming: Inheritance
One class is an extension of another.
Object-Oriented Programming: Inheritance
IS437: Fall 2004 Instructor: Dr. Boris Jukic
Java Programming, Second Edition
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Inheritance.
Object-Oriented Programming: Inheritance
Presentation transcript:

Inheritance Chapter 14

What is inheritance?  The ability to create a class from another class, the “parent” class, extending the functionality and state of the parent in the derived, or child”, class.  It allows derived classes to overload methods from their parent class.

Inheritance and Object-Orientation  Inheritance is one the pillars of object-orientation.  It is the mechanism of designing one class from another.  One of the idea for code reusability, supporting the concept of hierarchal class

Classes in Visual Basic In visual basic you can:  create classes from scratch.  Create classes using some or all properties of an existing class.

Characteristics of Inheritance  A derived class extends its basic class. That is, it contains the methods and data of its parent class, and it can also contain its own data members and methods.  The derived class cannot change the definition of an inherited member.  Constructors and destructors are not inherited. All other members of the base class are inherited.  The accessibility of a member in the derived class depends uponr its declared accessibility in the base class.  A derived class can override an inherited member.

Example:  Parent class: bank account. Generic debit and credit methods.  Savings account: child class. Will over write some of the parent’s methods.

 Create new console application in visual studio: HelloInheritance.vb  Add a class to the project: Account.vb

Keywords in the Base Class MustInherit  Means that this class is an abstract class.  We cannot use this class to creates any objects.  To create objects, we have to use classes that inherit from account class. Private – As - ‘only accessible in base class Protected – As - ‘accessible in base class and derived class Overridable  Means this method can be overridden in derived classes.

Keywords in the derived Classes Public Class ChildClass Inherits BaseClass  That means ChildClass inherits from the base class BaseClass. Overrides  Means this method is inherited from the base class and overrides to fit the class functionality.

Constructors of the derived classes The constructor of the derived class must contain:  a statement that calls the base class constructor.  An initialization of the additional class fields.  Example: Public Sub New (ByVal Nm As String, ByVal Bal As Double, ByVal Min As Double) MyBase.New (Nm, Bal) MinBalance = Min End Sub calls the base class constructor initialization of the additional class fields

Account.vb Public MustInherit Class Account Private Name As String Protected balance As Double Public Sub New (ByVal Nm As String, ByVal Bal As Double) Name = Nm Balance = Bal End Sub Public Overridable Sub Credit (ByVal Amount As Double) Balance += Amount End Sub Public Overridable Sub Debit (ByVal Amount As Double) Balance -= Amount End Sub Public Overridable Sub Display () Console.WriteLine(“Name: “ + Name + “, “ + “Balance = “ + Balance) End Sub Public Sub ChangeName (ByVal newName As String) Name = newName End Sub End Class

SavingsAccount.vb Public Class SavingsAccount Inherits Account Private MainBalance As Double Public Sub New (ByVal Nm As String, ByVal Bal As Double, ByVal Min As Double) MyBase.New (Nm, Bal) MinBalance = Min End Sub Public Overrides Sub Debit (ByVal Amount As Double) If Amount <= Balance Then MyBAse.Debit(Amount) End If End Sub Public Overrides Sub Display() MyBase.Display() Console.WriteLine(“$5 Charge if balance goes below $” + MinBalance) End Sub Public Overrides Function CalculateBankCharge () As Double If Balance < MinBalance Then Return 5.0 Else Return 0.0 End If End Function End Class

Module1.vb Module Module1 Sub Main() Dim sa As SavingsAccount = New SavingsAccount(“Freda Smith”, 100.0, 25) sa.Display() sa.Credit(100) sa.Debit(180) sa.ChangeName(“Freda Jones”) sa.Display() Console.WriteLine (“Bank charge: “ + sa.CalculateBankCharge()) Console.Readline() End Sub End Module

End of the Course Best Wishes, Yara