Showing posts with label Object Oriented Programming. Show all posts
Showing posts with label Object Oriented Programming. Show all posts

Tuesday, April 30, 2013

Overloading Object oriented programming का एक महत्वपूर्ण feature है जो की किसी procedure के multiple versions बनाने और उन्हे अलग अलग tasks के लिए use करने के सुविधा प्रदान करता है। overloading मे procedure का नाम same होता है इसमे केवल prameteres मे difference होता है जो की उसी procedure को एक ही नाम से अलग अलग प्रकार से task perform करने मे हेल्प करता है। इसका उदेश्य किसी procedure के अलग अलग versions बनाना है जिनके नाम एक ही रहते है। 


Overloading Rules: 


         Overloading करते समय निम्न बातों को ध्यान मे रखना चाहिए। 



  • Same Name: सभी overloaded procedures के नाम same होने चाहिए।


  • Different Signature: सभी overloaded procedures मे निम्न मे से कम से कम एक difference होना चाहिए तभी procedures को overload किया जा सकता है-




  1. Number of Parameters


  2. Order of Parameters


  3. Data Types of Parameters


  4. Number of Type Parameters


  5. Return Type



Ex:



Public Class Shape



Public Function Area(ByVal radius As Double) As Double



Return Math.PI * radius * radius



End Function



Public Function Area(ByVal length As Double, ByVal width As Double) As Double



Return length * width



End Function



Public Function Area(ByVal a As Integer, ByVal b As Integer, ByVal c As Integer) As Double



Dim s As Integer



s = (a + b + c) / 2



Return (Math.Sqrt(s * (s - a) * (s - b) * (s - c)))



End Function



End Class

 Mybase की तरह की यह keyword किसी method को override करने पर derived class की overrided method को कॉल करने के लिए use की जाती है। यह निश्चित कर देती है की call की गई method इसी derived class की overrided method है। 



Public Overrides Function Area() As Double



Return ((Math.PI * Radius * MyClass.length) + MyBase.Area())



End Function




Limitations of MyClass keyword: 



  • MyClass real object नहीं है इसीलिए इसके द्वारा variable मे value को assign, procedure मे pass etc. नहीं कर सकते हैं।


  • Myclass का use केवल उसी क्लास के members के लिए किया जा सकता है।


  • Myclass को standard modules मे use नहीं कर सकते हैं।


  • Myclass को ऐसे methods के साथ नहीं use किया जा सकता है जो की base class से override नहीं की जा सकती है।

Monday, April 29, 2013

 Mybase keyword inheritance मे base class की overrided method को call करने के लिए use की जाती है। जब inheritance करते समय कभी भी base class की overrided method की आवश्यकता पड़ती है तब mybase keyword का use कर use call कर सकते हैं। जैसे given example मे जब Circle के area calculate किया जाता है साथ ही जब Cone का area calculate करते है तब Circle class के area को override करने के आवश्यकता पड़ती है इसमे base class (Circle) के area method को कॉल करने के लिए mybase का use किया जाता है।
 
Circle.vb

Public Class Circle
    Private cradius As Double
    Public Property Radius() As Double
        Get
            radius = cradius
        End Get
        Set(ByVal value As Double)
            If value < 0 Then
                msgbox("Wrong Input:")
            Else
                cradius = value
            End If
        End Set
    End Property

    Public Overridable Function Area() As Double
        Return Math.PI * Radius ^ 2
    End Function

  
End Class
Cone.vb 
Public Class Cone
    Inherits Circle
    Dim l As Double
    Public Property length() As Double
        Get
            length = l
        End Get
        Set(ByVal value As Double)
            l = value
        End Set
    End Property
    Public Overrides Function Area() As Double
        Return ((Math.PI * Radius * length) + MyBase.Area())
    End Function

End Class


Limitations of MyBase keyword: 
  • Mybase keyword केवल immediate base class के लिए use की जा सकता है।
  • Mybase real object नहीं है इसीलिए इसके द्वारा variable मे value को assign, procedure मे pass etc. नहीं कर सकते हैं।
  • Mybase को MustOverride base class के लिए use नहीं कर सकते हैं।
  • Mybase base class के अंदर प्रयोग नहीं की जा सकती है।
  • Mybase को friend member के लिए use नहीं किया जा सकता है।

