Java – OOP Basics 1/5 (Class and Object)
Video Title: Java – OOP Basics 1/5 (Class and Object)
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 that site is
A great resource for getting practice on loops and conditionals in Java all right 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 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 okay so coming over to objectoriented programming it’s all about the objects just like how objects behave in the real world software
Objects have Behavior too 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 fly 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 all right 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 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 rightclick 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 we’ll 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 we’ll 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 Ines 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 uh particular Fields right so let’s introduce ourselves hello my name is and then we can concat repend the name variable at the end of this
Statement similarly I can print out the height in inches print out the age and we’ll also print the eye color similarly let’s define some other methods in this class will make them eat by the way I’m using a an eclipse shortcut when you type in sis out like
That and then you hit the control uh key and then you press space it autofills 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 it’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 now 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 object 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 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 the 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 uh 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 uh make these humans sort of come to life and let me show you how that
Works so you go to new and we 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 in 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 a 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 the 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 we call the humans 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 uh everything will happen and an object will be created
Once the program is running 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 Java application basically the 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 applications all 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 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 inches tall 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 5 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 he’s 72 in tall and uh 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 we say his last name is Sabo 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 Joel and give him some values and call the speak method on uh on that object and we’ll continue in the next video
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
This is a marvellous tutorial. I'm on week 8 of a computer science degree, but the teacher has not written a line of code in 9 years and I am lost.
After watching a few of these tutorials. I feel like i comprehend an exponential amount. Thank you!
Would you please bring new course from beginner to hero
BEAUTIFUL! LOVE IT.
You’re accent is amazing
13:28 Joe Who
heightInInches xD
wow
Elvis Presley is that you?
i searched for so many videos to understand the OOP basics in java and this is the only video that really helped me!! highly recommend it for everyone. thank you so much for your great explanation!!!!!!!
what a voice… orgasm is what I am having
beautifully explained!
This helped me a lot!!!
For more visit: https://youtu.be/wbrWpt–cgw
Great lecture! Thank you for this!
For more visit: https://youtu.be/q2RgFH1rPos
this is a very wonderful way of teaching
after 3 semesters i still couldn't understand how to define objects in a class
thank you sir and Allah bless you
I just started learning java this year and I was so lost and scared that I would fail my exams cause I didn't understand oop at all but now I do thanks to this tutorial 😭 You're such a life saver 🙏 THANK YOU SO MUCH😭
Tom Szabo was my Class-mate 😀
Superb work mate
great tutorial!
Nice, keep it coming. Thanks
amazing tutorial!!! BTW Tom is the future Braun Strowman.
Imtiaz, thank you for sharing these incredible videos.
Hands down one the best trainers, really engaging, clear and such brilliant delivery
How did you call an object in "human" class from the "earth" class without using inheritance?? Please explain this anyone.
I'm watching this because we have a project wherein we need to do an Oop with case switch or if else, for loop or do while, mathematical operation and logical operator all in one program. Im dying hahahaha
72" tall at 5 years old?
One of best teaching methods in whole world
Hindi me banao vdos all
you're the best 🙁 thank you so much for doing what you do.
I rarely comment on videos, this is awesome. Thanks for making it simple to understand.
This is the most beautiful OOP tutorial. Love you.
public class Human
{ //data members
String name;
Int age;
Int height;
String eyeColor;
public Human(int age, int height, String name String eyeColor) <– not the default constructor anymore because we created our own.
{
this.age = age;
this.height = height;
this.name = name;
this.eyeColor = eyecolor;
"this" referring to the data members above the constructor in the human class.
}
}
then in the earth class you can make the object like this
Human Tom = new Human(5, 72, "Tom", "Brown"); <– these will be assigned to the data members in the Human class.
then just Tom.Speak(); like normal.
nice brother
your examples are cool, thanks~
A great video that makes it really easy to learn
GOD SEND YOU I HAVE A FINALS AFTER 1 WEEK AFTER FINALS IM GONNA WRITE RESULT HERE
TOM ZSOBO – code auto detects nationality to be Hungarian
Does anybody else feels that being a 5 yr old kid Tom is about 3 fucking ft taller than most of his fellow classmates ? 🙂
Why didn't you use the main function in Human.java?
You forgot to create a method that assigns Tom to an NBA team being 6 ft talk at 5 years old
Thank you so much, this was very helpful!
Great explanation. Thank you!
Thanks 😊
you are the best!
Thanks man