Coding 101 32 (Transcript)
Shannon Morse: Today on Coding 101 we are checking out classes, specifically, abstract classes and interfaces.
Netcasts you love from people you trust. This is TWiT! Bandwidth for Coding 101 is provided by Cachefly; at C-A-C-H-E-F-L-Y dot com.
Fr. Robert Ballecer, SJ: This episode of Coding 101 is brought to you by Hover dot com. Hover is the best way to buy and manage domain names. It’s simple, honest, and easy to use. For ten percent off your first purchase go to Hover dot com and enter promo code C one zero one eight. Welcome to Coding 101, it’s the Twit show where we let you in the wonderful world of the code monkey; I’m Father Robert Ballecer.
Shannon: And I’m Shannon Morse and for the next thirty minutes we are going to get you all learned up on everything you need to know to be a C# code warrior.
Fr. Robert: I have had way, way too much sugar.
Shannon: I have too, see?
Fr. Robert: Now, last week, Shannon, we covered classes, which was nice, because we are doing something a little different with this module where we revisited the first language that we taught, and we’re giving sort of a 102 rundown. So it’s not just loops and variables, but we’re talking about some of the more high level concepts. And of course, we have to cover classes, because if we’re covering objects, we need classes.
Shannon: But it’s scary.
Fr. Robert: It is a little scary.
Shannon; Actually, It’s not too bad. I started playing with my own code, and I was like, “okay, I can do this.” So, you want to start off with some Snubs compile?
Fr. Robert: Let’s do that please.
Shannon: Yeah, alright, let’s go over to my desktop. So I have Visual Studio Express running over on my desktop here; ta-da! And I have created this one called “Program dot CS.” If you are confused about how to get a console program running in Visual Studio Express for Windows Desktop, the free version, just go to our last episode we ran through it in there, Lou did a great job of that. So I made this one, this; I’ll go over here; okay so I made a new class called “Food.” Ta-da! See? Class called “Food.” And under it I decided to make this public stream called “Color.” So under “Color” I also chose that colors generally are going to be green, for pretty much every food out there. Except, under there, I also decided to make a variable of class.
Fr. Robert: You’ve got an inheritance; I like that, that’s important.
Shannon: So I made an inheritance of food, called “vegetable,” and under “vegetable” I decided to make all vegetables “orange.” So then I went over to “program,” and this is totally weird, but hey, it’s an idea. So under here I have “Class, Program” and under “Static, Void, Main String,” So I didn’t change any of that. I made my first food, I just called it “Food one,” and that one is going to be “green.” The second one, now the reason I put this in here, you’ll notice a mistake, but I put this in here is specifically to show you that just because I wrote “purple “ in here, it’s still going to stay green when I run it because I haven’t overrided the original “green” up here.
Fr. Robert: Which is important. Lou told us about this the other day, which is if you ‘re going to inherit from a class, if you’re going to make a derivative object, then you need to override if you’re going to be changing from the original.
Shannon: Right. See you’ll notice when I run this it’s not going to say “green, purple, orange,” it’s going to say in the console write out, it will say “green, green, orange.” Down at the bottom, the third one I wrote out is “vegetable, pumpkin.” So I made a pumpkin, which is a vegetable, which is in the class of food, and under here I just changed pumpkin’s color to “orange.” Now when I run this, I’ll hit “start” up here, you’ll notice that it says “green, green, orange.” Ta-da!
Fr. Robert: Ta-da!
Shannon: So another very, very simple one, it’s very similar to the one he did for “animal” last week. But it gives you an idea of something else you can do with it. So you don’t have to use hints all the time, you can use strings too.
Fr. Robert: Now this is a very good example, because we have been trying to tell our audience that this is not earth shaking.
Shannon: It’s not.
Fr. Robert: If you watch the first module of Coding 101, if you watch them teach us C#, it’s the same concept. The only thing we are doing is we’re organizing them better. We’ve always had the idea of making things easy to use.
Shannon: That’s right.
Fr. Robert: Be them functions or methods, you won’t have to rewrite the over and over. This is just taking it to the next logical step, letting us create objects.
Shannon: Yeah! Now I also wanted to take a moment and send a shout out over to our viewers who have joined our community over at Google Plus, which is bit dot LY slash TWiT coding one zero one. We had four specifically that I wanted to point out this week. First up was Cosmic Ray, he linked us to a lot of really good videos made over at channel nine dot MSDN dot com, which are fundaments of C# and they work in Visual Studio Express as well, so they are really, really good videos, and the guy is really nice to listen to, as well. So I enjoyed them. We also got one from Nathan Fulmer. He went on a mini rant, but it was a good rant.
Fr. Robert: It was a good rant; it was a very good rant.
Shannon: He said, basically what he got down to was just because you know the syntax of a code doesn’t mean that you are a programmer. You need to know how to solve problems. So he links to a couple of different places where you can find problems you can try to figure out with code, as well as some other information about it; a really good article. So definitely check out his links I there, in the Google Plus community, as well. The third one I wanted to check out was Kelvin; so Kelvin said, “Hey, don’t forget MONO,” which is available on Mac, so that is a C# compiler on Mac. And the last one I wanted mention was from Joe. So Joe shared this information about C# in particular, he said you don’t have to necessarily use a compiler like Visual Studio Express, you can do it in Notepad, and he links us to a bunch of information about how to do it, and he walks you through it. He is also really good about helping people, if you run into any mistakes, if you want to run it in Notepad. So thank you guys so much for sharing your expertise in C#, really appreciate it.
Fr. Robert: Those are all really great tips. I especially like what Nathan was saying. This is actually something that we said at the very beginning of Coding 101, which is the reason why we’re switching languages, were not sticking with one for any amount of time is because we’re not really teaching languages, we’re teaching methodology.
Fr. Robert: We’re teaching problem solving. The whole idea is can you take something in the real world, and solve it inside of code. And that’s dead on. The other thing I really liked is the fact that Joe gave us the information about being able to write in Notepad. And of course you can do that because code is code. But the reason we are using Visual C# is because A: it’s free; you can get it from Microsoft, it doesn’t cost a thing; it is a big download. But what you get with that download, and this is especially important to beginners, is you get a developer environment that tells you what you can do.
Fr. Robert: Right, we have both used this.
Shannon: Really, handy. It just gives you a nice little red squiggle every time you mess up.
Fr. Robert: It will tell you if you did something wrong and it will also give you the options. You can always use the dropdown menus to say, “I just used the console command, what else can I use?” And it will tell you all your options. So yes, Notepad; I like the idea of using Notepad because it’s really for those people who know what they are doing. But if you are doing a complicated project, or if you are just starting, at least learn a little bit in the IDE so that it helps you out.
Fr. Robert: Yeah, yeah. Now speaking of help you out, what if I wanted to help you get the word out about any project that you might be running?
Shannon: Ooh, how would I do that?
Fr. Robert: Well as they say in real estate, it’s all about location, location, location. On the internet, it’s the same thing: location, location, location; although we could say url, url, url.
Shannon: This is why no one ever goes to my website.
Fr. Robert: Yeah. If you have a great idea, if you have a great project, if you’ve got a great website, but you’ve got a horrible, horrible name, it’s difficult to navigate there.
Shannon: Yeah, no one can find “Snubsy one two three X Q Y.”
Fr. Robert: How about “Snubs dot Club?”
Shannon: Oh, that would work.
Fr. Robert: That’s easier, it’s shorter, and you can do it with Hover, the sponsor of this episode of Coding 101. Now what is Hover? Hover is the number one trusted provider of domains. When you have a great idea and you want to secure a domain for it, you do want something that’s catchy and you want someone to set it up for you who is trustworthy. Well that’s what Hover gives you, exactly when you need it, to get the job done. You will find the perfect domain for your idea so you can get started working in it, rather than all that back end stuff. And right now Hover is having a sale on all new domain extensions through September first. The sale is for new and existing customers alike. Summer is the perfect time to start a new project, and every single domain is deeply discounted so you can choose what you like: dot club; how about “Snubs dot ninja org?”
Shannon: I like “Snubs dot Pub.”
Fr. Robert: How about “Snubs dot guru?”
Fr. Robert: Or how about “Snubs dot just about anything?” That’s what you get with Hover: anything you need. People love Hover; geeks, developers, designer and programmers love Hover because they know they will have the best tools and support. But you don’t have to be an expert to get a domain because the service is simple enough to use. They take all the hassle and friction out of registering a domain. Hover gives you easy to use, powerful tools to manage your domain so that anyone can do it. Now I told you that it’s easy, let me explain how easy it is. All you have to do is search for a few keywords, and Hover will show you the best available options and suggestions. In fact, one of the things I like about Hover is sometimes when I’m searching for a domain, and I can’t get the one I like, it gives me suggestions that are not just close to the name, but it will say, “I think you’re trying to do this; here’s an even better domain.” And that’s just one of the services that makes Hover so easy. They also offer a valet transfer service to make the process completely painless. For free, Hover will take care of the entire process for you, and let you know when your domains are settled in your hover account. They will transfer all your DNS settings now matter how many domain names you may have, and it’s no additional cost. Now the big thing, however, has to be that Hover is honest. They don’t believe in heavy up handed selling. They are not going to sell you something and then immediately turn around and go, “Well, you know what; if you want full functionality, why not get this?” Hover is known to be just honest. They will sell you what you want, when you need it, for what you need. Plus, Who Is Privacy is included for free on every domain that supports it. Now if you ever do need support, Hover has the best customer support around. It’s known for its no wait, no hold, no transfer phone service. So when you call, a real person is ready to help. They also offer you volume discounts. They will give you a discount on your domain renewal starting at just ten domains, and going up in value from there. The idea is, the more domains you have in your account, the less you have to pay to renew them. So here is what we want you to do: visit Hover today to register your domain name, and for ten per cent off your first purchase, take advantage of their summer sale now. Go to Hover dot com and use our promo code: C one zero one eight. We thank Hover for their support of Coding 101.
Shannon: I’m ready.
Fr. Robert: Ready?
Shannon: Ready to move on from them classes.
Fr. Robert: Well, we can’t really do that yet, missy. ‘Cause classes are going to be on our minds for a while. Why are we doing that?
Shannon: I don’t know.
Fr. Robert: I really don’t know. Okay.
Shannon: Because it’s fun.
Fr. Robert: It is fun. But let’s move on to the ivory tower. Now last week we learned about classes, and it was a lot of information, but hopefully people were able to sit down and say, “Oh, I get it. It’s just sort of a way to organize everything.”
Fr. Robert: You got it, no problem. You were able to work it into your programs, and you could use all your existing code. If you did the first module, drop it into a class, you’re good to go.
Fr. Robert: Now let’s do a quick review. When we talk about classes we talk about blueprints.
Fr. Robert: Right, we talked about this last week. A class doesn’t actually exist; a class is just a blueprint for something that could exist, right?
Shannon: Yes, I remember that.
Fr. Robert: When I initialize it, when I declare it, when I create an object from that class, then it actually exists, then I can actually interact with it.
Shannon: Yes; yeah. I understand that.
Fr. Robert: And I can also, I can create, I can instantiate multiple copies of that class.
Shannon: Right, Yeah.
Fr. Robert: Right, okay?
Shannon: You can name them just different names.
Fr. Robert: You can give them different names; you can overwrite methods within various classes so that you can customize the classes. But the idea is, you write it once, it has a hierarchy, it has all the methods, it has all the variables, it has all the data that you might need, and then you just make as many copies as you require, and you change the things that you have to change.
Shannon: Okay, so where so you go from here?
Fr. Robert: Well, you know, we talked last week about declaring a class and when we; go ahead and go to my laptop really quickly; you may remember this: this is the code to declare-my laptop that’s not my laptop…
Shannon: That’s me!
Fr. Robert: This is the code to declare a public class called “C one zero one.” So; public, class, C, one zero one. And then inside of here is the body. The fields, the methods, the properties, the events, everything that you might want to enclose.
Shannon: Pretty much anything.
Fr. Robert: Yeah. It could be the console commands.
Fr. Robert: It could be your group of variables; it could be any of those methods slash functions that you may have written in the first module. No problem, right? And we also know that when we want to actually turn that into something, I have to instantiate it, I have to create the object. This was the bit we learned last week that some people kind of freaked out about.
Shannon: So this is where you actually create it?
Fr. Robert: Right. So I go from blueprint to something that actually exists inside a system memory. In this case I have “C 101 object one.” So I now that object one is going to be something that I can reference based on the class called C 101, and then I’m creating a new class from, a new object from the class of C 101.
Shannon: So every time you do this you can create a different object and they can all just call back to the same class and do the same thing.
Fr. Robert: Right. So C 101 object one equals new C 101. C 101 object two equals new C 101, and I can do that as many times as I need, right? Easy.
Shannon: Yeah. I get it!
Fr. Robert: Easy, peasy. Now let’s talk about a little something-something that we did at the end of the lesson yesterday, or last week, and that is the idea of inheritance. Lou briefly talked about this; you used it in your code.
Fr. Robert: The idea is you don’t just have to use the class the way it is written, right?
Shannon: You can override it.
Fr. Robert: You can override it, but on order for you to override it you first have to inherit. So go back to my computer; inheriting from a class looks like this: so I have my class: public class module four, but this, right here, tells the compiler that I will be inheriting form C one zero one. I will now make a sub-class module four based on C 101. And then in here I could use those override statements, to make sure that I’m using the proper methods, or that they are spelled out correctly for whatever I’m going to be using it for.
Shannon: Right. So in my case, this new class, or the sub class, ended up being “vegetable.”
Fr. Robert: Right.
Shannon: And then we could just override it with…
Fr. Robert: Correct, correct, correct. And this is all part of the big, the grand scheme of C#, which is to give your data methods, to give your code, better hierarchy, better organization.
Fr. Robert: Come back, Brian. So does that p[art make sense?
Shannon: Yeah, it totally makes sense to me.
Fr. Robert: Yeah, yeah.
Shannon: It takes a little bit; I think it really helps to do your own code to understand it.
Fr. Robert: It does.
Shannon: That’s how I; I did my own code and I was like, “Oh! Okay, I understand classes now.”
Fr. Robert: Now there’s a little wrinkle her.
Shannon: Uh oh.
Fr. Robert: Yeah.
Shannon: Don’t tell me that! I just got it!
Fr. Robert: No, no, no, no, this is good; this is actually going to make this easier. Because there are always going to be some people when you write a class and then you inherit it and you start overriding it, people are going to say, “Well, why would I write it in the first place, if I’m just going to override all the methods?”
Shannon: That’s a good point; why would you?
Fr. Robert: Well, you know it still does make for a better programming; it makes for a better hierarchy. But there are two other tools within C#, actually within most object orientated programming languages, that allows me to create something that is like a class, but not quite.
Fr. Robert: Yeah.
Fr. Robert: Let’s talk about the first one. It’s called an abstract class.
Fr. Robert: Okay, so imagine this: If a class ids a blueprint, so the blueprint that’s ready to gibe to system memory and say, “Go ahead and make an object out of this.”
Shannon: Got that part.
Fr. Robert: An abstract class is more like the architectural outline. It has the shape, it has the general look and feel, but I can’t actually create it. The system memory wouldn’t know what to do with it because it’s incomplete.
Fr. Robert: It’s what we call…
Shannon: So it’s like somebody drew a picture and they were like, “Here, make a blueprint out of this.”
Fr. Robert: Yeah. Classes can be instantiated.
Fr. Robert: Abstract classes cannot.
Shannon: Okay, got it.
Fr. Robert: Okay? So they’re incomplete, they’re incomplete.
Shannon: Why would you make something incomplete?
Fr. Robert: This is exactly what we want to talk about. Why would you make an abstract class? Why would you make something that isn’t complete?
Shannon: Sounds like a waste of time.
Fr. Robert: It sounds like a waste of time, but it’s not for this reason: even though it can’t be instantiated, I can still create a sub class of that abstract class.
Shannon: You can?
Fr. Robert: I can, just like I did with a regular class. And then I can override the components that are incomplete, and I can fully implement them.
Fr. Robert: Think of it this way:
Shannon: Depending on whatever your needs might be.
Fr. Robert: Yes, exactly. Depending on what my needs might be. But the reason why I use an abstract class is, if say on a programming team, I want to force other people to use a particular structure.
Shannon: Oh yeah, that’s smart.
Fr. Robert: This is good programming. So let’s say I’m the lead programmer for a project. I will create an abstract class that has all the methods that I want people to use. And it has all the variables in the way that I want people to use them. It has all the procedures spelled out in how I want people to write them. And now I tell all my programmers, I say, “I want you to use this class, this abstract class, to create what you code.”
Shannon: Oh, that’s awesome. It will keep things organized and hopefully not break.
Fr. Robert: Yeah, it gives them a structure.
Shannon: That’s cool.
Fr. Robert: Now creating an abstract class, or declaring an abstract class is really no different than declaring a class the only thing that is different is that you add the word “abstract” before class.
Shannon: Oh, okay. That’s easy.
Fr. Robert: Right, but that tells C#, “I cannot instantiate this, it’s not complete.” If you tried to compile an abstract class, it will not work.
Shannon: Yeah. That makes sense.
Fr. Robert: Because there is nothing in there, right?
Shannon: It would probably just give you an error?
Fr. Robert: Yeah, and it would also tell you, “This is abstract; it’s never been fully implemented.” Now a few thing s about abstract classes: one, they are created with the key word “abstract.”
Fr. Robert: Two, they are incomplete, therefore they cannot be instantiated. They cannot be created from the class. The can be created from a sub class of that class, a sub class that has fully implemented the procedures within it. Abstract classes are incomplete; therefore they can only be used as sort of what we call a base class. And non-abstract classes that are derived from abstract classes must complete any abstract members that are inherited from that abstract class.
Shannon: Oh, okay. So this is where the coders come in.
Fr. Robert: This is where the coders come in. If me, again, as the lead programmer, I make an abstract class, and I have a bunch of abstract members, abstract methods, so on and so forth, the programmer that I tell to use my class can’t just take it.
Fr. Robert: Turn it into a sub class, and run it. He actually has to go through all the places where I put abstract and put in the proper implementation.
Shannon: He has to fill in the blanks.
Fr. Robert: He has to fill in the blanks.
Fr. Robert: Now there is another bit of this called the interface, the interfaces, it’s the same thing as an abstract class, except it’s completely devoid. All it has is the tag lines.
Fr. Robert: Yeah. But they do the same thing. Again, the whole idea is to enforce a whole hierarchy of coding.
Fr. Robert: Okay and just as a class allows me to organize methods and variables and procedures, an abstract class lets me organize methods and variables and procedures, leaving some of them open to the programmer, and then an interface leaves everything open to the programmer except how the program is structured.
Shannon: Okay. Again, this is one of those times where the code will make more sense when you see it.
Fr. Robert: Yeah, exactly. This is kind of complicated; before we get way off the rails, before we get too confused, let’s actually show you what it looks like in the IDE. This is the time in the show where we bring back our code warrior. We welcome back to the show the man, the myth, the McLegend, Mister Lou Maresca. Lou Maresca, thank you very much for joining us once again on Coding 101.
Lou Maresca: Thanks for having me back.
Fr. Robert: It’s always good to see you. Now Lou, you showed us a little bit of inheritance yesterday. And it’s funny because we did get a couple of people who wrote in and said, “Wait a minute, wait a minute, if you’re just going to override like Lou did, then why do I write the class in the first place? If you’re just going to get rid of everything I gave you, then what’s the point?” And that’s why we are talking about abstract classes and interfaces.
Lou: That’s right, yeah. I mean the big thing about abstract classes is, you know, a lot of times there is actually a lot of what we call designer or coding patterns that are out on the web, or around, you know what computer scientists use today, they use the abstract capability. Because what it allows you to do is what you said, sometimes classes get so big and have so much implementation in them, you don’t necessarily want to override everything, but you do want to follow what we call the signature or the architectural plans of that class still. And so that’s where abstract kind of comes in. There is another type, and I’m not sure if you want to get into that yet, there is also what we call interface, too. And that one is a little different from an abstract class, because what that is, is basically just a signature, a breakdown, a blueprint of what you want the class to implement, but it doesn’t have any component, no implementation whatsoever, it’s just a breakdown of it.
Fr. Robert: Interface has no body, there’s nothing. There’s just, okay, there should be this method, this method and this method.
Shannon: Oh, okay.
Fr. Robert: We leave the rest up to you. But you know Lou, it’s interesting because when someone is coding individually, just by themselves, just practicing, I often hear, “Well, why do I have to do this? It’s so much easier if I just write it. You know, why am I writing an entire class just to console a menu? It doesn’t make sense to me.” But I think the key is when you start working with larger organizations and much bigger programs, you want to be able to understand someone else’s code without having to pour over every line. And if you force them to use a class that everyone else is using, then you at least know what is supposed to be in each place.
Lou: Yeah, a good way to think about it too is just a building block to the bigger thing. So if you are building a small piece of functionality, classes are what they call separations of concerns, where you are supposed to basically separate the functionality from everything else. So the class should be by itself, it should be able to do stuff by itself without the need to talk to anyone else. So when you think about a class and think about functionality, let’s say you wanted to talk to a web server and get some information, well that’s really all the class should do. And from then on, whoever needs to talk to the web server and get some information, they use that class. And that’s kind of the key is you kind of have a bunch of building blocks to potentially use it later and have more functionality later and easier.
Fr. Robert: We have Dr. Morbeius in the chat room and I like him, he’s a good member of the TWiT army, but he brings up what again, I think a lot of people say, which is object orientated programming is mostly for corporate programming. He’s tried OOP before, and it just makes things too complicated. I’m sure you hear that a lot from novice programmers, what would you tell them?
Lou: So I completely disagree. Sorry. But I think one of things you have to do is, you have to understand that most people, most of the programs we’ve been showing here are real tiny, they’re small, they are easy to follow. But if you stared getting into, even if I just were to program at home, I built an app for my son to just show photographs on the TV, I mean even that app has about ten thousand lines of code. And if that weren’t broken out into classes, there would be no way I would be able to fix that code if it ever broke. It would be just one line file, four thousand lines of code, and I would have to find spots where things were breaking and it would be kind of a nightmare to maintain, even as a personal application. So I think it’s a good practice, even as a home user, to start using classes and breaking it down into functional components, functional pieces.
Fr. Robert: Okay, enough of the ivory tower, let’s get into the IDE. Show us what it looks like when you make an abstract class. Show us what it looks like when you actually use it.
Lou: Sure, sure. One of the things is, if you show my screen again real quick, we will just do again, a simple console app. And what I wanted to show is kind of getting into the abstract side; we will also show an interface version of it so you can kind of see the difference. So I am going to add a class really quick, and I’m going to call it “social network.”
Fr. Robert: Could you go ahead and bump up your font size just so that…?
Lou: You bet.
Fr. Robert: There we go.
Lou: Alright. So then, this is kind of what you see. But what we’re going to add in there, we’re going to add that it’s public, it’s visible by everybody, and that it’s abstract. How easy is that? Now it becomes a class. But if we go back over here, like you said, let me bump it up really good, there we go. So if I go in here in the main program that gets created when I create this app, if I try to create “social network,” like you said, and I start to create a new object off of it, it’s going to be like…
Shannon: It’s not going to know what to do.
Lou: Sorry, can’t create an instance of an abstract class, sorry, you’re not able to do that. Basically, what we what to do is add some functionality in here but we don’t necessarily want to have all the functionality. So I have a little version of this here and I’m going to copy it over real quick. It’s faster for me to type. What it is going to do is it’s basically going to be the blueprint for the “social network” class. So I’m going to pop this in here, and I think I’m missing a curly brace somewhere that’s why it’s not formatting. Curly brace down here, there we go. So if we look in here, what is this class actually doing here? First thing is, is it has an email address in what we call a property. And a property, we never really talked about them, but we consider them basically just an extension of a field or a variable. It’s a way to put some code inside before you get it, before you set it. Because if I were to put a string email address in here, and make it just a field or variable, then there would be no way for me to do anything in between when somebody was getting a value out of it, or putting a value in between it. So this way it gives me the ability. But you notice that in here I make it virtual. So not only did I override this later, but it will just return a string whether I just want to store it here or I want somebody to override. And then also I have another property called “allowed number of characters” and it’s allowing a hundred and forty characters by default, but I can again override this if I really wanted to. So what I wanted to do also is add some other implementation in here, there are some other functions and stuff that I’ll add. So we’ll add it below here. And you will see some other things that you will notice. First thing is, I added a function, which is called “login,” but it takes an email address, but again, it’s abstract, which means it has no; normally I’d have curly braces on here and have some code, but I don’t have that. Because you know what, login for every different social network, whether it be Facebook, or Google Plus, they are all different. So most of them take an email address to start with, so that’s what this function has.
Fr. Robert: That’s a great example of where you would use an abstract. Which is, look, if I’m writing a program and it’s supposed to interfacing with social networks I know there’s going to be a login, but I want to give each coder who is going to code for each social network, the ability to specify specifically what’s going to be used. I know they are going to use the email address, but other than that, I don’t. So that’s what I’m going to give them.
Shannon: That makes sense.
Lou: We could make it even more abstract and remove the email address even from here because, like Twitter allows you to use your username too. It doesn’t necessarily take an email address. In this case we could maybe say a user name like that and that makes it even easier and more abstract. And then we have “posts” which are very similar, more abstract. It has no curly braces underneath it, it has no code whatsoever. And again, it takes a message so if you want to send a message to a wall or a public place, it just takes a message. And then very similar, a little bit more complex signature we call this, but this says I’ll take a photo too. So if I want to post a photo it takes the bytes in an array for that photo and messaging in abstract. Then the last one down here is actually a function, but you will notice something: I actually have curly braces underneath it. And I have that, it will say “who am I” it’s going to return the email address of the user is actually “who I am.” But that’s kind of the key is what you’ll notice is a lot of times abstract classes will contain a little bit of implementation, too because it’s just a no brainer that maybe by default, there is no such functionality on Twitter to say, “what’s my user information?” And there is no way to get that so in this case, it will just return what my user name is. So that’s a way to actually add some (unintelligible) into the abstract class. So now that I have this kind of model, let me show you like a specific example of a type of social network. So for instance, I’m going to create a class for Twitter. And you notice here that; actually, let me do another one in here so you can see what happens when I actually do it. So I’m going to create one for “Facebook,” and I’m going to inherit from “social network,” and as you see there was a little blue thing underneath it, it’s basically saying, “Well there is some stuff you can do here.” And you will notice that if I hover over it, it says options that you should basically implement all the abstract stuff. So all the stuff that doesn’t have curly braces that I marked as abstract I can click on this and say, “implement it.” And you notice like immediately it implements all the methods.
Shannon: That’s cool.
Fr. Robert: I did not know you could do that. That is fantastic. So if someone gives you an abstract class with a bunch of abstract members, the minute you type in the line to say, “I’m going to be inheriting form social network,” which is an abstract class, the IDE will say, “Oh, if you are going to do that, these are all the numbers that are abstract. You need to actually tell me what they are going to do before I can do anything with them.”
Lou: Right, pretty cool.
Shannon: That’s awesome.
Lou: Makes it a lot faster. There is a keystroke before it, too, for power users, but I can’t remember where that is. But it will basically implement it really fast. It adds in here, like this is called an exception, but right now the reason why it does this is if you run this and somebody tries to use this it says, “Oh by the way, the developer didn’t implement this yet.” So anyway, let me delete this because I’m not going to create a Facebook, I’m just going to do a Twitter one for now. And I have a Twitter one, and you’ll notice in here, they don’t really do anything yet, I just deleted the exception, but again, I overwrote the login, and I’m doing some other stuff in here that I wouldn’t normally. So I’m not going to add any implementation in here, but basically all this would do is put all my code to “post a message” and “post a photo,” that kind of thing. And this would call into the Twitter interface to basically do that. So now if I pop back over to my application, I can actually do something interesting. Because I can undo this, but instead, I’m going to do “new Twitter.” And now I can actually do stuff. I can actually return an email address, I can login and post, and now it’s all because I have a specific version of that social network. And now you know you have inheritance, but you also have a little bit of the ability to force a user to implement some things, basically it’s a way to force a developer to implement things when you as a developer creating a base class and don’t want to implement everything so you force a developer to implement for you.
Shannon: That makes sense.
Lou: Yeah, yeah.
Shannon: It’s a lot easier to understand when you actually see the code. So you have the layout, the abstract class, and then you actually have to make real classes from it, for people to be able to use it.
Fr. Robert: Precisely. And this is why we like using this visual environment, because it gives you that option of saying, “Well I don’t want to scrub through the entire class and look for what I need to override, you just tell me what are the things I need to fill in.”
Fr. Robert: What we would say is, “You show me what I have to fully implement. What’s not complete that I have to fill in the blanks for?”
Lou: Right, right.
Shannon: So you can use your social network one to like, build an app that requires a social network login, they could choose between Facebook, Google Plus and Twitter, and it would just implement that different class depending on which one you chose.
Fr. Robert: Right. And what you would so is write an abstract class with “login” as one of the procedures, one of the methods, and then you would put in the comments, you would say, “This is where you give me all the information I need for that specific social network.” And it’s up to the programmer who is instantiating it, to fill that in.
Shannon: Problem solving!
Fr. Robert: And again folks, we know that there are a lot of you that are looking at this going, “Why would I do…this is silly.” This again, we’re going into Coding 102 here, this is what you do to enforce a hierarchy. Lou has said it, Snubs has said it, I have said it, sometimes, especially when the program gets big, when it goes beyond you, you need to be able to enforce a certain structure to the program, otherwise its going to be impossible to fix or change in the future. That’s what this does, which is why we are giving it to you.
Lou: There is a really cool principle, it’s kind of science-y, but it’s called the Open and Close Principle and it’s the whole idea is you are always open to add things, but you should be closed to changing things. And the reason is that if you build a class that has a specific functionality, you should always be open to add additional functionality, and it’s easy to do that when you break it apart and have little pieces, but you shouldn’t go back and actually change it because it could cause some bugs and it could break people using that class and that kind of thing. But it makes sense because once you break things up well enough and you modulate well enough, it will always be maintainable.
Fr. Robert: Lou, there is that one extra thing, the interface. This is where people go, “Wait a minute, just taught me about abstract classes, why do I need interfaces? It seems like they do the same thing, right?” And the interfaces are just another way to enforce hierarchy, but why are they separate from abstract classes?
Lou: Sure, so interfaces are basically used to, like you said, used to enforce a scheme, what they call a scheme or signature, or force you to use a bunch of properties and functions, but it forces you to implement all of them. So instead of just giving you, you know, a couple of them, and forcing you to implement them, now you have to do all of them. So like if you show my screen real quick, I’ll give another example, is where I had the abstract class and some of them I gave you some implementation for, some with an email address I gave you some and then down here the “who am I” actually has the “return the email address,” but in some cases, let’s say I built an interface, which is just the same exact thing, just do “public, interface and the name,” and a lot of people like to prefix it with it “I” kind of like everything that Apple does is prefixed with an “I” well so most people prefix their interfaces with an “I” so you know it’s an interface. It’s just an old school way of doing it. And then you’ll notice down here that everything doesn’t have, it’s basically like the abstract version, it doesn’t have any curly braces underneath it, it doesn’t have any code inside of it, which means now if I wanted to create a Facebook version of this, using the interface, it’s going to make me implement all of them. So if I do “public class, Facebook,” and I do “I, social network,” again I get that little line underneath it and it says, “Oh, you want to implement everything?” and I could say, “Sure, why not,” and then everything that is in here, and you notice now it gives me code for every one of those members of that interface so that I pretty much have to put code in for all of them.
Fr. Robert: So whereas in abstract class you could have methods that are completed, right, they are not abstract, and then just have a few abstract members that need to be customized for a particular application. But when you’re dealing with an interface, there is nothing there; all it is is the signature, that’s it, done.
Lou: Right, a lot of people, it’s kind of a religious war when it comes to when you’re supposed to be using interfaces versus abstract classes, so we’re not going to even try to give you; you know, there’s some best practices around it, but it’s better to try and play around with them and see what the differences are, and then sometimes you’ll figure out, “Okay well in some instances I know the functionality and what it should be, the base, so I’m going to put it in there, but most of everything else I don’t know so I’m going to make the developer implement it.” And then in that case you know it’s abstract. An interface, you know, I don’t know anything, you just want to force them to do specific things like, login, like you know, return my email address. In that case, you could put, use an interface, you don’t have any code, you’re not forced to write any base code. And that’s a rule that I normally follow, it’s very simple, but it makes sense.
Fr. Robert: Yeah. Well Lou, I think we have sufficiently puzzled people for this episode, so how about this: how about for homework if we tell people to, if you’re a beginner, and if you are truly lost, then what we would like you to do is take what you have just learned about classes, and take some of the code from the first module, which again, is available on our Get Hub, and put it into a class. See what happens when you put it into a class inside of the Visual Studio IDE. Just so you can get comfortable with using a class, with declaring a class, with calling a class. Do that; if you’re feeling lucky, or you are more experienced, go ahead and play with abstract classes. Take some of that same code, drop it into an abstract class, and also add abstract members. This is an incremental learning step, folks, you may not get it immediately, but as Snubs said, sometimes when you see it, and you see the IDE actually telling you what you can do, it kind of clicks.
Fr. Robert: Yeah. Now Lou, all of your code is going to be available on our GitHub, yes?
Lou: Yes, definitely.
Fr. Robert: Fantastic. What do we want the folks to look forward to next week? How are we going to puzzle them then?
Lou: So we are going to build upon the idea of classes, but we are start using what we call new collections, collections are ways of doing things. So in the past I think we learned about arrays, and I think another language you learned about was lists and array lists and stuff like that? So we are going to build upon that inside of C# because we want you to understand there are other objects or types that allow you to build lists and allow you to loop through them easier and build upon your class and more functionality. So we’re definitely going to go through that. Another thing too that I want to go through is there is something called a package system in Visual Studio that allows you to download binary really easy, it’s called Nougat, and so I want to go over what that is and basically allow you to use that to make easier to kind of bring in third party functionality into your app a lot easier. And that will kind of build up what we’re going to do next after that, so we’ll go over that next week.
Fr. Robert: The first time I used Nougat I was like, “Is this legal? I shouldn’t be able to do this, right?”
Lou: I’m stealing people’s codes.
Fr. Robert: Exactly, I’m like, “I don’t think they wanted me to be able to see what they programmed.” Lou Maresca, we want to thank you again for being our code warrior, thank you for walking us through some difficult stuff.
Shannon: Yes, thank you.
Fr. Robert: It’s easy to code sometimes, but the concepts, remember folks, the concepts are the parts that we really need you to learn and these are universal concepts, this is not just in C#. Can you tell the folks at home where they can find your work?
Lou: You bet. It’s Lou MM on Twitter and of course, as always, CRM dot dynamics dot com is where all my work is posted as we ship it.
Shannon: Thank you, Lou!
Fr. Robert: Thank you, Lou, we’ll see you next week and well done, code warrior. Now folks, we realize that this was a lot of material, and we know that you are going to have to have fill in material, that’s fine, that’s okay, but you can always find out more about what we have done each week by going to our show pages. And Shannon, where can they find that?
Shannon: TWiT dot TV slash code.
Fr. Robert: Code. Break coding, yeah. And as long as you’re there you’ll also be able to get the link to our GitHub; our GitHub is where we are going to have all the code from this episode. We didn’t have any code from last episode because honestly, there was no code. But this week we are going to include the examples, include Lou’s code, and we’re also going to link you back to the GitHub repository from module one. So if you want to see some simple C# code, stuff that you can just drop in your console and make work before you start getting into getting busy with classes, go ahead and drop over TWiT dot TV slash code.
Shannon: Also we are on iTunes and YouTube!
Fr. Robert: Woo hoo!
Shannon: YouTube dot com slash TWiT coding one zero one and you can search for us on iTunes: definitely check it out, please subscribe it helps us out and it helps you guys get more shows.
Fr. Robert: Yea!
Shannon: We really appreciate it.
Fr. Robert: Yeah we really like that. You will also be able to find us in the socials.
Shannon: The socials?
Fr. Robert: The socials, yes. The biggest social that we use of course is G Plus. If you drop into Google Plus and just do a quick search for Coding 101, actually did we set up a…
Shannon: Bit dot LY slash coding one zero one.
Fr. Robert: That’s right, you set up a shortener. One more time.
Shannon: Bit dot LY slash coding one zero one.
Fr. Robert: Go there and join because it’s not just beginners, it’s not just experts, it’s not just people who just tinkle around with this or do it professionally, It’s all sorts. That’s what great about the community. If you have a question, ask it; if you see a question you can answer, go ahead and answer it. It’s part of the development of talent, for homegrown talent for coders around the world; this is what it means to be a code monkey slash code warrior.
Shannon: I love our community, you guys are awesome.
Fr. Robert: Yeah, yeah. Now we’re not just on the G Plus, right?
Shannon: Nah, I think we’re on the Twitters, too. At Snubs.
Fr. Robert: We keep defaulting to the southern thing. Snubs, Snubs, at Snubs. And you can find me at Padre SJ, that’s Twitter dot com slash Padre SJ. Go ahead and follow us, and you’ll find out what we’re doing each week, on every episode of Coding 101, plus, you’ll find out what we’re doing when we’re not doing this show. I’m all over the TWiT network, Snubs is on Techzilla, she’s also on Hack 5, she’s everywhere, so follow us on Twitters and see what we’re doing when we’re not being code monkeys.
Shannon: We’re always code monkeys.
Fr. Robert: We’re always code monkeys.
Shannon: And definitely check out our live stream, you can watch the show live on Thursdays at one thirty Pacific time, and that’s over at, what’s our website? Live dot TWiT dot TV?
Fr. Robert: Live dot TWiT dot TV. Find us every Thursday one thirty Pacific time and don’t forget as long as you’re watching us live, drop into out chat room at IRC dot TWiT dot TV. We do take questions from the chat room all the way through the show and it’s a way for us to keep out finger on the pulse of are people getting lost? Do they want us to move on? If you want to be part of the lesson, make sure you drop in and be part of the experiment that is TWiT TV. I think that’s it.
Shannon: I think so.
Fr. Robert: Well until next time, I’m Father Robert Ballecer.
Shannon: I’m Shannon Morse.
Fr. Robert: End of line!