Locked lesson.
About this lesson
Let's begin a discussion of object-oriented programming so we can create our own data types.
Exercise files
There are no related exercise files for this lesson.
Quick reference
Intro To Classes Part 1
Classes are Blueprints for creating our own Data Types.
When to use
Use them whenver you want to model something from the real world in your code.
Instructions
Classes allow us to create our own data types and objects.
Hints & tips
- Classes let us create our own data types
- Classes let us model real world things in our code
- C# Is an Object Oriented Programming Language
- An Object is an instance of a Class
- 00:04 Okay, in this section we want to start talking about classes in object oriented
- 00:07 programming.
- 00:07 Now we're not going to get into this in great detail,
- 00:10 because you can have an entire course on object oriented programming.
- 00:12 I just want to explain the basics of it, show you how to create classes and
- 00:16 use them, tell you what they are, and give you a basic understanding of them.
- 00:21 So in this video, we're not going to actually do any coding.
- 00:23 I just want to spend a couple of minutes talking about classes and
- 00:26 object oriented programming, because this actually confuses a lot of people.
- 00:30 And it doesn't have to, because it can be very simple, if you just understand some
- 00:34 basics, and so, we're going to talk about in this video.
- 00:36 So up until now we've been doing procedural programming,
- 00:39 functional programming.
- 00:40 You start at the top of your code, read one line of code,
- 00:43 then go down to the next one, down to the next one, and
- 00:46 just execute line after line from top to bottom, functional programming.
- 00:50 And yeah, we have methods that we can call and do things, but
- 00:53 it's still very linear from top to bottom.
- 00:55 With classes and
- 00:56 object oriented programming, we're doing something quite different.
- 01:00 We're actually creating classes,
- 01:02 which are basically blueprints that allow us to create our own datatypes.
- 01:07 And this is very exciting, very interesting.
- 01:09 So, think about this, if you wanted to model something from the real
- 01:12 world in your computer program, how would you do it?
- 01:15 So imagine a hat, you wanted to create a model of a hat in your program.
- 01:19 What datatype would you use for that?
- 01:22 It's not really a string.
- 01:23 Hats could be red, blue, green, they can be large, they can be small,
- 01:27 they can have different styles, right?
- 01:29 A string doesn't really encompass what it means to be a hat.
- 01:33 You can have one hat, or two hats, or ten hats.
- 01:35 So, they're kind of integers, but
- 01:37 that doesn't really encapsulate what it means to be a hat either.
- 01:40 So, we need our own data types to describe a hat, and
- 01:43 classes allow us to create those datatypes.
- 01:46 We can create a blueprint, a class for
- 01:49 our hat that describes all of the things that it means to be a hat.
- 01:53 Then we can create an object, an instance of the class of a hat.
- 01:58 So, we can create a hat object that is an instance of that blueprint in that class.
- 02:04 So, it may be hard to visualize all this and
- 02:06 we're going to walk through it all in the next video.
- 02:08 But it's actually not much more complicated than that.
- 02:11 So, imagine we have a car class, right?
- 02:15 We define what it means to be a car.
- 02:16 And then we create a car object with which is just an instance of our class, right?
- 02:21 The class is the definition.
- 02:22 The object is when we create one of them, right?
- 02:26 So, we've defined what it is to be a car, we create a car object.
- 02:29 Now that object can have attributes, and it can have methods.
- 02:33 An attribute is like a color.
- 02:35 What color is your car?
- 02:36 Is it red, is it blue?
- 02:37 How many doors does it have?
- 02:39 Does it have tinted windows?
- 02:40 Very descriptive things, those are attributes.
- 02:42 The car can also have methods.
- 02:44 The transmission system, that's a method, right?
- 02:47 It's the whole system.
- 02:48 The engine system, that's a method.
- 02:51 You put in inputs, you get outputs, it's a method.
- 02:54 The air conditioning system that's a method, right?
- 02:56 So, we want to designate between attributes, colors, sizes,
- 03:00 descriptive things and methods,
- 03:02 which we've already talked about a little bit in the past that do something.
- 03:06 So, you have a class that defines what your thing is.
- 03:09 You create one of those things that becomes an object.
- 03:12 That object can have attributes, colors, sizes, things like that.
- 03:15 It can also have methods where it does stuff.
- 03:17 And all of this is sort of used in the same way our datatypes were used in
- 03:21 the past when we define something as a string, well,
- 03:25 now we define something as a blue car, right?
- 03:27 It's an object from our class.
- 03:29 So, this is overwhelming, this is a lot of stuff to take in.
- 03:32 And that's what I wanted to sort of talk about it a little bit in this video, and
- 03:36 I hope I didn't confuse you.
- 03:37 Because it's actually a very simple concept once you see it in action, and
- 03:40 which you start to see how powerful and how cool these things really are.
- 03:44 So, that's all for this video.
- 03:45 In the next video, we're going to jump right in and build our first simple class.
Lesson notes are only available for subscribers.