110 likes | 124 Views
This article explains the benefits of using inheritance and interfaces in object-oriented programming, with examples and discussions on common issues. It covers topics such as code reuse, method overriding, abstract classes, and the difference between inheritance and interface implementation.
E N D
Why Inheritance? Employee Customer • 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)
Person Employee Customer Simple Inheritance 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
Inheritance Issues • Visibility • Public, Protected • Keyword MyBase • i.e. super in Java • MyBase.New() • Multi-level Inheritance
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
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
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
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
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
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
Interface Inheritance • Interface can inherit from other interfaces Public Interface ITest Inherits IComparable, ICloneable ReadOnly Property textValue() As String End Interface • Example InterfaceExample1