{"id":2178,"date":"2013-02-04T12:35:51","date_gmt":"2013-02-04T04:35:51","guid":{"rendered":"http:\/\/www.vbtutor.net\/index.php\/"},"modified":"2018-06-24T17:44:56","modified_gmt":"2018-06-24T09:44:56","slug":"visual-basic-2012-lesson-4-object-oriented-programming","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-4-object-oriented-programming\/","title":{"rendered":"Visual Basic 2012 Lesson 4: Object Oriented Programming"},"content":{"rendered":"<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-3-working-with-control-properties\/\">[Lesson 3]<\/a> &lt;&lt;\u00a0<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-tutorial\/\">[CONTENTS]<\/a> &gt;&gt;<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-5-writing-the-code\/\"> [Lesson 5]<\/a><\/strong><\/h4>\n<p>Before learning how to write the program code in Visual Basic 2012, we think it is better for you to grasp the meaning of object-oriented programming.<\/p>\n<p>As we have mentioned earlier, Visual Basic 2012 \u00a0is a full-fledged object-oriented programming language.What does it mean by object-oriented programming language? For a programming language to qualify as an OOP language, it must have three core technologies as follows:<\/p>\n<ul>\n<li><strong>Encapsulation<\/strong><\/li>\n<li><strong>Inheritance<\/strong><\/li>\n<li><strong>Polymorphism<\/strong><\/li>\n<\/ul>\n<p>These three terms are explained below:<\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"1777484012\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h3>4.1.1 Encapsulation<\/h3>\n<p>Encapsulation refers to the creation of self-contained modules that bind processing functions to the data. These user-defined data types are called classes. Each class contains data as well as a set of methods that manipulate the data. The data components of a class are called instance variables and one instance of a class is an object. For example, in a library system, a class could be a member, and John and Sharon could be two instances (two objects) of the library class.<\/p>\n<h3>4.1.2 Inheritance<\/h3>\n<p>Classes are created according to hierarchies, and inheritance allows the structure and methods in one class to be passed down the hierarchy. That means less programming is required when adding functions to complex systems. If a step is added at the bottom of a hierarchy, then only the processing and data associated with that unique step needs to be added. Everything else about that step is inherited. The ability to reuse existing objects is considered a major advantage of object technology.<\/p>\n<h3>4.1.3 Polymorphism<\/h3>\n<p>Object-oriented programming allows procedures about objects to be created whose exact type is not known until runtime. For example, a screen cursor may change its shape from an arrow to a line depending on the program mode. The routine to move the cursor on the screen in response to mouse movement would be written for &#8220;cursor,&#8221; and polymorphism allows that cursor to take on whatever shape is required at runtime. It also allows new shapes to be easily integrated.<\/p>\n<p>VB6 is not a full OOP in the sense that it does not have inheritance capabilities although it can make use of some benefits of inheritance. However, VB2012 is a fully functional Object Oriented Programming Language, just like other OOP such as C++ and Java. It focuses more on the data itself while VB6 and earlier versions focus more on the actions. VB6 and its predecessors are known as a procedural or functional programming language. Some other procedural programming languages are C, Pascal, and Fortran.<\/p>\n<p>Visual Basic 2012 allows users to write programs that break down into modules. These modules will represent the real-world objects and are knows as classes or types. An object can be created out of a class and it is known as an instance of the class. A class can also comprise subclass. For example, the apple tree is a subclass of the plant class and the apple in your backyard is an instance of the apple tree class. Another example is student class is a subclass of the human class while your son John is an instance of the student class.<\/p>\n<p>A class consists of data members as well as methods. In Visual Basic 2012, the program structure to define a Human class can be written as follows:<\/p>\n<pre style=\"font-size: 110%;\">Public Class Human\r\n'Data Members\r\nPrivate Name As String\r\nPrivate Birthdate As String\r\nPrivate Gender As String\r\nPrivate Age As Integer \r\n\r\nOverridable Sub ShowInfo( )\r\n MessageBox.Show(Name)\r\n MessageBox.Show(Birthdate)\r\n MessageBox.Show(Gender)\r\n MessageBox.Show(Age)\r\nEnd Sub\r\nEnd Class\r\n<\/pre>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"1777484012\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>Another Example:<\/strong><\/p>\n<pre style=\"font-size: 110%;\">Public Class Car\r\n'Data Members\r\nPrivate Brand As String\r\nPrivate Model As String\r\nPrivate Year Made As String\r\nPrivate Capacity As Integer\r\n\r\nOverridable Sub ShowInfo( )\r\n MessageBox.Show(Brand)\r\n MessageBox.Show(Model)\r\n MessageBox.Show(Year Made)\r\n MessageBox.Show(Capacity)\r\nEnd Sub\r\nEnd Class\r\n<\/pre>\n<p>Let&#8217;s look at one example of how to create a class. The following example shows you how to create a class that can calculate your BMI (Body Mass Index).<\/p>\n<p>To create a class, start Visual Basic 2012 as usual and choose Windows Applications. In the Visual Basic 2012 IDE, click on Project on the menu bar and select Add Class, the Add New Item dialog appears, as shown in the Figure below:<\/p>\n<p><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2357\" title=\"AddClass\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.jpg\" alt=\"\" width=\"819\" height=\"480\" \/><\/a><\/p>\n<p>The default class Class1.vb will appear as a new tab in a code window. Rename the class as MyClass.vb. Rename the form as MyFirstClass.vb.<\/p>\n<p>Now, in the MyClass.vb window, enter the following code<\/p>\n<pre style=\"font-size: 110%;\">Public Function BMI(ByVal height As Single, ByVal weight As Single)\r\n BMI = Format((weight) \/ (height ^ 2), \"0.00\")\r\nEnd Function\r\n<\/pre>\n<p>Now you have created a class (an object) called MyClass with a method known as BMI.<\/p>\n<p>In order to use the BMI class, \u00a0insert a button into the form and click on the button to enter the following code:<\/p>\n<pre style=\"font-size: 110%;\">Private Sub BtnCalBmi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalBmi.Click \r\nDim MyObject As Object\r\nDim h, w As Single\r\nMyObject = New MyClass1() \r\n h = InputBox(\"What is your height in meter\")\r\n w = InputBox(\"What is your weight in kg\")\r\nMessageBox.Show(MyObject.BMI(h, w))\r\nEnd Sub\r\n<\/pre>\n<p>When you run this program and click the button, the user will be presented with two input boxes to enter his or her height and weight\u00a0subsequently and the value of BMI will be shown in a pop-up message box.<\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block;\" data-ad-format=\"autorelaxed\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"6598395509\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-3-working-with-control-properties\/\">[Lesson 3]<\/a> &lt;&lt;\u00a0<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-tutorial\/\">[CONTENTS]<\/a> &gt;&gt;<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-5-writing-the-code\/\"> [Lesson 5]<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 3] &lt;&lt;\u00a0[CONTENTS] &gt;&gt; [Lesson 5] Before learning how to write the program code in Visual Basic 2012, we think it is better for you to grasp the meaning of object-oriented programming. As we have mentioned earlier, Visual Basic 2012 \u00a0is a full-fledged object-oriented programming language.What does it mean by object-oriented programming language? For a &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-4-object-oriented-programming\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2012 Lesson 4: Object Oriented Programming<\/span><\/a><\/p>\n","protected":false},"author":23013,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2178","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Visual Basic 2012 Lesson 4: Object Oriented Programming<\/title>\n<meta name=\"description\" content=\"This visual basic 2012 explains the concepts of object oriented programming in visula basic 2012\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_Lesson4.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Visual Basic 2012 Lesson 4: Object Oriented Programming\" \/>\n<meta property=\"og:description\" content=\"This visual basic 2012 explains the concepts of object oriented programming in visula basic 2012\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_Lesson4.html\" \/>\n<meta property=\"og:site_name\" content=\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Vbtutor\" \/>\n<meta property=\"article:modified_time\" content=\"2018-06-24T09:44:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.jpg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@liewvk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-4-object-oriented-programming\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_Lesson4.html\",\"name\":\"Visual Basic 2012 Lesson 4: Object Oriented Programming\",\"isPartOf\":{\"@id\":\"https:\/\/www.vbtutor.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_Lesson4.html#primaryimage\"},\"image\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_Lesson4.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.jpg\",\"datePublished\":\"2013-02-04T04:35:51+00:00\",\"dateModified\":\"2018-06-24T09:44:56+00:00\",\"description\":\"This visual basic 2012 explains the concepts of object oriented programming in visula basic 2012\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_Lesson4.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_Lesson4.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_Lesson4.html#primaryimage\",\"url\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.jpg\",\"contentUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.jpg\",\"width\":\"819\",\"height\":\"480\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_Lesson4.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2012 Lesson 4: Object Oriented Programming\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vbtutor.net\/#website\",\"url\":\"https:\/\/www.vbtutor.net\/\",\"name\":\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"description\":\"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Visual Basic 2012 Lesson 4: Object Oriented Programming","description":"This visual basic 2012 explains the concepts of object oriented programming in visula basic 2012","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_Lesson4.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2012 Lesson 4: Object Oriented Programming","og_description":"This visual basic 2012 explains the concepts of object oriented programming in visula basic 2012","og_url":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_Lesson4.html","og_site_name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","article_publisher":"https:\/\/www.facebook.com\/Vbtutor","article_modified_time":"2018-06-24T09:44:56+00:00","og_image":[{"url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.jpg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_site":"@liewvk","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-4-object-oriented-programming\/","url":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_Lesson4.html","name":"Visual Basic 2012 Lesson 4: Object Oriented Programming","isPartOf":{"@id":"https:\/\/www.vbtutor.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_Lesson4.html#primaryimage"},"image":{"@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_Lesson4.html#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.jpg","datePublished":"2013-02-04T04:35:51+00:00","dateModified":"2018-06-24T09:44:56+00:00","description":"This visual basic 2012 explains the concepts of object oriented programming in visula basic 2012","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_Lesson4.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2012\/vb2012_Lesson4.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_Lesson4.html#primaryimage","url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.jpg","contentUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.jpg","width":"819","height":"480"},{"@type":"BreadcrumbList","@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_Lesson4.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2012 Lesson 4: Object Oriented Programming"}]},{"@type":"WebSite","@id":"https:\/\/www.vbtutor.net\/#website","url":"https:\/\/www.vbtutor.net\/","name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/users\/23013"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=2178"}],"version-history":[{"count":64,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2178\/revisions"}],"predecessor-version":[{"id":13096,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2178\/revisions\/13096"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=2178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=2178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=2178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}