Java – OOP Basics 3/5 (Inheritance)
![*](https://i0.wp.com/allprowebdesigns.com/wp-content/uploads/2023/12/1703333178_maxresdefault.jpg?resize=840%2C430&ssl=1)
Video Title: Java – OOP Basics 3/5 (Inheritance)
This is the next lesson in the prerequisite section of the course if you already understand the basics of object-oriented programming in Java and you know the difference between say an interface and an abstract class feel free to skip this section and move on to section two where the course actually
Begins but for those of you that were with me in the previous two lessons I introduced object orientation to you and we created some classes and I showed you how to create objects from those classes in this lesson we’ll further build build upon uh object-oriented programming and
Move on to the to interfaces and Abstract classes now let me ask you a question why do you think they call it objectoriented programming I mean why don’t they call it object-oriented science or object-oriented Theory or something like that well the reason why they put the word programming in there
Is because it’s a way of programming it’s a better way of programming as opposed to an older uh approach which was to dump all of the code into to one file and uh just have methods um all over the place in that one file would
Just be a very very large file full of code um if we were to use an older programming language but the new and proven way of better organizing our code is called the object-oriented way of programming and that’s what um Java is based off of and that’s what I’m teaching you here objectoriented
Programming is a way to organize our code rather than having all of the code in one file we split the code into multiple files that make logical sense so in the previous lesson what we did was we created a class called animal and we gave it some instructions for how the
Objects will behave and we created some other classes such as fish and bird um and in the zoo class we had the main method which really is the entry point every application needs an entry point and the main method was the entry point for our applications so when we hit run
This is where uh the program starts and it would go line by line and uh basically create our objects uh based on how we organize our code so we are providing instructions on line by line how the object will be created and what methods we want invoked on that object
Right so again remember this is a variable that points to some object that gets created in memory uh when this U part runs um and then it goes to the next line and then it invokes the eat method moves to the next line creates a new object assigns it to a variable of
Type bird and then uh based on what the specification in the bird class we’re able to access the fly method so we invoke it here right so these are at the end of the day um instructions on how our our application should be run and we have code nicely organized in these
Files okay great so let me build upon the idea of code organization in object-oriented programming now notice how in the animal class um I’ve defined two methods eat and sleep that pretty much all animals in a zoo do and this behavior of eating and sleeping is applicable to the bird
As well as well as a fish I don’t know if this fish sleep because I’ve never seen their eyes close but definitely the birds um I think they sleep but but anyway this behavior is applicable to other animals right so how do we better organize our code so that other classes
Can take advantage of of of these two methods as well the way to do this is through inheritance right inheritance is another way of organizing our code and that basically is exactly what it sounds like one class inherits behavior from another class and the best way to do this is to
To make sure that the class that is wanting to inherit that behavior is actually a breed of of the parent right so basically the bird is a a kind of animal right it is an animal and a fish is also an animal so it makes sense for
The bird as well as the fish to be inheriting behavior of the animal so the terminology here I’ll introduce some terminology to you the animal class is referred to as the parent class and these two classes will uh be child classes so for bird in here we can type in
Extends animal and now the bird is a child class of animal um the animal is also referred to as the super class another name you’ll hear is that the animal is the base class it contains base functionality and the bird class is a subass it contains sort of sub
Functionality so eclipse is complaining here it’s showing a little um red squiggly line indicating that there is an error so if we hover our Mouse over it it’s saying that implicit super Constructor animal is undefined for default Constructor so let me translate that for you it’s saying the way to create a bird
Is different than the way to create an animal so basically the instructions on how to create a bird right the bird Constructor is different than the animal Constructor and if we’re saying that the bird is an animal right it extends an animal it inherits functionality of an
Animal if you’re saying that the bird is an animal then how can it have a different way of creating a bird a bird is an animal it should be created the way animals are created so we need to basically Define a Constructor in bird that is compatible to the one that we
Have in an animal so by default um if you remember I mentioned this in the previous lesson there’s a default Constructor in every class um that would look like this right it would have the same as the it would have the same name as a class name and this exists by
Default in all classes where there is no Constructor defined but when we extend a class like this that default Constructor won’t work anymore right because we are there’s a conflict uh between the the ways in which an animal is created and the way in which a bird
Is created all animals require if you go back to the animal class these parameters to be passed in right so a bird cannot possibly be created without these um parameters so in the bird class we need to define a similar Constructor uh as animal so the way we do this is we
Can hover your mouse over the bird class and click on ADD Constructor and it will pretty much automatically fill that in for you I just got rid of some comments there and what it’s doing is in the bird Constructor it’s calling super which means it’s calling the super
Classes Constructor right to create a bird object we are propagating those values over into the animal uh because a bird is an animal it can’t possibly be uh created on its own if if it’s extending from the animal class okay so hopefully that makes sense um but if it
Doesn’t don’t worry we’ll be doing this plenty more in the course and uh it’ll make uh all the sense pretty soon so if we come over to the fish class the fish is also an animal so let me extend uh animal here and now the fish
Is a child of the animal and it it should inherit all of the base functionality that’s in that that is in animal so if I hover my mouse over into fish I can can add the Constructor here and it will basically call the animals Constructor right so when we create a
Fish object we pass in the values over um and uh those values will basically be carried over into the um super Constructor to create a valid animal object right so let’s test this out let’s go back into the zoo and notice that we can no longer create a bird like this right
When we don’t have a default Constructor anymore we have a specific Constructor in the bird class so we need to give it some values to properly instantiate a bird instance so um I’ll say that this bird is 3 years old its gender is it’s a um we’ll say it’s a
Female and let’s say it’s a 10B bird that sounds kind of heavy for a bird but not sure how heavy birds are but it doesn’t matter anyway and then we can invoke the the the method that’s defined in the parent class of this bird so bird
1 do we can make it eat now all right this bird is now also capable of eating we can also make this bird sleep right these methods are accessible to us now now can you guess if we try to make for example this animal object or animal
Variable I’ll refer to it uh if I call animal 1. flly do you think it’s going to work of course not because the fly behavior is part of the bird class okay it’s sitting here and this is not visible to the parent and thank God that it’s this way
Because it’s a much better way of organizing the code the parent should not be able to inherit from the child that’s sort of backwards the child always inherits from the parent so basically again what is a class it’s a specific it’s a design of how things out
To be and a bird needs to if it’s a if a bird is an animal it better follow the same specification because it is an animal that is why in Java everything that um all the method visibility is driven by the variable type so let me
Show you what I mean by that this animal one variable is a variable of type animal so the methods that are visible to so this particular variable are uh the methods that are defined in its type specification so the animal is its type the methods that are defined in that
Animal are the only methods that are visible to this particular variable in the bird’s case the methods that are visible to it are all of the methods that are defined in its class as well as the methods that are defined in the parent class okay so this type basically the
Bird specification when you ex when you when we have bird extend from animal the animal specification is sort of copied over and that’s why we had to conform to the animal the the way animal is created that’s why we had to define a similar Constructor and all of the methods that
Are in animal are basically copied over into this class because we are extending it but luckily we didn’t have to type them out here that’s what this extends keyword does for us so again keep in mind that the uh the methods that are visible to a particular variable are
Based on the type of that variable all right and that is animal all right so I think now is a good time to calibrate what we’ve learned so far I know we’ve covered a lot of ground we talked about inheritance and classes and objects and Constructors and so on uh but I don’t
Want you to miss the whole point the whole point is code organization that’s what objectoriented gives you um newcomers to this field often get caught up in the intricacies of you know creating objects and interfaces and and there’s a lot of other Concepts that you’ll learn U but I don’t want you to
Miss the entire point and that is to organize your code better that’s what object-oriented programming provides you that is why we decided to extend the animal class we made bird extend animal because a bird is an animal and if we didn’t have the ability to extend animal
We’d have to copy over the method for example in the b in the animal class we’d have to copy over these two methods into the bird class right we’d have to paste them here and there would be code duplication going on if I chose not to extend animal but extending animal
Basically allowed me to write cleaner code right so this is what object orientation is all about it’s about better organizing your code and when uh in terms of a computer it doesn’t care what language you use or what framework you use to write it’s just going to you
Just have to give it instructions and it will execute those instructions sequentially so back in the zoo class when our application starts up from the main method it just goes line by line executing all of these lines creating the objects that it needs and uh basically running the application uh but
Just keep in mind mind that these are just files and when you run the application that’s when the objects come to life and they do things right and that’s how the application runs I know it’s a very basic point but I want you to keep that on the back of your head
When you’re uh first starting off programming uh in an objectoriented language all right so I wanted to introduce two other ways of better organizing your code and that was interfaces and Abstract classes but I think uh now is a good time to end this lesson because it’s already getting
Pretty lengthy so I’ll introduce you those topics in the next lesson all right thanks for watching I’ll see you soon
-
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
You are the best at explaining things with easy-to-understand examples. No technical complicated expressions and the tone of your voice is calm and well-balanced to express something. I have been looking for this kind of video since September 2020 to understand the basics of OOP, and I finally found it in December 2022. BRAVOOOOO Professor!!!!👏 Thanks for sharing your good knowledge.🤝
Simple easy explanations oop.
This guy is the Top of the line. I am gonna follow all his tutorials. All that is available both free and paid.
Very understandable tutorial helped me with my assessment for today
It does not comes to be $15. It says $149. I am doing wrong
Great series of videos.
.
Sir,
Why are using "this" keyword while declaring the variables inside the constructor?
super clear , thanks a lot !!
🙁
Make more videos
Hi guys, is there a "friend" inheritance among classes in Java instead of using parent-child classes?
How dare you not using dark mode!! Jk your videos helped me a lot with preparing for my college exam. Thank you!
Great videos man!
Can a child object have MORE parameters than the parent? Provided it has all the parameters that the parent has already.
You are so good at explaining things. Thank you so much. I wish my teacher could be that clear.
Your voice is so soothing!
Dynamic type please
hi ! thank you this video! however i am confused about the difference between object and interface?
Very good, simple explanation of inheiritance
Everything makes a lot of sense now. Thank you for your understandable tutorial
Thanks bro,your explanation is very clear !
I rarely comment but this is the best and most simple explanation of oop !!
super…..
Amazing explanation..Thank you….
Hello please send me your WhatsApp number
Hey man just want to let you know you are brilliant, I have been trying to get my head around some of the concepts you have covered for a few weeks now, and just from the first lesson a lot of the mess just fell into place even though I had been watching many other tutorials on the same topics. Cheers for being different and clear man <3
Wow!! Man, your teaching is dupe!! You are such a great teacher. Thanks a lot. This video was really helpful I must admit.
thanks a lot bro, this is very helpful!!!! <3 Great job <3
Nice videos. Your voice reminds me a lot of Sam Haaris's.
Mr. Ahmad, would you recommend extending Animal to Zoo as well?
Hı Imtiaz I agree with the comments below.. Well. what is the difference if I register and pay for the online course ? than this you tube video ? it seems like you will just transfer the information to the brains in the online course coze this you tube is already pretty much calm down and explanatory.. thank you
Oh my god! I love you. Your videos are very clearly explained
I have badly important question please answer if you know , here it is Does fishes sleep ? Ha ha ha 😀 What a joke. Hey i have java exam tomorrow Ha ha .Guys i think something different. I need more coffee :D. Oh and birds sleep yes. Omg i have exam tomorrow. I feel very healthy mentally Good video. Thx.
For a long time I thought I'm stupid and can't get the oop 😀 Thank you for the amazing tutorial!
We can inherit those class only whose constructor is same as child class(or parent class)
All animals are created equal!
you should've covered polymorphism here
Spot on!
Imtiaz thank you for your videos and it is really helpful. You would be an amazing professor or teacher.
Thank you sir!
Hey! I love the theme and the font color. It is not same for my eclipse.
Can you tell how and where to change this, and get the same as yours?
A very helpful resource
so far your tutorials is very helpful. keep it up!
How can i practice what i learned so far? Is there some place where there's a list of problems that i can solve with the material covered so far?