Inheritance Modifiers: 

         Inheritance के लिए Visual basic .Net कुछ class level modifiers provide करता है। 
  • NotInheritable: class को ininheritable बनाने के लिए। ऐसी class को inherite नहीं किया जा सकता हैं।
  • MustInherit: इसका use करने पर class को base class मे use करना अनिवार्य हो जाता है।   इसे केवल base class की तरह ही use किया जा सकता है।
Oerriding Properties and Methods in Derived Class:

         By default derived class base class की सभी properties और methods को inherite कर लेती है। कभी कभी Derived class मे inherite की गई property या method को अलग प्रकार से प्रयोग करने के आवश्यकता पड़ती है ऐसी अवसर पर इसे overriding करना कहलाता है। इसके लिए VB।Net Base class और Derived class दोनों में use होने वाली properties और methods के लिए कुछ modifiers प्रदान करता है जिनहे प्रयोग कर किसी भी method आदि की overriding को control कर सकते हैं। जो निम्नलिकीहित हैं। 

For Properties and methods of Base Class: 
  • Overridable: इसका use करने पर property या method को override किया जा सकता है।
  • NotOverridable: इसका use करने पर property या method को override नहीं किया जा सकता है।
  • MustOverride: इसका use करने पर property या method को override करना अनिवार्य हो जाता है।
For Properties and methods of Derived Class: 
  • Overrides: यह overridable property या method को override करते समय प्रयोग की जाती है।

VB.net मे सभी classes को inherite किया जा सकता है। लेकिन ऐसी सभी classes जिनमें की       NotInheritable keyword का use किया गया है inherite नहीं की जा सकती हैं। 
  • Classes को other projects से भी inherite किया जा सकता है।
  • VB.Net केवल Single और Multilevel inheritance को support करती है।
  • Inheritance करने के लिए Base और Derieved class दोनों के access type equal होने चाहिए तभी inheritance possible है।
 

 Inheritance VB.Net का एक महत्वपूर्ण feature है जो कि एक class को किसी अन्य class के features को use करने के facility provide करता है। यह पहले से बनी हुई class पर based नई class बनाने की सुविधा देता है। यह reusability को support करता हैं जहां किसी feature को जोड़ने या पुराने class के features को use करने के लिए उन्हे फिर से नहीं बनाना पड़ता है उन्हे inheritance के द्वारा पुरानी class से inherite कर नई class मे add कर लेते हैं। inherite कि जाने वाली class को base-class या parent class और inherite करने वाली class को derived या child class कहते हैं। 
Inheriting a Class: 
         Derived class मे base class को inherite करने के लिए Inherits keyword का use कर base class का नाम define कर देते हैं। inherite करने के बाद base class की सभी properties, methods, events, fields और constants को derived class मे use किया जा सकता है। 

DerivedCircle.vb

Public Class DerivedCircle    Inherits Circle     Public Function Circumfrence() As Double        Return 2 * math.pi * radius    End FunctionEnd Class
 
Form4.vb 
Public Class Form4
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        Dim dc As New DerivedCircle        dc.Radius = Val(TextBox1.Text)        MsgBox("Area = " & dc.Area)        MsgBox("Circumfrence =" & dc.Circumfrence)    End Sub
  
End Class

Constructors:

किसी object को declare करते समय उसे construct करते समय new keyword का use कर उसका नया instance create किया जाता है। जब किसी class के object को declare करते है तब new keyword object का नया instance बनाने के लिए प्रयोग होता है। Constructors ऐसे member functions हैं जो कि किसी भी नए object मे fields और properties को initialize करने के लिए use किए जाते हैं। जब object को create करने के लिए new keyword का use करते है तब यह automatically call होकर properties को initialize कर देते हैं।

किसी भी class मे multiple contructores को भी use किया जा सकता है। जिसमे एक default और कई parameterized contructors हो सकते हैं। 


Constructors new subroutine के द्वारा implement किए जाते हैं। यह constructor object के new instance create होने पर automatically call हो जाते हैं। constructor बनाने के लिए Class मे कहीं भी new subroutine को define कर किया जाता है। parameterized contructor के लिए उसमें arguments को भी define कर दिया जाता है। 


 



