Download presentation
Presentation is loading. Please wait.
Published byAndrea Stevenson Modified over 9 years ago
1
Inheritance Chapter 14
2
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.
3
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
4
Classes in Visual Basic In visual basic you can: create classes from scratch. Create classes using some or all properties of an existing class.
5
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.
6
Example: Parent class: bank account. Generic debit and credit methods. Savings account: child class. Will over write some of the parent’s methods.
7
Create new console application in visual studio: HelloInheritance.vb Add a class to the project: Account.vb
8
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.
9
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.
10
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
11
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
12
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
13
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
14
End of the Course Best Wishes, Yara
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.