JAVA OOP, Part 1: Classes and Objects | Vyara Georgieva | Skillshare

JAVA OOP, Part 1: Classes and Objects

Vyara Georgieva, Technical Trainer & ESL Teacher

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
6 Lessons (44m)
    • 1. Course introduction

      6:11
    • 2. Introduction classes and objects

      5:42
    • 3. The building of a class - fields

      7:43
    • 4. How to print an object

      6:44
    • 5. The building of a class - constructors

      13:38
    • 6. Chaining constructors

      4:13

About This Class

This is a series, covering the basics of OOP programming in Java. Welcome to its 1st part - Classes and objects!

Once we cover the idea of classes and objects, you are to be introduced to the basic OOP principles - encapsulation, inheritance, abstraction and polymorphism, The lectures are structured in a way to let you move in pace with the instructor, applying everything you have just seen by yourself. The logic of every piece of code is to be explained in details. Once the theoretical part cover, you'll complete 3 more projects, by yourself, as I'll be guiding you on the way. Nowadays, every programmer needs to be aware of what OOP is and how it is applied - so, why not making the first step now? 

Transcripts

1. Course introduction: Hello and welcome to your newest challenge. Joppa. Object oriented programming. Basic Principles. Course. My name is Vera Andi. I'm here to guide you on your way to understanding the key concept. Object oriented programming is based on the language. We're going to use its Java. But please keep in mind that in discourse we'll talk about universal ideas about concept that can be used in different sin, taxes and different languages. So it's not a course about the syntax in Java or how something is done strictly in Java, but how something is achieved in programming in general. Let's see how the course is structured and, as you have already seen, of course, just to say the game. It is not, of course, for complete beginners on by a complete beginner. I mean, somebody who has never, ever written a colt in any language, because here we're not going to start with what of arrival is or how data is stored, or how the iterations within a loop function or what on if close here, we'll focus on the principles off object oriented programming, starting a step before that, the course itself is separated into different models into different parts. The 1st 1 is the theoretical one. The 2nd 1 the practical in the theoretical part, will pass through the whole theory. You'll be provided with various examples on each of the examples will be explained in the So you'll be given the chance to practice absolutely everything we have covered in the lecture. In the practical part, you will be given the opportunity to do by yourself three different projects in orderto practice again, everything we have passed because programming or coding can be learned on Levi, according not only via listening. So please very mind in order to succeed not only with this, but with any other program in chorus. Just put yourself into coding the mori coat. The better you become, what exactly we're going to talk about in the first model or in the theoretical model, we're going to speak about classes and object before starting the four basic principles. Why? Because it's impossible to talk about either encapsulation or inheritance if you have no idea what the class or an object s, Once these basic concept clarified, we're going to talk about encapsulation and inheritance and obstruction and pulumur if ism . The theory covered comes the practical part in the practical part. As I have already told you, you will be given three models or three different problems that you will need to self. You're part here is toe. Take the instructions carefully to listen to my explanation of the instructions on from their own to start coding by yourself. Off course a gent. You'll be having the court, my cold as a reference on, We will solve the problems together step by step. So before moving. Come on, let me make several more notes. The environment I'm going to use is intelligent. You may opt for anything else. If a custom still no problem, just have it installed, please. As we're not starting with installation, the second very important note within the theoretical part. I'm not going to write the coat in front of you one because writing the coat makes you focused on the cold itself and not on understanding it. If you watch me write something, it will not make you better. But if you listen to me explaining everything step by step, you may catch the logic on. You may write the court by yourself. Off course. Each of the examples we're going to look at you may find at the end the court, as their reference on it, gained my strong advice. Just toe code it by herself to write the coat by herself. Okay, ready to start. Well, let's do it. 2. Introduction classes and objects: the 1st 13 going deeper. We need to clarify some basic ideas or the actors behind the stage Alexe it. So what is the object oriented programming rule grooming that is oriented? The words open, right? But Optic happened to obey instances off classes. So in order to be able to speak about the principles off Opie for object oriented programming, we need to know what the class is and what an object iss Teoh. Let's start from there. What classes are what objects are. What is the correlation between them on how objects depend on classes and how classes model object? Let's go to the world around us. Just look around what you will see our object object you're surrounded by every day. Here is your laptop or your cat or the mouse of your computer. These objects you may touch and they have their specifics. They have their characteristic, you know, for example, the cover of your laptop. You know the brunt off the mouse. You know, the breed of the can't and someone these air specific characteristic Andi goingto programming. Now, in order to resemble what we have in our real world, we need to great the same models, or we need the same object to be recreated in programming. In order this to be done, we need a new obstruction. That Elise beyond that is above the doctor and this abstraction exactly our classes. When you hear a class, think of it as a model as, ah, glow print. The object is based on. If we speak about the laptop, for example, this is the exact uptick that has its brunt. It's perimeters, it's cowers on and so on. Behind this, there is a class computer or a class laptop that hold, Oh, the general characteristics each and every laptop has. So the class is just a model, a blueprint, and the object is created based on the characteristics we define in our class. In order this to be clearer, let's look at the full wink example. You know him right? It's don't so has some specific characteristics he walks onto pose, for example. He has great bluish for he always Jesus, Derik and so on cue may speak. Actually, all of these characteristics turn him into a new, unique object. But apart from Tom, there are a lot of other can't on these cats. Share some common characteristics. So these common characteristics are exactly the ones that appeared to be defined within a class. Look at this picture. What do you have here? Just a see what? Without any specifics, we can't see even the FERC. Our but we know that it is dear. We know that this he has probably a name that it has probably on agent sold, but nothing is clear here. So this is our class cat and the previous one. Tom was just on instance just a creation based on everything we have defined within our class again. Look at that. The class gives us the obstruction. The class gives us some general characters. And based on the class, we make great various objects. The object are being attributed. Some values Andi. On this way, we may say that this object has that specific characteristic based on what was pre defined within the model. The object is based on hope. It's a little bit clearer or no. I only see how old that is achieved in practice. 3. The building of a class - fields: all right. So bearing in mind, everything we've spoken about. So for let's see how, actually ah class is clear and how it is structured within a product. Open your environment, please. As I told you, I will be using the intelligent E for the course. Examples also asked for the problems we're going to solve after that to have in mind that it's always, ah, good idea. And it's one of the best practices to store every class you declare within ah separate dot Java file or, in other words, in different files. Different classes, one class Onley in 15 The first exercise will do is to create ah simple project, the aim of whom is the print some cats, some objects can, and in order to put we're define took classes. The 1st 1 will be cat or here we will add the description off our class. And the 2nd 1 we'll called pace here. We'll declare our main method. Onda within its body were going toe in Stan. She ate the cat class. We have already declared all right, well long. In order to create the can't class, we need to define field when you hear feuds in programming. Think off them as placeholders or as such kind of structure that keeps our data. Actually, these are just varietals that think the values attributed to them. So when you hear a field, think of it as a place where we store the data for our class. Andi, remember, please, for each and every class, it's obligatory to have few because a class can't exist without its data. As we're going to talk about cats once again, look at the C word of a cat and think for something in common for all of the cat again class. If that structure which those hell these are the characteristics that everyone derived from me needs to have, or in other words, these are the characteristic without which a cat can't exist. Probably there a lot. But let's say for now that we need the name, the age and the cower, the first hour of the cat. So for each off the characteristics, we want to receive some data. We need toe attribute a specific for you. Please don't forget the following. We have different cat each of these cats. He has some characteristics. These characteristics can be different But there is something universal. And this universal thing is the data holder which tells this cat needs to have this this and that. So this is what we're doing at the moment we're creating that based on which each of these three cats can be instance, she ate it So within the public class, public labored for now we'll talk about access modifiers a little bit later within the body off the public last cat. We define our field before the definition. We need to think, of course, what kind of arrivals we need in order to store the information. We need the name we need this drink, the age. Let's say that we don't require precision here. So on into Durwood of the work on the for cover needs to be stored in a strength again. And this is what we do. We just declare them drink, name, drink, very cower and in h that's it. We have the fields we didn't Our in class once defined the field. What we want is to create a cat, a game on object. Ethan instance of the class. The object is created based on the class. How it's done within the main method off the best class that we created. We invoke the class, the class cat. We give this instance a name in this case, my cat Andi. After that, we involved the constructor of the what is sick of stroke there. What types of constructors there are and how they are involved will speak a little bit later. But for now, please remember, we have can't or, in other words, invocation of the class we have name. This is the name off our Arctic or off our unique instance from the class cat. And after that we have new the key word new and this is actually the constructor already we have you object, but apart from that, we want toe attribute our object field some values how it's there once t object declared, we use the dot annotation on On this way we may attribute values to the field that have been declared within our class cat. So we want, for example, our first optic to have a name. Mike. The cower off it's for to be brown the eight. The victory on different lines. We use the name of the object we created on a via the dot notation. We invoke the different field and attributes them Vandals. That's it. Ready? So let's move on and see how these can be printed in the console. 4. How to print an object: Once we attribute id the values to our fields, let's see how the object itself can be printed. Or, in other words, let's examine how the toast rink made it work off course. First we try street forward in the class. We invoke the print line method and give us a perimeter. The name off the object. We have just declared my cat what we see in the console. However, it's not the result. We exactly expected what actually happens in jungle. Every club can tow strength method by default. On this, just drink metal refers dough the dough strength method off the super class off old Java classes. This is the object class. So we have optic class Andi under the object cross our old, the other classes. Still, each and every class can tow strength method on what is returned by this string. Metal is the following. It's big. The name of the class the object belongs to Can it takes at Andi, then the hash coat of the class. So this can caffeinated drink is what we're given as an output. But what we want is something different. Or in other words, we need to find a way to change the the food behavior off the tow strength matters, quality, how it's done when it comes to a change off UN already defined behaviour off method In programming, we speak about method overriding toe overwrite. A method means to change the way a method behaves change its functionality in the context of old P. This happens in case when we have some methods in the supercar. On the overriding is an applicable auction in the sub classes. What this exactly means, what is a stupor and the subclass? How the relation between them is formed and so on will speak about a little bit later. But if curious, just check in Google. What inheritance is to have a basic idea? What's important for now we want to change the behavior off on already defined method. What we need to do is to override it. Let's go toe the class camp Andi here we will override the two strength matter. Please remember this combination in intelligence, it's really useful. Each and every environment has something like that. But please check what the alternative for your environment is. So the out and concert invokes the blocks that can be out of generated. What we want to be out of generated for us right now is the toast. Drink matter. So what we do? We praise the toast drink method and then ah, different dialogue appears in this day. Evoke. We may choose what kind of information we want to be printed. Once we try toe, bring our Achter in our keys. We want the whole information we contributed to be printed. So what happens? We mark all off the field grid's enter on that the result at what we have just then what actually is done here? The default to string method is overridden. You see, even there's an annotation telling you what you have done on the output is formed by the concatenation off some picks waas the names of the future. Off course you can change the way your output is, Vermont it for now. We'll leave it as your idea is just to show you how you need to print your object. And of course, when we turn back to the test class and try again toe printed the object we have already created, the result is different. We have it formed exactly on the way we still? Right now we have the name, the first color and G eight police have in mind. Ive need it, Teoh, bring just a different heuristic Police have in mind that it's just a different characteristics is necessary to be printed. It could be done really easy. We just ad the name. Uh, I were hope. Dig followed by the dot annotation Onda we say Which property? Exactly. We want to print. That's it. The output is what we want it. So congratulations. Now you may attribute fellows. Andi, you may bring your objects in the console. Let's move on. 5. The building of a class - constructors: so for we have spoken about what? The class, What fields are how toe define the fields in our optic. What is an object? How to bring the object and how toe override some little It's time to move on. Andi, go on specifying what our class looks like. So the first building book we have spoken about are the field, the 2nd 1 the constructors. But before going back to our project, let's see a simple example. You know what? This right Everyone has ever played or theme at least Ah, Lego constructor or a constructor off any kind. What specific about these constructors? Well, you're usually given different pieces of blokes. You'll combine them in a way or another. Andi, based on the way you combine them, different figures appear when we speak about constructors in programming. Please keep these big jurors in mind. Our building books are actually our fields. Where is the pictures that appear in a legal constructor? Are our hope to so based on the way we combine the fields were given the opportunity toe output, different objects? That's the role of focal instructor in a simple words. Just a example. That's how Ah constructor looks so we have. For example, we are in the same class. Can't So we define the constructor with exactly the same name. The class it is in is defined. So in this case, cat After that the constructor is always followed by Curly. Break it on. There is a border. Think of a constructor as, ah special metal method that doesn't have a return type method that is a little bit more specific Just because it came the idea with what it was created Waas toe in Stan she ate new object Andi Because of its idea, it may be with or without perimeters. The values could be attributed within the body of the constructor or within the main method . The variations are different. So let's stop and have a look at each of them in details What we have so far. That's how our class looks. We have already declared our few. We have overridden the toast drink method in order to be able to print each of our optic. What we want to do after that east to add the constructor. But keep in mind even in this situation, when we haven't declared the constructor ourselves there is a default constructor. Each and every class cast a constructor on and a class without a constructor cannot exist if we define our own constructor or our custom constructor. The default constructor is over rhythm and doesn't exist anymore. If we don't define a constructor as in our case, we just use the default one. And this is what actually happened. The outer generated constructor or the implicit default constructor Looks like that exactly on the same way as an empty constructor. So again, this is the fourth constructor, the one that we didn't define but was defined for us. And here Exactly. We invoked it. I told you before, When we create a new object, actually we invoke the constructor in order the object to be created with the keyword new. So this is what we do. We create a new instance of the class with a specific name. Andi. In order to create it, we invoke a constructor in our case, the seafood constructor via the keyword new and calibrated Perfect. So let's he know what would happen. Ive we creed another object based on our class with a different name. Of course it's mandatory. So Let's go with Tom Cats. Onda. We want to print the default values of the field. The difference with our previous my cat object in the my cat we attributed develops here. We're not contributing anything here. We're invoking them via the dot annotation Onda. We want to see what the output off each of these fueled will be. Here you are there is actually always available that is attributed to a field. The values that contributed differ based on the type the field s so it can be No, it can be zero. It can be something else. Just test for yourselves. These air the before values off of arrivals in Java. This is what you receive as an output. All right, move on. If we want, we may create on empty constructor, the empty constructor and the default constructor, as we saw earlier. Look exactly on the same way. But the difference is enormous. The difference is that when we don't define a constructor on our own, then the default one is also generated. But once we define a constructor of our own, we can't use the defaulter By contrast, the empty constructor or the one that we define can always be invoked and can always be used. It's a big practice when you write your own court to not depend on the default. But your own empty constructor defined another way to attribute values. Instead of doing this within the main method. As with it in the previous section, you may attribute the values for the fields we think the body of the constructor. So I hear you say that the name should be given the H three the cover of the for black on After that, Once you print the instance, it's a new instance that we're creating here. So once you print the new instance on the console, you see the output that you declared within the body of the constructor. All right, recorded its right. Good. Next time you noticed that we're using the word this. So what does this stand for? This refers to the current instance of the class to the current object. Think of it on this way. I am creating a new hope. Took The object is Devo cat Andi then for the optic Devo cat or, for the current instance. Okay, the field name and attributed this well or attributes did the value deVeau for these instance are for these objects. Give Moquete, take the age field and contribute the valedictory. And so so this is the this if this nothing difficult but really useful. And it's very important to understand that this is the current instance of the class. The current object We're creating the most common kind of constructors, actually, or the one that is most commonly used. He's deconstruct er with partner makers. What did me within the can't close. Be clear another constructor And this time give it as perimeters. Two of arrivals drink, name and in each we think the body see that the value of the name field will be equal to the value Attributes it to the perimeter name and the value of the H field off the class will be equal toe the H value attributed to the perimeter. H How is them? We are creating a new cast, Andi, for this class, we want to use exactly this constructor neither the default nor the other one. We have already declared. So you are given a hint by the intelligence. What kind of values you are expected to enter have a look at what appears you are printing the output. So you're defining the name you're giving the age of value. But the four Kaur is also defined. Why? Because it's a non written rules that in a constructor all off the field has to be given some kind of value. Otherwise, biggie photo panels are what are attributed to them in order this one to be attributed, What we made Oh, we may use our constructor in a way that the name on the age are automatically given some values. And we see that the aesthetic value or developed, that will always be attributes did toe the for cower will be great. On this way the constructor structure, we may control what will be the output in the console all the time. So we created a constructor with two perimeters as we don't want the third perimeter to be taken dynamical But to be pretty fine, Andi, in order each to be pretty fine, we need to do it. Ah, a different attribution to the field. We attributes the fuel degree and indeed Gozo. When we create a new instance on use be constructor. We received the output as we expect that we have the value of the name as we want it. The cower off the four as we pretty find into constructor on the h This that we gave as a perimeter within the constructor invocation. Perfect. Now you're aware of constructors as well. Let's see what we may do in order to avoid coat repetition. 6. Chaining constructors : one more detail about constructors we haven't examined so far as you saw when defining constructors, they could be quite more than just one or two at the same time. Imagine a situation when our fields are not three, as in the current case, but 30. Or why not 300? It would required inconvenient, if not ridiculous toe rewrite the code all the time on all the time to be attributes eating the same values to the same fields. And Toby real writing this over and over again. You know that this one to be avoided, what is actually recommended to be done? We write the full quote on this way for the constructor. With most of the perimeters on from their own, we built our next constructors based on the constructor, we have already defined how it is done. We declare our new constructor again with the same name. And this is the so called constructors overloading so the same name, different perimeters. That's how the compiler can understand what kind of a constructor were using Onda. We give as perimeters the once that we need see the difference here we have a difference. Drink That hasn't been referred here, but the order off the para meters is exactly the same in the main constructor. We give the name the age the for cover in the second home. We give the name the age and the idea is here that we want Teoh directly attribute value toe the third Kara Meter and not to at the value that dynamical. So how it's done, we again use the this reference. But here this has a different meaning. We don't speak about on instance off a class here. Rather, we speak for our reverends toe on already defined constructor. So we think the body off the this reference, we see what exactly to be used in the invocation off the second constructor on the cold below. It's not necessary anymore. This is equal toe this definition. It's obviously better, more comfortable, more convenient to be used. So again, when is it used? It's used when we need not to rewrite the code Onda We based all off other constructors on the main constructor or the constructor with most of the perimeters. This as the term is called constructors chaining on its available in Java. Well done. Congratulations. You have just completed the first section off your course. You've already learned a lot, but actually now we're starting with the principles of the object oriented programming. Get ready and let's continue.