Public Class Circle


Public Sub New()



Radius = 0



End Sub



Public Sub New(ByVal R As Double)



Radius = R



End Sub



End Class



 


इस प्रकार object को declare करने पर constructor automentic call हो जाएगा और value initialize हो जाएगी। 



Dim c As New Circle(10)

Methods: 


Methods object द्वारा perform किए जाने वाले actions को represent करते हैं। methods functions को ही कहा जाता है। इन्हे किसी special task को perform करने के लिए use किया जाता है। यह किसी भी problem को devide कर उसे easy बना देते हैं। 



<Access modifier> Function <Function Name> ([arguments]) As <Return Type>





[Function’s statements]





Return <returning value>





End Function




Ex:




Public Function Area() As Double



Return math.pi * radius ^ 2



End Function

 Properties: 


    Properties class मे use होने वाली fields की तरह होती हैं। लेकिन इन्हे Get और Set property के द्वारा implement किया जाता है। जो की data को control कर input और output को कंट्रोल किया जाता है। इसमें data को set करते समय उसे verify भी किया जा सकता है। 


         Set Section data को property मे set करते समय use होता है। और Get Section data को property से retrieve करते समय invoke होता है। property मे value को pass करते समय उसे Set section मे verify किया जा सकता है। data के valid होने पर उसे local field मे store का देते है । इसी प्रकार data को get करने के लिए उसी variable के द्वारा value return कर दी जाती है। जैसे previous example मे Radius एक property है। property को निम्न प्रकार से declare करते हैं। 


Syntax:



<access_specifier> [Readonly] property <property_name> () As <Type>



Get



[Statements]



End Get



Set (Byval Value As <Type>)



[Statements]



End Set



End Property




Exp:



Public Property Radius() As Double



Get



radius = cradius



End Get



Set(ByVal value As Double)



If value < 0 Then



msgbox("Wrong Input:")



Else



cradius = value



End If



End Set



End Property

Fields: 


Classes fields, properties, methods और events से मिलकर बनती है। यह सभी object information को contain करती है। Fields class मे use होने वाले variables को कहते है। जिसमें value को directly read और write किया जा सकता है। Fields को normal variables की तरह ही declare किया जाता है। इन्हे member variables भी कहते हैं जो कि data को handle करने कि लिए किया जाता है। Previous program मे cradius field है। 



Private cradius As Double

Objects run-time entity हैं जो की class को run करने के लिए use की जाती है। class के variable को ही object कहा जाता है। यह run होकर class के data को store और use class के functions के द्वारा process करता है। इसके create होने पर यह memory मे space लेता है और delete होने पर यह space system को वापस हो जाता है। VB.Net मे object को declare करने के लिए variable declaration statement का use new keyword किया जाता है। 




Syntax




Dim <obj_name> As New Class_Name



Or



Dim <Obj_Name> As Class_Name = New Class_Name





Ex:     




Dim cir As New Circle




               Or 




Dim cir As Circle = New Circle 

Friday, April 26, 2013

VB.Net एक Object oriented programming language है इसलिए यह इसके सभी features को support करती है। जो निम्न हैं-

  1. Class: class एक user defined data type है जो की data और functions को contain करता है। इसका कोई visible interface नहीं होता है और न ही ये खुद से run हो सकती हैं। VB.Net मे Class fields, properties, methods और events को contain करती है।

Building a Class: VB.Net मे class बनाने के लिए यह Class Library project provide करता है। जिसका use कर class का collection बनाया जा सकता है। इसके अलावाWindows Application project मे भी Class Files को add कर use किया जा सकता है। इसके लिए सर्वप्रथम एक windows application project create कर लेते हैं। इसके बाद Project menu मे जाकर Add New Windows Form या Add Class select करते हैं। इसके बाद डिस्प्ले होने वाले New Item Dialog box मे Class templet को selectकरते हैं। और file का नाम enter कर Add button पर click करते है। इसके बाद Project मे एक class file add हो जाती है और class declare हो जाती है।

image084

image085

 

Class Declaration Syntax:

