1 / 11

Inheritance & Interface

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.

litteral
Télécharger la présentation

Inheritance & Interface

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance & Interface

  2. 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)

  3. 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

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

  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. 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. 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. 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. 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. 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. Interface Inheritance • Interface can inherit from other interfaces Public Interface ITest Inherits IComparable, ICloneable ReadOnly Property textValue() As String End Interface • Example InterfaceExample1

More Related