Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Inheritance & Interface. 2 Why Inheritance? Common properties? Methods to send congratulations to Employees and Customers Public Sub SendCongratulationsE(ByVal.

Similar presentations


Presentation on theme: "1 Inheritance & Interface. 2 Why Inheritance? Common properties? Methods to send congratulations to Employees and Customers Public Sub SendCongratulationsE(ByVal."— Presentation transcript:

1 1 Inheritance & Interface

2 2 Why Inheritance? Common properties? Methods to send congratulations to Employees and Customers Public Sub SendCongratulationsE(ByVal employees() As Employee … End Sub Public Sub SendCongratulationsC(ByVal customers() As Customer … End Sub Example InheritanceExample1 Cannot reuse the same procedure Inheritance: –Sharing common code and properties –The process of extending the functionality of a class by adding new properties and methods and creating in this way a new class (the Derived class) EmployeeCustomer

3 3 Simple Inheritance EmployeeCustomer Person Public Class Person... End Class Public Class Customer Inherits Person... End Class Public Class Employee Inherits Person... End Class ‘ To send congratulations Public Sub SendCongratulation(ByVal persons() As Person)... End Sub

4 4 Inheritance Issues Visibility –Public, Protected Keyword MyBase –i.e. super in Java –MyBase.New() Multi-level Inheritance

5 5 Overriding (Polymorphism) (1) Derived classes may have different behavior with their base classes for the same method Public Class Employee Inherits Person... Public Overridable Function GetMontlyPay() As Decimal Return m_salary / 12 End Function End Class Public Class Manager Inherits Employee... Public Overrides Function GetMontlyPay() As Decimal Return MyBase.GetMontlyPay() + m_bonus End Function End Class

6 6 Overriding (Polymorphism) (2) ‘ Calling methods, depends on the Type of the object Dim e As Employee, m As Manager … e = m d = m.GetMontlyPay()‘ call Manager’s d = e.GetMontlyPay()‘ call Manager’s too! Dim e2 As Employee … d = e.GetMontlyPay()‘ call Employee’s Stepping through InheritanceExample2

7 7 Abstract Classes Abstract classes are classes with Abstract methods (with definition but no implementation) Abstract classes cannot be instantiated Public MustInherit Class Account‘ abstract class Protected m_balance As Decimal... Public MustOverride Sub CloseMonth()‘ abstract method End Class Public Class SavingsAccount Inherits Account... Public Overrides Sub CloseMonth()‘ provide actual implementation If m_balance < 0 Then ' If the balance is negative, apply a charge of 25 dollars m_balance -= 25 Else ' apply an interest of whatever the interest ' stored in the s_interest value m_balance *= 1 + s_interest End If End Sub End Class

8 8 Interface (1) A definition for certain behavior. This behavior is implemented by any class that implements the interface. Act as a Contract, the class implementing the interfaces “promises” to provide the implements Similar to classes, can have members properties and methods, but NOT fields All properties and methods must be abstract No modifiers for the properties and methods are allowed A class can implement multiple interfaces Public Interface IComparable Function CompareTo(Byval o as Object) As Integer End Interface

9 9 Interface (2) Public Class Test Implements IComparable … Function CompareTo(ByVal o As Object) As Integer _ Implements IComparable.CompareTo Dim t As Test = CType(o, Test) If m_testNo = t.m_testNo Then Return 0 If m_testNo < t.m_testNo Then Return -1 Return 1 End Function End Class

10 10 Interfaces Vs Inheritance A class can inherit from one class only (single inheritance), but it can implement any number of interfaces An interface is different from a base class: An interface defines behavior only, whereas a base class can hold state (have fields), define, and implement behavior A base class defines what a derived class is; whereas an implemented interface defines how the object behaves in certain circumstances An object is not said to be an IComparable, but it is said to be comparable, it can behave as described by the interface, but is an instance of the base class

11 11 Interface Inheritance Interface can inherit from other interfaces Public Interface ITest Inherits IComparable, ICloneable ReadOnly Property textValue() As String End Interface Example InterfaceExample1


Download ppt "1 Inheritance & Interface. 2 Why Inheritance? Common properties? Methods to send congratulations to Employees and Customers Public Sub SendCongratulationsE(ByVal."

Similar presentations


Ads by Google