Java – OOP Basics – 2/5 (program structure)
Video Title: Java – OOP Basics – 2/5 (program structure)
Okay so this is the next lesson in the prerequisite section of the course if you already understand the basics of object orientation in Java and you also know the difference between for example objects and interfaces feel free to skip this lesson but for those of you that
Were with me in the previous lesson I left you with an assignment so hopefully you got some practice on creating animal classes and objects to roam around in our Zoo so I’ll go over that solution right now here’s my animal class and I gave it the attributes such as age
Gender and weight and pound so all of our animals will have these attributes and I gave a the instructions for how the animal should be created by using a Constructor with arguments being passed in here so when we create the actual object we have to give the arguments and
Those arguments will be assigned to this to the fields in this class I also defined two behaviors that all an animals should have they should be able to eat and they should be able to sleep so again I’m going to keep drilling on this point a class is a specification
It’s a blueprint or a design in this example how animals should be in our application all right so coming to the zoo class in here I defined the main method and remember that this is the starting point for our Java application so let’s create an animal and I’ll just
Call it animal one it’s not important to get into the details of what we want to call that variable new animal right so I’ve created a variable animal one of type animal and we want to assign the created object to this variable recall that this we can’t really use the default Constructor of
The animal class because we’ve defined specifically what Constructor we want to use right these are instructions for how a proper animal should come into existence in our application so we need to pass in values in the Constructor method right here so it expects an animal to have an age so we’ll give that
Uh we’ll call it a male and the weight in pounds just say it’s a 23 pound animal whatever animal it is and now we can invoke methods on this particular variable like that so this variable actually points to the object that was created in this line
And then we ask that variable to hey make the object that you’re pointing to execute this particular method right that concept is a bit abstract right now I know it’s a little confusing to you don’t worry it will clear up pretty soon especially after practicing some more
Problems in the course I also asked you to be a bit creative and create other classes the fish class and the bird class so let’s create those fish and in this class we’re just going to have one method I’ll keep things simple public void swim fish are able to
Swim and I’ll leave it a simple implementation saying swimming all right so again this is a specification for fish in our application every object that we create from this particular class will have the behavior of being able to swim now let’s create the other class we’ll create the
Bird and I’ll Define a simple method in here say fly and we’ll just say that it’s flying again this is a specification I know I keepe repeating this but this is so important for you to intuitively understand that this class is a specification it’s a design and all
Objects that we create from this class are going to have the behavior and uh we’ll know the the instructions from this particular class so back in the zoo let’s create a bird bird one I’m not going to give any names new bird and notice that we don’t have to pass in
Any arguments in here because I haven’t defined a specific Constructor remember every class has a Constructor by default if you don’t Define one it actually has one that looks like this this is there by default if we don’t specify a a Constructor now in the zoo back in the zoo class
I created a variable of type bird that points to the object that was created and then I do bird one. fly now if we run this it’s going to behave as expected this eating behavior is from the animal class and the the flying behavior is from the bird
Class all right so as you tried that out hopefully you got this far on your own now let’s quickly review how Java code is organized because before moving further I think that’s an important point we should cover this diagram explains how Java code is organized in general Java code for the most part
Consists of classes and methods and these are basically Java files they’re they’re files with the extension. Java and they’re usually contained in folders referred to as packages so here in Eclipse we have have a package this is actually a a functionality Eclipse called the default package which is really uh not where you
Want to put your code you want to define a specific package so I’ll just right click here go to new and then you can go to package and then you can create the package and then put the classes inside that package for better organization so classes basically
Contain methods which have all of the behavior different find and the classes are basically a blueprint or a specification or a design and we use that blueprint to create the actual objects now I know we kept making the distinction between what a class is and what an object is that’s a very
Important distinction to be made early on but it’s time now to get a deeper intuition about what objects really are what exactly do you think is an object you already know that we use the construct of a particular class to construct objects of that class so for example
When we created Tom and Joe in the previous lesson they were both instances of type human but when exactly did those objects come into existence strictly speaking it’s not exactly correct to be calling the variable Tom or or this variable bird or animal one an object because objects are a runtime concept
They don’t actually exist until the application runs so in our Eclipse if I hit this run button again remember this is the entry point for our application so it’s going to start here executing the program it’s going to go line by line and when it gets to here it’ll
First create a variable of type animal it’ll allocate space for in in memory for that particular variable and then somewhere else in memory it’s going to create the actual object all right and this variable animal one is going to point to the memory address for where that object is
Actually located so when we invoke the eat method on this variable this variable will be able to tell its object it’s going to say hey object I know where you’re located in memory do this for me execute the method eat and remember that the eat methods behavior
Is defined inside of the animal class what is it going to do it’s just going to say that it’s eating it’s going to print that out it’s going to print that to the console so similarly as the program is executing it’s going to move on to the
Next line it’s going to come onto this line where we are creating a new object of type bird all right and we have this variable called bird one that is going to point to the location in memory where the actual bird object is created and then this variable is going to when we
Invoke the fly method on this variable it’s going to be able to tell the object in memory to execute the fly behavior and that fly behavior is of course defined in the bird class right here it’s just going to say it’s just going to print flying to the console so again
Because objects are a runtime concept they don’t actually exist until the application runs so before hitting the Run button here in Eclipse all we have are files with Java code in them that’s just a way of organizing our code so basically just files containing instructions all over the place for how
Our program should operate when we actually run the application so there are no objects at this point right the application is not running there are no objects at this point when we run the actual application the objects come into existence so they’re born so to speak
And only then do they carry out their behavior and do things in the application now this is a subtle but an important Point beginners in objectoriented programming think about the classes and methods they would need to write but more experienced developers think in terms of runtime and how objects will behave while the
Application is running if I keep talking like this you’ll soon transition into this way of thinking as well but for now it’s enough to just understand that when we run our application memory resources get allocated to contain the objects of our program and the instance variables these These are ALS referred to as
Instance variables these variables point to the actual location for where those objects exist in memory and through these variables we can actually ask those objects to do things such as fly and eat and they get this Behavior through the variable type which is animal and we have that behavior defined in animal
Class to be able to eat and sleep and so on all right so I I tried to repeat this concept multiple times in couple of different ways so that you can hear it in different flavors and I hope that you have a deeper understanding of objects
In a Java application but if it’s still a little confusing for you don’t worry uh we’re going to be coming to this later in the course and by then it’ll make a lot more sense
-
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
Thank you, I'm really glad I found your channel. I'm still a trainee in Java Development in our company and it's really helpful that someone can make me understand this concepts. My goal is to really be able to develop my thinking and intuition and this really helped me through these initial stages.
I want code
Hi Sir What is the name of the software app you are using to type in the java program
Oh my Jesus. U r awesome
Thanks ❤️
You have made an excellent presentation, simply excellent ! thank you very much
Your voice is so calming…
I really love ur videos a lot and i helps me in learning java thank you
the best teacher ever..the concept of animal is the type of variable animal1 and object exist only during runtime…it surely made me clear all doubts which was nt cleared fr the past 200 lectures of other instructors.thnks a lot sirr..thnks a lott
Thank you Ahmad for this videos. I am getting your Udemy course, you are very good at explaining. Thank you and keep up the good work!
Very good explanation.
Just when i was about to 'throw the towel' i came up with this channel.. awesome way of making simple topics even simpler! so tired of over-complicated tutorials, can't thank you enough.. keep it up!
Really well made video. Cleared up a lot of my doubts!!!
@Imtiaz Ahmad, I've just came across your tutorial, I am trying to sing up but it is not $15 as you mentioned; it says $194. Help!
Awesome
ahhhh man why didnt i see this 2014… thanks lot though…
thanks a lot, you are much better than my teacher.
Thank you so much. I've read this on codeacademy for a couple of times and still didn't understand.. your explanation are more clear and direct.
I deeply love indian programming guides, and you sir, are the god of them. Well done.. well done
its kinda sad that i have to pay thousands in tuition to people who cant explain concepts like this, just to get my degree.
Hi Imtiaz ,
can you upload the second video that you're mentioning at the start of this tutorial, Please.
thanks
Just brilliant!
Hi Imtiaz, thanks for the upload.
I got a little confused with the order to watch the videos and follow the instructions, I mean, what's happening between first video and this one.
Did you create "Human" class, but then, you wont used it again… Or…. that I missed something?
There's a missing episode between last one and this one.
Does anyone know why he didn't have to extend the classes from ZOO. i.e When he created an object when he creates the bird object in zoo?
Hi there. Than you for this tutorial. You explain briefly and structured. I like it very much.
However, I think there is something missing. In the first lesson, you show the class human and main in world. Plain constructor. Next course is 2/5 and you show a finished class animal with a enhanced constructor with arguments and the this keyword. Did i miss some video?
Huh? You didn't assign us to do anything to have with Zoo but with a Human object called Joe.
Also out of nowhere we see values inside contructor and inside the brackets some functions, this.[…]
Explain man why and how
hi, how do I use the object bird1 outside of zoo once i create it in zoo?
very good teacher
Mr. Imtiaz Ahmad is very forward thinking. He made gender a string rather than a Boolean. Gender is non-binary in this guys program.
wow
Thanks Imtiaz Ahmad
u r really a nice teacher with a good knowlegde in OOp
Cheers
nice
ur the mvp
Do you have more video's about java programming?
Could you please give me a brief overview of what program structure is in relation to object orientated programming ?
You teach better than the professors at Rutgers
This guy is way better than my cs101 teacher