Master Java : Object Oriented Programming in Java | Lukas Vyhnalek | Skillshare

Master Java : Object Oriented Programming in Java

Lukas Vyhnalek, Microsoft Employee, Programming Teacher

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
5 Lessons (30m)
    • 1. What is OOP

      2:57
    • 2. What is Class ?

      2:14
    • 3. CODE | Class and objects

      8:55
    • 4. Inheritance

      3:35
    • 5. PROJECT | Animal Farm

      12:45

About This Class

Save your precious time by buying this course. You will learn how to program in Java in a fast and easy way!

This course is designed to help you start with programming, I explain important concepts such as Variables,Conditions,Loops or Functions. Then I show you how to use these things in Simple applications. Almost every programmer started by creating simple applications and almost every programmer is now earning decent money.

You don't need any experience in programming to take this course.This course is designed for complete beginners. All you need is Computer and ability to learn.

Transcripts

1. What is OOP: look around. What do you see? Objects. Everything around us, this object a glass of water, Its object computer is object. Smartphone is object. Even you are object and almost every object have some attributes. For example, the glass of water can have attributes indicating how much water is in glass. You can have attributes like gender H color of your hair, and your computer can have attributes like operating system brought, says sir. Also, Galler of computer is in that reviewed and many more. Also, you can do stuff with objects. In computer science. It is guilt method. So, for example, you can drink from glass of water and by drinking you are changing its attributes, indicating how much water is in glass. Also, you can fill the glass with water, and once again you are changing. Get tribute, but you don't have to change attributes with each method. For example, if I tell you to make a sound, you can make sound like Ah, and you are still male or female and another important thing. ISS. You can have more objects off same type or class if you want. For example, I bet my glass of water is not only one in this entire world, there will be other glasses. So, for example, if I drink from mine, it only changes the attribute off. Mine. Glass of water. Your glass will stay the same when you program in object oriented bargain things works pretty much the same. So you have different dives off objects. You can have more objects off the same time, almost every object have some attributes like its color or something like that, and almost every object have methods. And these are just things you can do with a certain type of object, like make sound Almost every human can make. Sound methods can change value off object attributes, but they don't have to, and that's pretty much it. I will see you at the next video. 2. What is Class ?: a non object oriented programming. Computer programs are designed by making them out off object that interact with one another . Their significant diversity off object oriented programming languages. But the most popping wants our class based meaning that objects are some instances off glass in Java and other programming languages like C Sharp or C plus bus, and many more is used. Glass based system. Other system used, for example, in JavaScript, its prototype based that in this girl's I talked about Java, so I will talk about glass based system examples. Off glass is a human other class can be computer, so an object oriented programming when baby is born, it is a convivial int to grading new instance of human object. The structure and behaviour oven object is defined by a class, which is kinda definition or blueprint off. All objects off a specific type and object must be explicitly created based on a class and an object created vice. Some class is considered to be an instance off that glass, so to sum things up, glass is something defining attributes and methods off object. You can create more objects using one class, and these objects have attributes off this glass and can perform methods from this glass. Also, objects created Vice. Um, class are coat instance off that glass, and that is pretty much it. In the next lecture, I will show you how that works in code. 3. CODE | Class and objects: So in order to create new class and that beans, you need to open this project step and find your project. Then all you need to do is click with right button on this name in our example class and objects. And then for new and year, you can see Java class. So click on that and then net beans ask you to choose glass name. I will just write my new class. One more thing is that the name should start with uppercase character and hit finish and you get here. So Manu class is generated and I can create new objects off this glass. So let's get back to our project. First glass you can see is this one called glass and objects. This one contains the main function. That is why there is this little play button. I don't know if you can see underneath. I have other class that I created just like this one and I called it dog. So this glass represents all instances off dark in my program. Let's look how it is implemented. First we got attributes off every dark in this implementation. I have name and age. Obviously there can be more attributes, but I wanted to keep things simple. So yeah, Then there is something called constructor that is function that it's called when you create new instance off this glass and it is used to kinda initialize the attributes if you want, so it works just the same as a function. You pass name and age of your dog and it returns you new instance off dark. Other thing is that constructor must have these same name as our class name and I will show you how it it's got in a minute. Other thing is that you can have more constructors off, same name. It is useful thing. Let's say that I haven't decide what name I give to my dark. So I create new instance of dark with some default name and defaulted age and computer is smart enough to know which constructor call because off arguments you pass. If you pass to arguments one of type string and 2nd 1 off type in teacher, this constructor will be called. But if you call constructor without arguments, this one will be called Also, you are probably dying to know what this this means. This keyword represents a reference to the current object. It's most gallantly used an object oriented goat. So that means this object this instance this dark. Remember how I talked about my glass and other glasses in the universe? If I drink from my glass, it will only have effect on attributes off my glass or this glass. So this represents the object over which the methods are called. So this that name refers to my dark object name and this that edge is referring to H at the boot off my dog. Another thing is these keywords. You maybe remember them from functions when we use public keyword. This give erred defines who can kill and who can see this method. These are a couple of options you can write private keyword and leg. You think only the class in which it is declared can see it. So in our example, only glass dock could see it are one is protected. That means you can see it in the glass. So in our example, Doc, but you can also see this method or attribute. These keywords can also be defined two attributes by default. Every attributes is off protected type and what protected means is it can be accessed from hold back it so from every class in our project. So in our example, it is the glass and object glass and my new glass and last one is public and that means everyone can see it. You can use these protection levels to protect somebody from accessing something they shouldn't success. Okay, so then there is something pretty much the same as function. So the 1st 1 is visible to everyone and it it's got get name and it returns a string value and it is just name of our dog. Then there is method gold set name where you best new name, and it is set as a new name to our dog. Same with age. These methods are called Gethers and setters because using them, you can either get value off some attributes or said value to some attributes. And last method is got make sound and it just brings name of our dog, says Woof, Woof. Now let's see how you can create instances of this glass. So I go back to our class and objects glass and here in the main function, I will create new instance off dark and it's gonna lie declaring a variable. So we got the type, Then there is name and then we sign it to new dark and in parentheses we pass name and date as arguments. So the first constructor will be gold and it returns us a new dark with name Ben and H four and online under. I call the Constructor without argument, so these parentheses are empty, but there is still the new keyword on the next line. I printed the name off my first dog and is its age old. So you can access attributes or method off every instance off some class by riding the object name that attributes or method. And this object is going to set as this variable that you use in class methods. So it is the current object over ridge de method is called and here I am, demonstrating the protection levels. I access the name directly Why? It's attributes since it is protected. And I can do that because I'm in the same package and then i x is the h by method, get H. This method is public, so it will work anywhere. Then I print the same way my second dog. So you believe me. The second constructor, what's called and not a method you can go is the make sound method, and we come methods pretty much the same as we call functions with method. There iss only additional object over which you call the method so that it's a lot of information in one lecture. Feel free to watch it again if you don't understand something or just ask, and I will see you next time. 4. Inheritance: an object oriented programming inheritance is when class is based on another class. Using the same implementation inheritance in most class based, object oriented programming languages is a mechanism in which one object acquires all the properties and behaviors off the baron's object, and it is very useful thing. When you set some class as a parent glass to another one. It's important to ask yourself this simple question. It's killed. The is a rule. The child glass name is a barren glass name. So, for example, dark is animal. It's correct, right? Because dog is an animal about. For example, conference room is a building is not correct, so you shouldn't in her it from building class. Another example is that Ford is a car. This one is also incorrect, so you cannot inherit from car class because Ford is not a car. Another important drew is the guests rule, and what that means is, keep it simple, stupid, and it is really useful. You shouldn't inherit whenever you can or whenever is the it's a rule. True, so example of class hierarchy is year on top. We have glass animal, and then there are It's Children's so dark take get, and there could be bunny more all of these glasses in the hurry. It's from animal class, so every attribute and non private methods that animal half are also an attribute of dark bake and get. I think that when I show you coat, this will clear up. Other important thing is method calling Sometimes can happen. That dog have method with same name as animal, and computer solves it by executing the first method that comes around. So let's say that both dog and animal have method gold one year older. If you called his method over dark object, the method inside duck class will be executed. And another important thing is, let's say, dark class doesn't have method gold one year older but animal class do. And since arc in her, it's from animal glass. If I call one year older method over Doc object Computer looks into dark glass and finds out that Derris no method killed one year older. So what computer does? He tries to find his method in its barren, so it goes up into an M a class, and it finds the method one year older, so he executes it in the next video, I will show you how that works. If you have questions, just ask and I will see you next time. 5. PROJECT | Animal Farm: this thing might sound confusing at first that it will get used to it. So I got here a few classes. Let's start with animal class. So in our farm, animals have name and age. And also there is this method gold make sound. And if you look at this line, it says this method should be over it. And in dark, big or lying class. What that means is that disk class is parent class to pick duck and Lyon and in thes glasses should be defined method called make sound. That way, this method is never called. Now you might think that if it is not killed, we don't need it right. Where'd Lin? No, and I will explain why in a minute now, let's look at dark class at this line. You can see it in her. It's from animal, and you can say by this public glass dock extents animal. So if you want to inherit from some class, you write extents in chavs glass. Then there's constructor and notice that I am referring here to animal attributes, name and date about. That is okay because dark in her, it's from animals. So it has all methods and attributes animal half. Then there's this override, and what that means then, means this method overrides method in animal, and then there is not over it and method. This one is too string, and this is bid weird. When you create class it automatically in her, it's from object. Glass in Java and every object in Java have method do string and by defining method to string re overriding method on object glass object class is the route off the class hierarchy. Every class as object as a super class, for example, even the race or string. Implement the methods off this glass, and one of the method is to string. Let's move on to next glass and did this big glass is similar to dark, so every picked object in her it's from animal. But it also have its own attributes. Gold weight and also the constructor is a bit different because it also takes weight that that is the only difference in this glass in this class, I wanted to show you you can also have attributes in child glass. Sure, the weight should probably be in animal class because every animal have weight, but let's say that in our farm, only Biggs have weight. Docks and lions don't. So the rest is pretty much same. Here is over in a method called make sound and under you may notice is a bit weird thing. I also wanted to show you that you can do this when you create string, you can add string by plus sign Right. But what do you can also do is split the string to multiple lines. So here is the return key word. And then there is one string value Blust sign name of the animal. Then there is not semicolon at the end of the line. And on the next line we started with plus sign and then they're string variable and another string value. And finally, Sema Corn. So these two lines represents one string value for computer. So computer returns this whole string. Okay, let's look at lying class. So once again, it is pretty much the same as dog Onley. This time there is additional attributes Have you eaten today? But constructor takes only two arguments, name and age. And that's OK. It's only up to you. What arguments you take. So the constructor says the name and date, but it also sets the Have You Eaten today attributes two falls. Then there is method called just eight, and it says they have eaten today to true. Then there is over it and make sound method and over it and do string method. Another thing you maybe didn't know is that you can have multiple returns, statements in method or function. The important thing is that no matter what happens, method or function always returns some value. So in this example, lion, either eighth or didn't there is no third option. So one of these to return statements will take place. Now let's take a look at farm class, this glass and one attributes and in this array off animals and also noticed this glass doesn't in her it from any other class. So when you create new farm, you passed array off animals and they set as value to animals attributes. But why animals when we have dark Bake and Lyon well, if there would be, let's say, lion array instead, off animal array, the farm could contain only line objects that we also want their picks and dogs, and I hope you remember that in array every element at to be off the same data type. So there is no chance we could fit, pick or dark into lion or a. So that's why we have animals array and farm can also do two things. First, it's Brende. And what this method us we have near loop by now should figure out that dissolute goes through every animal in our animals beret and what it does brings whatever D to string method returns. So now notice. In animal class, we have no to string method, but in dark, Bake and Lion we do. And the thing is, even though we have dog object sends it in her, it's from animal class. We can add it to animal data type ray. This is important. So in this Ray can either be some animal or instance off any class that in her it's from animal. So these two string method is actually called over doc bake or line object. Since we are operating under the premise that in animals array, we have either dark bake or lion same way with this make sound method, this function makes sound. It's actually called over some dog big or lion. And now I get back to the thing I mentioned in the beginning. Why we have make sound method in animal class. It is because of this in the array could also be animal object. So if this method were not defined in animal glass, you will get error and that beans one let you run your rap now you might think OK, but the two string method is not defined in animal class, but we can still call it here. Yeah, that is because, as I said previously, the method to string is defined in object glass and every object in her. It's from this glass, even though it is not ridden. A detox like glass animal extents object. It is automatic. So the Met the two strings works even on this farm class, and we didn't wrote it here. But back to my point, this method makes every animal in farm make sound. Let's look at we can create instances off these objects, so here's the animal farm class, which contain main method. So here I initialize new array that can contain animals, and I tell the computer that I want space and memory for five animals So there is no animal in this ray yet. I only told computer that I want space for five animals and now I'm going to fill the space . So I create new variable called Greg Auf Die Bligh in and I said it to new lion. Instance. Then on this line, I can, for example, call method just eight over this variable, and then I can set it as a value on in the zero in our array. Another way I can add new animal into array is here, So I don't create new variable. I directly set the object do array. And as you can see, I have animal Ray and I insert here lion bake and dark object. Then I create new farm variable and I passed their disarray. So now this Reyes cloned and it ISS as animals attributes in my little farm object. Another thing is here I call to string method over my little farm. And even though this method is not defined in farm class, it will work because it is defined in object glass. Then I called the brain method over my little farm and then I call make sound method over my little farm. So let's take a look. How will it look like? So here is my little farm to string it. It returns the name off glass and then some Hesh coat, I think. But right now, I'm not sure. And it pretty much doesn't matter. All I wanted to do is show you that these objects really in hurr It's from object class. Even though we didn't explicitly say so. Then there is printed information about animals in farm. And then there is noise that every animal make, so, Yep. Um, that's pretty much what I just told you. A lot of information. So it is okay if you don't understand everything now, feel free to wars his lecture again, or just ask me. And with that being said, I will see you in except