03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Object Orientation Basics x 197<br />

C#<br />

Person myPerson = new Person() { FirstName = "Imar", LastName = "Spaanjaars" };<br />

In <strong>VB</strong>.<strong>NET</strong>, you need the With keyword <strong>in</strong> front of the properties list. In addition, you need to prefix<br />

each property name with a dot (.). Other than that, the syntax is the same for both languages.<br />

Object <strong>in</strong>itializers are great if you need to set a bunch of properties on an object quickly without<br />

be<strong>in</strong>g forced to write specialized versions of the constructors.<br />

Although it’s useful to have this Person class <strong>in</strong> your application, at times you may need specialized<br />

versions of a Person. For example, your application may require classes like Employee <strong>and</strong> Student.<br />

What should you do <strong>in</strong> this case? Create two copies of the Person class <strong>and</strong> name them Employee<br />

<strong>and</strong> Student, respectively?<br />

Although this approach certa<strong>in</strong>ly works, it has a few large drawbacks. The biggest problem is the<br />

duplication of code. If you decide to add a SocialSecurityNumber property, you now need to add it<br />

<strong>in</strong> multiple locations: <strong>in</strong> the general Person class <strong>and</strong> <strong>in</strong> the Employee <strong>and</strong> Student classes. Object<br />

<strong>in</strong>heritance, a major pillar of object orientation, is designed to solve problems of this k<strong>in</strong>d.<br />

Inheritance<br />

Earlier you learned that System.Object is the parent of all other data types <strong>in</strong> .<strong>NET</strong>, <strong>in</strong>clud<strong>in</strong>g<br />

all the built-<strong>in</strong> types <strong>and</strong> types that you def<strong>in</strong>e yourself, mean<strong>in</strong>g that each type <strong>in</strong> .<strong>NET</strong> (except<br />

Object itself) <strong>in</strong>herits from Object. One of the benefits of <strong>in</strong>heritance is that you can def<strong>in</strong>e a<br />

behavior at a high level (for example <strong>in</strong> the Object class) that is available to <strong>in</strong>herit<strong>in</strong>g classes automatically<br />

without the need to duplicate that code. In the .<strong>NET</strong> Framework, the Object class def<strong>in</strong>es<br />

a few members that all other objects <strong>in</strong>herit, <strong>in</strong>clud<strong>in</strong>g the ToStr<strong>in</strong>g() method.<br />

To let one class <strong>in</strong>herit another, you need to use the Inherits keyword <strong>in</strong><br />

<strong>VB</strong>.<strong>NET</strong> <strong>and</strong> the colon (:) <strong>in</strong> C#, as shown <strong>in</strong> the follow<strong>in</strong>g example that<br />

def<strong>in</strong>es a Student class that <strong>in</strong>herits Person:<br />

<strong>VB</strong>.<strong>NET</strong><br />

Public Class Student<br />

Inherits Person<br />

C#<br />

public class Student : Person<br />

{<br />

}<br />

To see how <strong>in</strong>heritance works, th<strong>in</strong>k aga<strong>in</strong> about the Person class shown <strong>in</strong><br />

earlier examples. That class had a few properties, such as FirstName <strong>and</strong><br />

LastName, <strong>and</strong> a Save method. But if it is <strong>in</strong>herit<strong>in</strong>g from Object, does it<br />

also have a ToStr<strong>in</strong>g() method? You bet it does. Figure 5-7 shows the relationship<br />

between the Object class <strong>and</strong> the Person class that <strong>in</strong>herits from<br />

Object.<br />

FIGURE 5-7

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!