Inheritance Interface 1 Why Inheritance Employee Customer Common

  • Slides: 11
Download presentation
Inheritance & Interface 1

Inheritance & Interface 1

Why Inheritance? Employee Customer • Common properties? • Methods to send congratulations to Employees

Why Inheritance? Employee Customer • Common properties? • Methods to send congratulations to Employees and Customers Public Sub Send. Congratulations. E(By. Val employees() As Employee … End Sub Public Sub Send. Congratulations. C(By. Val customers() As Customer … End Sub • Example Inheritance. Example 1 • 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) 2

Simple Inheritance Person Employee Customer Public Class Person. . . End Class Public Class

Simple Inheritance Person Employee Customer Public Class Person. . . End Class Public Class Customer Inherits Person. . . End Class Public Class Employee Inherits Person. . . End Class ‘ To send congratulations Public Sub Send. Congratulation(By. Val persons() As Person). . . End Sub 3

Inheritance Issues • Visibility – Public, Protected • Keyword My. Base – i. e.

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

Overriding (Polymorphism) (1) • Derived classes may have different behavior with their base classes

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 Get. Montly. Pay() As Decimal Return m_salary / 12 End Function End Class Public Class Manager Inherits Employee. . . Public Overrides Function Get. Montly. Pay() As Decimal Return My. Base. Get. Montly. Pay() + m_bonus End Function End Class 5

Overriding (Polymorphism) (2) ‘ Calling methods, depends on the Type of the object Dim

Overriding (Polymorphism) (2) ‘ Calling methods, depends on the Type of the object Dim e As Employee, m As Manager … e=m d = m. Get. Montly. Pay() ‘ call Manager’s d = e. Get. Montly. Pay() ‘ call Manager’s too! Dim e 2 As Employee … d = e. Get. Montly. Pay() ‘ call Employee’s • Stepping through Inheritance. Example 2 6

Abstract Classes • • Abstract classes are classes with Abstract methods (with definition but

Abstract Classes • • Abstract classes are classes with Abstract methods (with definition but no implementation) Abstract classes cannot be instantiated Public Must. Inherit Class Account Protected m_balance As Decimal. . . Public Must. Override Sub Close. Month() End Class ‘ abstract class ‘ abstract method Public Class Savings. Account Inherits Account. . . Public Overrides Sub Close. Month() ‘ 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 7

Interface (1) • A definition for certain behavior. This behavior is implemented by any

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 Compare. To(Byval o as Object) As Integer End Interface 8

Interface (2) Public Class Test Implements IComparable … Function Compare. To(By. Val o As

Interface (2) Public Class Test Implements IComparable … Function Compare. To(By. Val o As Object) As Integer _ Implements IComparable. Compare. To Dim t As Test = CType(o, Test) If m_test. No = t. m_test. No Then Return 0 If m_test. No < t. m_test. No Then Return -1 Return 1 End Function End Class 9

Interfaces Vs Inheritance • A class can inherit from one class only (single inheritance),

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 10

Interface Inheritance • Interface can inherit from other interfaces Public Interface ITest Inherits IComparable,

Interface Inheritance • Interface can inherit from other interfaces Public Interface ITest Inherits IComparable, ICloneable Read. Only Property text. Value() As String End Interface • Example Interface. Example 1 11