Unit III B: Constructors, Destructor, Inheritance

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

Unit III b

Constructors,Destructor,Inheritance
Constructors and Destructors
• Constructor:
• A constructor is a special member function
whose task is to initialize objects
• A constructor is created when object is called.
• Constructor never returns a value
• We create a constructor by adding
”Sub New()” to class
Default constructor:
The default constructor doesn’t have any
parameters.
Syntax:
Sub New()
‘Statements
End Sub
Example
Public Class Emp
Dim bonus, salary As Integer

Public Sub New()


salary = 20000
bonus = 5000
Console.WriteLine("Salary:" & salary & "bonus:" & bonus)

End Sub
End Class

Module Module1

Sub Main()
Dim obj As New Emp

Console.ReadKey()
End Sub

End Module
Output
Parameterised constructor:

• It accepts input through arguments


• Constructor accepts 1 or more parameters
• Syntax:
Sub New(ByVal variable_name as Datatype)
Statements
End Sub
Example
Public Class Emp
Dim bonus, salary As Integer

Public Sub New(ByVal s As Integer, ByVal b As Integer)


salary = s
bonus = b
Console.WriteLine("Salary:" & salary & "bonus:" & bonus)

End Sub
End Class
Module Module1

Sub Main()
Dim obj As New Emp(30000, 5000)

Console.ReadKey()
End Sub

End Module
Overloaded Constructor:

Defining more than one constructors with


different parameters is known as overloading
of constructors.
Public Class Emp
Example
Dim bonus, salary As Integer
Public Sub New()
salary = 20000
bonus = 5000
Console.WriteLine("Salary:" & salary & "bonus:" & bonus)
End Sub

Public Sub New(ByVal s As Integer, ByVal b As Integer)


salary = s
bonus = b
Console.WriteLine("Salary:" & salary & "bonus:" & bonus)

End Sub
End Class
Module Module1

Sub Main()
Dim ob As New Emp()
Dim obj As New Emp(30000, 5000)

Console.ReadKey()
End Sub

End Module
Output
Destructor
• Destructors are used to deallocate the memory
allocated to objects
• Object class includes the finalize method which
gets called to remove the object
• Finalize is also used to release the objects or
database connection.
• When .Net runtime determines that the object is
no longer needed then finalize method is
automatically called by garbage collector.
Syntax
Protected Overrides Sub finalize()
‘statements
End Sub
Destructor
Public Class A
Public Sub New()
Console.WriteLine(“Constructor is executed")

End Sub

Protected Overrides Sub Finalize()


Console.WriteLine("Destructor is executed")

End Sub
End Class

Module Module1

Sub Main()
Dim obj As New A()

Console.ReadKey()
End Sub

End Module
Inheritance
• An important feature of OOP is reusability
• The original class is base ,parent or super class
• While inherits the functionality of base class and extends it in own way is
called sub, child or inherited class
• “inherits “ keyword is used by child class.
Syntax:
Public Class A
[fields,methods]
End Class

Public Class B
inherits Class A
[fields,methods]
End Class
Types of inheritance
• Single Inheritance
• Multilevel Inheritance
• Hierarchical Inheritance
• Hybrid Inheritance
• Multiple Inheritance
Single inheritance
• When a single derived class is created from a
single base class then inheritance is called
single inheritance.

Person

Teacher
Public Class Person
Dim name1 As String
Dim age As Integer
Public Sub New()
name1 = "ABC"
age = 20
End Sub
Public Sub info() Module Module1
Console.WriteLine("Name:" & name1 & "Age:" & age) Sub Main()
End Sub Dim t1 As New Teacher()
End Class
t1.show()
Public Class Teacher
Inherits Person
Console.ReadKey()
Dim subject, qualification As String
End Sub
Public Sub New()
qualification = "B.E"
subject = "Eng" End Module
End Sub
Public Sub show()
info()
Console.WriteLine("qualification:" & qualification & "subject:" & subject)
End Sub
End Class
Output
Multilevel inheritance
When derived class is created from another
derived class that inheritance is called
multilevel inheritance
Student

Sports

Result
Public Class Student Public Class Result
Public hindi, eng, marathi, tot, avg, Inherits Sports
sp As Integer Public Sub cal()
Public Sub getmarks() tot = hindi + marathi + eng + sp
hindi = InputBox("Enter marks of avg = tot / 4
hindi") MsgBox("Total marks:" & tot & "avg"
eng = InputBox("Enter marks for & avg)
english") End Sub
marathi = InputBox("enter marks End Class
for marathi")
End Sub Module Module1
End Class
Sub Main()
Public Class Sports Dim r1 As New Result()
Inherits Student r1.getmarks()
r1.get_sports()
Public Sub get_sports() r1.cal()
sp = InputBox("Enter sprots
marks")
End Sub Console.ReadKey()
End Class End Sub

End Module
Output
Hierarchical inheritance
• When more than one derived class is created
from a single base class ,then it is called
hierarchical inheritance

Vehicle

Car Bike
Public Class Vehicle
Public no_wheels As Integer
End Class

Public Class Car


Inherits Vehicle
Public Sub show()
no_wheels = 4
Console.WriteLine("No of wheels of car:" & no_wheels)
End Sub
End Class

Public Class Bike


Inherits Vehicle
Public Sub disp()
no_wheels = 2
Console.WriteLine("No of wheels of car:" & no_wheels)
End Sub
End Class

Module Module1
Sub Main()
Dim c As New Car
c.show()
Dim b As New Bike
b.disp()

Console.ReadKey()
End Sub
End Module
Output
Hybrid Inheritance
• Any combination of single, hierarchical and
multi level inheritance is called Hybrid
inheritance
College

Branch

SY TY
Public Class College
Public n As String = "GP" Module Module1
End Class

Public Class Branch


Sub Main()
Inherits College Dim s1 As New sy()
Public b As String = "CM" Dim t1 As New ty()
End Class s1.disp()
t1.show()
Public Class sy
Inherits Branch Console.ReadKey()
Public Sub disp() End Sub
Console.WriteLine("Second year student from " & b & " " & n & "College")
End Sub
End Module
End Class

Public Class ty
Inherits Branch
Public Sub show()
Console.WriteLine("Third year student from " & b & " " & n & "College")
End Sub

End Class
Output
Multiple inheritance
• In this type of inheritance a child is inherited
from more than 1 base class.
• Multiple inheritance cannot be supported
using only classes but it can be implemented
by using interface
Interface
• Interface is just like a class but in which only
declaration of method is allowed
• We cannot define a method in interface
• All methods declared in a an interface must
defined in the child class
• ‘Implements ‘ keyword is used to inherit a
interface
Syntax
Interface interfacename
interface body (method declaration)
End interface

Class Classname
implements interfacename
[interface defination] Implements interfacename.method_name

End Class
Multiple Inheritance using interface

Area Rectangle

Perimeter
Interface area
Sub cal_area()
End Interface

Public Class perimeter


Public Sub cal_peri()
Console.WriteLine("Perimeter of rectangle")
End Sub
End Class

Public Class rectangle


Inherits perimeter
Implements area
Public Sub cal_area() Implements area.cal_area
Console.WriteLine("Area of rectangle")
End Sub
End Class

Module Module1
Sub Main()
Dim obj As New rectangle
obj.cal_area()
obj.cal_peri()
Console.ReadKey()
End Sub
End Module
Output

You might also like