[Access_Modifier] [MustInherit | NotInheritable] Class <Class_Name>


        [Inherits <Base_Class_Name>]


        [Statements]


End Class



Access_Modifier: Optional. Can be one of the following:





MultiInherit / NotInheritable: Optional, Define class can be inherited or Not.


Class: Keyword, Define Class


Class_Name: Name of the Class


Inherits: used when Inheritance is used


Base_Class_Name: Name of base class at inheritance



image086

  1. Object: यह OOPs की basic run-time entity है। जो कि किसी object (person, place, a bank account etc.) को represent करता है। object class का variable है जो कि class को execute करता है और उसमे उपलब्ध methods को use कर डाटा को process करता है। object के create होने पर यह memory मे अन्य variables की तरह ही space लेता है।
  2. Class : Class एक user defined data type है जो कि data और code को contain करता है जो कि object द्वारा use किया जाता है। class एक structure है जो किobject कि working को define करता है। class को create करने के बाद उसके कई objects बनाए जा सकते हैं। इस प्रकार कहा जा सकता है कि class एक ही प्रकार केobjects का collection है।
  3. Data Abstraction and Encapsulation: data और functions को एक साथ bind करना Encapsulation कहलाता है। यह class का सबसे important feature है। इसमे डाटा को class के बाहर access नहीं किया जा सकता है केवल class के functions ही इसे access कर सकते है। data abstraction बिना background process कीdetails के data को input और output करने से है जिसमे यह Class के functions के द्वारा perform होता है।
  4. Inheritance: यह OOPs का एक महत्वपूर्ण feature है जो कि एक class को दूसरी class के features को access करने की facility provide करता है। इसमे एक classका object अन्य class की properties को भी access कर use कर सकता है। यह reuseability के feature को implement करता है जहां किसी class मे नए features कोadd करने के लिए नई class बनाकर उसमें पुरानी class के features को भी implement किया जाता है साथ ही नई class मे और codes भी जोड़ दिये जाते हैं।
  5. Polymorphism: यह भी OOPs का important concept है जो की एक से अधिक form को बनाकर उसे use करने की facility देता है। यह Function और operators दोनों के द्वारा perform किया जाता है जहां function name या operator वही रहता है पर arguments की संख्या या type या operands के type अलग होने पर अलग प्रकार से useहोते है और task को perform करते हैं। जैसे + operator numenrical addition और strings को जोड़ने के लिये use होता हैं।
  6. Dynamic Binding: OOPs मे inheritance और polymorphism इस feature को implement करते हैं जिसमें Binding या linking procedure के call होने पर perform होते हैं। इसे dynamic binding कहते हैं।
  7. Message Passing: OOPs मे objects परस्पर communicate करते हैं। objects information को send और receive कर communication करते हैं। OOPs मे message passing function call के समय perform होता है। जिसमें information function argument के रूप में function के माध्यम से object मे input होता है और object कीmethod उसे process कर result generate का देती है।

Programming मे दो प्रकार के program paradigm का use किया जाता है। यह Procedure Oriented और Object oriented programming हैं। नए समय की सभी programming languages Object oriented programming को use करती हैं। OOPs एक software development paradigm है जो कि procedure oriented approach मे आने वाले problems कोsolve करने के लिए किया जाता है। Object oriented programming, Data को program development के दौरान system मे flow नहीं होने देता। यह इसे किसी भी other functionके द्वारा अचानक change होने से रोकता है। इसमे problem को object के द्वारा solve किया जाता है और data और function object के साथ काम करते हैं। object के data को केवल object का function ही access कर सकता है। इस प्रकार कहा जा सकता है कि Object oriented programming एक ऐसी programming approach है जिसमे data औरfuncitons को एक साथ bind कर दिया जाता है जिसे class कहते हैं और उसे objects के द्वारा use किया जाता है। जहां प्रत्येक object के लिए उसके data और functions अलग अलग store और process होते हैं। यहा object के लिए memory space allocate होता है जिसमे उसका data और functions store होते हैं।

Features of Object Oriented Programming:

  1. Object
  2. Class
  3. Data abstraction and encapsulation
  4. Inheritance
  5. Polymorphism
  6. Dynamic Binding
  7. Message Passing

Contact us

Name

Email *

Message *