topik 4.2 inheritance polymorphism
TRANSCRIPT
-
7/30/2019 Topik 4.2 Inheritance Polymorphism
1/18
F5227VISUAL BASIC .NET PROGRAMMING
Topic 4.2 :
Apply Inheritance And Polymorphism
-
7/30/2019 Topik 4.2 Inheritance Polymorphism
2/18
Course Learning Outcome (CLO) Upon completion of this course, students should be able to :
1. Create a simpe VB.NET based application based on the
Windows Application template.
2. Describe on essential terminology including memory,data types and graphical user interface.
3. Apply object oriented programming techniques to
create classes, add methods and add properties.
4. create a simple VB.NET
based Web forms applicationthat uses an XML Web Service and manipulate data in
database by using Microsoft ADO.NET.
-
7/30/2019 Topik 4.2 Inheritance Polymorphism
3/18
Course Learning Outcome:
Topic 4.0
1. Apply object oriented programming techniques to
create classes, add methods and add properties. (CLO3)
-
7/30/2019 Topik 4.2 Inheritance Polymorphism
4/18
Topic 4.0
Topic 4.1 : Use Classes, Object and Methods
Topic 4.2 : Apply Inheritance And
Polymorphism
Topic 4.3 : Construct Program Using String
Methods
Topic 4.4 : Understand the concepts ofException Handling
-
7/30/2019 Topik 4.2 Inheritance Polymorphism
5/18
Inheritance
A key feature of OOP is reusability.
It's always time saving and useful if we can
reuse something that already exists rather
than trying to create the same thing again and
again.
Reusing the class that is tested, debugged and
used many times can save us time and effort
of developing and testing it again
-
7/30/2019 Topik 4.2 Inheritance Polymorphism
6/18
The old class is called the base class and the
new class is called derived class.
The derived class inherits some or everything
of the base class.
In Visual Basic we use the Inherits keyword to
inherit one class from other.
-
7/30/2019 Topik 4.2 Inheritance Polymorphism
7/18
Once a class has been written and tested, it
can be used by other programs to suit the
program's requirement.
This is done by creating a new class from an
existing class.
The process of deriving a new class from an
existing class is called Inheritance.
-
7/30/2019 Topik 4.2 Inheritance Polymorphism
8/18
Inheritance
Public Class One
---
---
End Class
Public Class Two
Inherits One---
---
End Class
-
7/30/2019 Topik 4.2 Inheritance Polymorphism
9/18
Inheritance
Using Inheritance we can use the variables,
methods, properties, etc, from the base class
and add more functionality to it in the derived
class.
-
7/30/2019 Topik 4.2 Inheritance Polymorphism
10/18
Imports System.Console
Module Module1
Sub Main()
Dim ss As New Two()
WriteLine(ss.sum())Read()
End Sub
End Module
Public Class One
'base class
Public i As Integer = 10Public j As Integer = 20
Public Function add() As Integer
Return i + j
End Function
End Class
Public Class Two
Inherits One
'derived class. class two inherited from class one
Public k As Integer = 100
Public Function sum() As Integer
'using the variables, function from base class and adding more functionality
Return i + j + kEnd Function
-
7/30/2019 Topik 4.2 Inheritance Polymorphism
11/18
Output
-
7/30/2019 Topik 4.2 Inheritance Polymorphism
12/18
Polymorphism
MSDN (Microsoft Developer Network)
explanation,
"Polymorphism is the ability for classes to
provide different implementations of methods
that are called by the same name.
Polymorphism allows a method of a class to be
called without regard to what specificimplementation it provides."
http://www.programmersheaven.com/2/Les_VBNET_5_p4http://www.programmersheaven.com/2/Les_VBNET_5_p4 -
7/30/2019 Topik 4.2 Inheritance Polymorphism
13/18
Polymorphism
Polymorphism is one of the crucial features of
OOP. It means "one name, multiple forms".
It is also called as Overloading which means
the use of same thing for different purposes.
-
7/30/2019 Topik 4.2 Inheritance Polymorphism
14/18
Polymorphism
Using Polymorphism we can create as many
functions we want with one function name
but with different argument list.
-
7/30/2019 Topik 4.2 Inheritance Polymorphism
15/18
Polymorphism
The function performs different operations
based on the argument list in the function call.
The exact function to be invoked will be
determined by checking the type and number
of arguments in the function.
-
7/30/2019 Topik 4.2 Inheritance Polymorphism
16/18
ExampleModule Module1Sub Main()
Dim two As New One()
WriteLine(two.add(10))
'calls the function with one argument
WriteLine(two.add(10, 20))
'calls the function with two arguments
WriteLine(two.add(10, 20, 30))
'calls the function with three arguments
Read()
End Sub
End Module
Public Class One
Public i, j, k As Integer
Public Function add(ByVal i As Integer) As Integer
'function with one argument
Return i
End Function
Public Function add(ByVal i As Integer, ByVal j As Integer) As Integer
'function with two arguments
Return i + j
End Function
Public Function add(ByVal i As Integer, ByVal j As Integer, ByVal k As Integer) As Integer
'function with three arguments
Return i + j + k
End Function
End Class
-
7/30/2019 Topik 4.2 Inheritance Polymorphism
17/18
Output
-
7/30/2019 Topik 4.2 Inheritance Polymorphism
18/18
Overriding the method
The Overridable and the Overrides keywords
Overridable allow a method to be overridden
and be used polymorphically.
Overrides is overriding the virtual method in
the base class.