Java - OOP Basics 1/5 (Class and Object)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Oh, this is good! I think I will sleep learn java.

👍︎︎ 2 👤︎︎ u/MacacoMonkey 📅︎︎ Nov 02 2018 🗫︎ replies
Captions
is a prerequisite lesson for those viewers who have little or no knowledge at all about object-oriented programming so if you've written simple Java programs in the past but get confused when you see more than one or two classes this lesson is designed especially for you so please don't skip it note however that I do expect you to already understand the basics of programming such as loops and conditionals and methods so if you don't know what those are I'd recommend going to a website called coding bat and getting some practice there now that site is a great resource for getting practice on loops and conditionals in Java alright so this section is intended to provide you with just the right amount of knowledge regarding object orientation there are some advanced topics we cover later in the course for which I'll present to you when the time is right but this prerequisite section should give you a solid foundation about thinking in terms of objects I use a very unconventional approach to teaching this so if you've had trouble learning this concept in the past you'll certainly get a new flavor from the way I teach this topic now if you're already aware of the difference between what an object is and what an interface is feel free to skip this lesson ok so coming over to object-oriented programming it's all about the objects just like how objects behave in the real world software objects have behavior - objects in software are what do all of the work in an application for example real world objects like cars accelerate people walk people can also be considered as objects planes flight fish swim and the world goes round and round similarly in software we create objects that have particular behavior or function and we ask those software objects to carry out that function and as a result our application runs as expected so when the application is started software objects come into existence and they do all of the work in an application so let's see an example I'll show you how to create software objects using Java and just follow along step by step as we go through this example I'll be using eclipse which you can download from eclipse.org but you can use pretty much any java ide or editor that you prefer alright so i've picked an example that we can all relate to we are all human beings and we carry out certain behavior on planet Earth if you think about Earth as a software application you'll be able to think of humans and other things in earth as software objects performing actions of course there's no way we can even begin to mirror the mastery of this beautiful universe through software but I think this example will help you gain a deeper intuition about object-oriented programming so let's create a tiny little world of humans now the way it's going to work is you first need to create a class and then we can use this class to create our actual objects that are going to do all of the work so let's first create a human class so I've already created this project and you right click there and you go to new class and we're going to create a class called human this class will allow us to create objects of type human classes basically contain instructions for how objects should be created and how they should behave in the application a class is basically a specification or a blueprint or a design of how humans are to be like in our application so using this human class we'll actually be able to create as many human objects in our tiny little world as we please so let's define the characteristics humans in our software will have will define some basic attributes to keep things simple so humans in our application are going to have a name we can say that they will have an age will give them a height in inches and we'll also let's give them an eye color so with these fields defined all objects that we create using this class are going to be are going to have attributes like age and height and inches as well as eye color great now rest assured that they will not have hair or ears or wings because that is not part of this class this specification we could have defined those fields too of course but this is of course going to be a simple example for our tiny little world that we're creating here all right so moving along we want humans to be able to do things in our application so I'll define some basic behaviors such as speaking and eating and work and walk so let's define these methods in this class humans will be able to speak and in here I'll just print out some statements that will basically give the values of each of these particular fields right so let's introduce ourselves hello my name is and then we can concatenate prepend the name variable at the end of the statement similarly I can print out the height in inches put out the age and we'll also print the eye color similarly let's define some other methods in this class we'll make them eat by the way I'm using an Eclipse shortcut when you type in cysts out like that and then you hit the ctrl key and then you press space it auto fills the rest of the command for you this is basically a print line statement to print to the console and we'll just say he's eating he'll also be able to walk and we'll give him the ability to do work all right so now we're getting somewhere all human objects in our application will be able to conduct these four basic behaviors now keep in mind that humans will not be able to for example sleep or swim because I have not defined those methods in this class all our humans have the capability of doing is just speaking eating walking and working and that's it that's what is in this specification for what a human is in our application again remember what I said about classes classes contain instructions for how objects can be created as well as how the objects carry out certain behavior the methods that I've defined here are the instructions for how humans will behave but we need a way to be able to create the actual human objects from this specification sort of like a birth method or how humans will be born and for that we need to define a special method in this java class and that method is called the constructor this special method is exactly what it sounds like it provides a way to construct objects and since this is a human class it will help us create objects of type human so let's define that method up here and now notice I you did call it a special method and it truly is a special method it doesn't have a return type and it's the exact same name as the class name so you want to keep in mind that constructor methods always have the same exact name as your class as a class in which they belong in and in reality we don't even have to define this method explicitly like we have done here because it comes by default for all classes in which a constructor is not defined but I wanted to leave it explicitly here so that you know what it looks like so now that you know what the constructor method looks like let's construct human objects I'll create another class called Earth in which we can make these humans sort of come to life let me show you how that works so you go to new and you select class the name of this class is called earth and in this class we're going to define a method called public static void main now hopefully you've seen this method before if you've written simple Java programs with just one class you've certainly seen this method this method is the entry point for all java applications and code is executed sequentially line by line inside of this method so first we need a variable of type human so let's define that we'll call this variable Tom now right now this is just a variable we need to assign this variable of value so we'll assign Tom the object tom is equal to new human so this new keyword is the thing that is used when calling the constructor method to create a new object so looking back at the human class remember that we defined a special method called the constructor which is the same exact name as a class name and this method is used to construct human objects so in our earth class I created a variable of type human and then I'm assigning that variable a new human object right so this is what is used to create objects in Java applications keep that syntax in mind if you haven't seen it already all right so tom is a variable of type human and recall the human's classes constructor now keep in mind that this tom is just a variable in reality this is not really an object objects come into existence when the application starts up right now we are just creating a structure a way to organize our code so when our application runs line by line that's when everything will happen and an object will be created once the program is running all right so now that we have a an object called tom we can make him speak we're invoking the speak method on that object so we're basically asking tom to speak now when we run this and you do that by right clicking and going to run as application basically a class with the main method in it should be the one where you right-click and hit run as and it will automatically give you the option of running as a java application so right so I ran that and notice what it printed now let me remind you what that method looked like speak was printing out all of these things and that it was printing out the basically the variables the values of the variables and when we look at this it's saying hello my name is null I am zero and just halt I am zero years old and my eye color is null now the reason for why this this is sort of an empty all of these variables are empty is because we're not assigning any of these fields so let's assign these fields of Tom let's give him some values so Tom should have an age of course we need to give him an age we'll say he's five years old Tom should have an eye color and we'll say that his eyes are brown and we'll give him what was that height in inches yeah and we'll make him a really tall baby we'll say C 72 inches tall and one more variable that's missing here is we need to of course give him a name so we'll say his name is Tom say his last name is sable now when I run this by the time it gets to here Tom will already have his fields populated with these values so we should expect to see the variables printing the actual value so when I run this you can also run it by hitting the play button here and notice that those fields belonging to Tom the Tom object the human object rather his fields are populated now let's create another object why don't you try this out on your own create an object called Joe and give him some values and call the speak method on on that object and will continue in the next video
Info
Channel: Imtiaz Ahmad
Views: 372,299
Rating: undefined out of 5
Keywords: Object-oriented Programming (Programming Language Paradigm), Java (Programming Language), Java Tutorial, Object Oriented Programming, Java SE 8 Programmer, 1Z0-808, java certification training, object oriented programming java, oop, object-oriented programming, object oriented programming
Id: 0NPR8GFHNmE
Channel Id: undefined
Length: 13min 44sec (824 seconds)
Published: Tue Dec 23 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.