Learn Java Object-Oriented Programming (with actual code)
概要
TLDRThe video aims to teach viewers about object-oriented programming (OOP) in Java, breaking down its complexities through coding examples. It begins with encapsulation, explaining how classes are used as blueprints for creating objects, with private attributes accessible via public getter methods. The concept of inheritance is illustrated by creating subclasses like 'Fruit' and 'Weapon' that derive from the 'Item' superclass. The video further explores polymorphism through method overriding, showing how subclasses can modify the behavior of inherited methods. Finally, it covers abstraction using abstract classes and interfaces to simplify complex implementations. The presenter encourages hands-on practice to reinforce learning.
収穫
- 📚 Simplified understanding of OOP concepts
- 🛠️ Importance of encapsulation for data safety
- 🔄 Inheritance promotes code reuse
- ⚙️ Polymorphism allows method flexibility
- 🔍 Abstraction minimizes complexity
- 📦 Java classes serve as blueprints for objects
- 📜 ArrayLists manage dynamic object collections
- 🔄 Method overloading enhances functionality
- 🖥️ Encouragement to practice coding
- 🎮 Real-world application through an inventory system
タイムライン
- 00:00:00 - 00:05:00
The video aims to simplify object-oriented programming by explaining its four principles: encapsulation, abstraction, inheritance, and polymorphism, through coding examples. The presenter starts with encapsulation by creating an 'Item' class containing attributes like name and quantity, which are private and can only be accessed or modified through public getter methods. This encapsulation secures the attributes and regulates access.
- 00:05:00 - 00:10:00
Next, the presenter builds an 'Inventory' class, which uses an ArrayList to manage multiple item objects. The Inventory class features a method to add items and another to display them, illustrating encapsulation by protecting the attributes while providing controlled access to them. The demonstration includes creating instances of items and adding them to the inventory to showcase the process.
- 00:10:00 - 00:15:00
The discussion moves to inheritance by introducing a 'Fruit' subclass that extends the 'Item' class, thereby gaining its attributes without redefining them. This showcases how subclasses can inherit properties from their superclass, making the code more efficient and organized. An example is given by creating fruit items with specific types.
- 00:15:00 - 00:20:00
After covering inheritance, the presenter introduces polymorphism, highlighting how methods can behave differently based on the object invoking them. This is further illustrated by overriding the toString method in subclasses, which provides specific string representations for different item types when displayed in the inventory.
- 00:20:00 - 00:29:44
Lastly, abstraction is explained as a concept that hides complex implementation details and shows only essential features. The presenter creates an abstract Item class to restrict direct instantiation and forces subclasses to implement specific methods, segmenting shared functionalities in interfaces and helping to reduce complexity while maintaining flexibility in the code.
マインドマップ
ビデオQ&A
What are the four main principles of object-oriented programming?
The four main principles are encapsulation, inheritance, polymorphism, and abstraction.
How does encapsulation work in Java?
Encapsulation in Java is achieved through private attributes with public getter methods which restrict direct access to the attributes.
What is inheritance in object-oriented programming?
Inheritance allows one class (subclass) to inherit attributes and methods from another class (superclass), promoting code reuse.
What is polymorphism?
Polymorphism allows methods to behave differently based on the object that invokes them, typically through method overriding.
How is abstraction implemented in Java?
Abstraction can be implemented using abstract classes and interfaces to hide complex implementation details.
What is an ArrayList in Java?
An ArrayList is a resizable array implementation of the List interface that can store objects in Java.
ビデオをもっと見る
VIDEO PERKENALAN DIRI: HERRIWANSA AKWILA LEJAP | BANK BRI 2024 | TALENTA BRILIAN
Ustad Das'ad Latif - KHUTBAH JUM'AT MASJID AGUNG BATAM
DEVICE OPENWRT ALL IN ONE - HP OPENWRT SIMCARD ON
Ritz, el gran visionario hostelero!
PERINGATAN ISRA' MI'RAJ NABI MUHAMMAD SAW 1446 H | 31 Januari 2025
[KO 23] SILAKAN SUBSCRIBE: Charging System: Rangkaian Sistem Pengisian Konvensional
- 00:00:00so my only goal with this video is to
- 00:00:01have you understand object-oriented
- 00:00:03programming at the end of it that's it
- 00:00:06it's a lot more simple than how I feel
- 00:00:08like a lot of people make it sound
- 00:00:09there's a lot of big words encapsulation
- 00:00:12abstraction inheritance polymorphism
- 00:00:15those are the four main principles of
- 00:00:17object-oriented programming and instead
- 00:00:19of me sitting here talking about it what
- 00:00:21I'm going to do is code it and explain
- 00:00:24how each one of these things are working
- 00:00:27as we code I think that's the best way
- 00:00:28to go about it so in order to talk about
- 00:00:30those four principles and we're going to
- 00:00:31start off with encapsulation we first
- 00:00:33have to make a class so I'm going to
- 00:00:35create an inventory system like you
- 00:00:37would find in a video game and what does
- 00:00:39that inventory hold items so we're going
- 00:00:41to create an a Java class and we're
- 00:00:43going to call it item so you can think
- 00:00:45of a Java class as a template or a
- 00:00:48blueprint to create new items and every
- 00:00:52new item that you make is an object
- 00:00:55hence the term objectoriented
- 00:00:58programming It's Not Over compc Li at
- 00:01:00it's quite literally an object like the
- 00:01:02phone on your desk the cup on your desk
- 00:01:03The Sword in your inventory those are
- 00:01:06objects and what of those objects need
- 00:01:08well they need some attributes so
- 00:01:11they're going to need a name so we'll
- 00:01:12save that name as a string and they're
- 00:01:14also going to have a quantity like you
- 00:01:16can have 30 apples in your inventory so
- 00:01:19that is going to be an integer and we
- 00:01:22went over data types and everything
- 00:01:23leading up to this point in the last
- 00:01:25video about learning Java in 15 minutes
- 00:01:27the world's shortest Java course
- 00:01:29whatever I named it we didn't go over
- 00:01:31private though so private as opposed to
- 00:01:33public means that they cannot be
- 00:01:36directly accessed from outside this
- 00:01:39class in order to access them and assign
- 00:01:42values to name and assign values to
- 00:01:44quantity which again will be different
- 00:01:46for every single item which is why we're
- 00:01:48not assigning a value of of item or
- 00:01:51anything to it right now the way to do
- 00:01:54this is via getter methods string get
- 00:01:57name and we're going to return name and
- 00:01:59then public int get quantity and we're
- 00:02:03going to return quantity these two
- 00:02:05methods allow you to access and modify
- 00:02:08these private protected attributes and
- 00:02:12this right here in this instance these
- 00:02:14two attributes and these two methods
- 00:02:16that allow you to access and modify
- 00:02:18these private attributes is
- 00:02:20encapsulation it keeps our details
- 00:02:22inside this class safe and provides
- 00:02:25controlled ways to access or modify it
- 00:02:28now in order to be able to use all of
- 00:02:30this remember this is uh so far it's not
- 00:02:32a complete blueprint but it is the class
- 00:02:35is a blueprint in order to create an
- 00:02:37actual object using this blueprint we
- 00:02:40are going to create a Constructor which
- 00:02:42is not called Constructor it is it is a
- 00:02:44special method that you call the same as
- 00:02:46your class and this name this name this
- 00:02:49quantity this quantity and the this
- 00:02:51keyword is just Java allowing you to
- 00:02:54have your parameters be the same name as
- 00:02:57your attributes instead of having to
- 00:02:59name it like like name two quantity two
- 00:03:02and then you'd have to come and remove
- 00:03:04this and then add two to each one of
- 00:03:07those it it's the same thing just uh a
- 00:03:10much better and cleaner way to go about
- 00:03:12it like this now we're going to call
- 00:03:13this Constructor the special method in
- 00:03:15order to create an object using this
- 00:03:17template but first we have to create our
- 00:03:19class inventory because inventory is
- 00:03:22what's going to be holding these items
- 00:03:25and displaying these items so we're
- 00:03:27going to call it inventory so in
- 00:03:29inventory we have to store those items
- 00:03:30so we're going to create another private
- 00:03:32field but we're going to use something
- 00:03:33we haven't discussed before and that is
- 00:03:35an array list an array list of item
- 00:03:38items so an array list is a collection
- 00:03:41which is a specialized subset of
- 00:03:43non-primitive data types it stores and
- 00:03:45manages groups of objects and it is
- 00:03:47predefined as you can see up here the
- 00:03:49automatic Import in java.util.arrays so
- 00:03:52Java has it built in now let me to
- 00:03:54repeat part of that it is to store and
- 00:03:57manage groups of objects whereas an
- 00:03:59array for example could store strings it
- 00:04:03can store integers but it cannot store
- 00:04:06objects whereas the array list can
- 00:04:09restore objects and is also dynamically
- 00:04:12resizable whereas arrays by definition
- 00:04:15are fixed even though in JavaScript and
- 00:04:17other languages they're not fixed they
- 00:04:18are resizable in Java they're fixed they
- 00:04:20don't store objects so we have to use an
- 00:04:22array list to store items objects now
- 00:04:25what I have to do is create the
- 00:04:27Constructor 4 inventory whereas we don't
- 00:04:30have any parameters that we're passing
- 00:04:31through we're just creating a new array
- 00:04:33list called items and just to clarify
- 00:04:36that statement this is where we are
- 00:04:38creating or declaring the array list
- 00:04:41called items of item or in other words
- 00:04:43item objects and within our inventory
- 00:04:46Constructor we are assigning a value of
- 00:04:49new empty array list to that array list
- 00:04:52of items because we're going to use a
- 00:04:54method to add items to this items array
- 00:04:58list and we do that with public void add
- 00:05:01item item item items add item but the
- 00:05:04way this works is that when you use this
- 00:05:06method you're passing in a parameter of
- 00:05:08item which we're going to create over
- 00:05:10here in just a second and then that is
- 00:05:13going to be added to this items array
- 00:05:17list that we created or in other words
- 00:05:20this method is adding the item into the
- 00:05:23inventory but also we want to be able to
- 00:05:25display that inventory we also want to
- 00:05:27remove item but I'm not going to do that
- 00:05:29right now want to display inventory
- 00:05:32we're going to use a for Loop for this
- 00:05:34item item system out printland item and
- 00:05:36I could make this a little bit neater
- 00:05:38hold on so this is how I make it
- 00:05:39prettier we're going to have item print
- 00:05:40out and then the name of the item and
- 00:05:42then the quantity print out and then the
- 00:05:44actual quantity of the item oh all right
- 00:05:46now that we have our inventory class and
- 00:05:48with our private attributes and then our
- 00:05:51public methods that is encapsulation
- 00:05:53allowing us to have access to these
- 00:05:55private attributes and then we have our
- 00:05:57Constructor right here that all allows
- 00:05:59us to use this class template in order
- 00:06:02to create a new inventory object as well
- 00:06:05as the same thing over in item well how
- 00:06:08about we create those so we're going to
- 00:06:09start off with creating inventory and
- 00:06:12we're going to call it inventory equals
- 00:06:14new inventory that may be a little
- 00:06:16confusing because everything has the
- 00:06:17same name but think of it just like
- 00:06:19string string equals string just like
- 00:06:23this so what's happening is that we're
- 00:06:24creating an inventory object called
- 00:06:28inventory just like here we creating a
- 00:06:31string called string which we're
- 00:06:33assigning the value of string which
- 00:06:36we're assigning the value of an empty
- 00:06:39inventory object with this inventory
- 00:06:42right here being the Constructor and by
- 00:06:44the way if you don't know how to drill
- 00:06:45in you just control click and it'll take
- 00:06:47you to exactly what it is and then this
- 00:06:49inventory over here is this class this
- 00:06:53entire class so then we're going to do
- 00:06:55the same thing with item so we're going
- 00:06:57to call Item we're going to call this
- 00:06:58item one cuz we're going create two
- 00:07:00items new item and we're going to call
- 00:07:02this item apples and we're going to give
- 00:07:04it a quantity of 20 we have 20 apples
- 00:07:07and then we're going to do item two but
- 00:07:09instead of apples we're going to make it
- 00:07:10a sword instead of 20 we're going to
- 00:07:12make it two because we're dual wielding
- 00:07:14these suckers we have two swords well in
- 00:07:16our inventory not in our hands actually
- 00:07:17that's not true we have to add these
- 00:07:19items these new objects that we just
- 00:07:21created into our inventory and the way
- 00:07:24to do that is add item one to inventory
- 00:07:27add item two to inventory and and then
- 00:07:29we want to display set inventory with
- 00:07:32our display inventory method and what
- 00:07:34should happen is that well we should be
- 00:07:38displaying these two pieces of inventory
- 00:07:41right here item apples quantity 20 item
- 00:07:43sword quantity 2 and if you recall that
- 00:07:47is exactly the format that we have over
- 00:07:49here in our for Loop item Apple's
- 00:07:52quantity 20 now let's hop into
- 00:07:54inheritance so we're going to create a
- 00:07:56new class and that is going to be called
- 00:07:58let's go with fruit so fruit for this
- 00:08:01instance is going to be a very basic
- 00:08:03example and then we're going to do
- 00:08:05weapons after this so you can see just
- 00:08:07how fruit and weapons differ from each
- 00:08:10other but also are similar because they
- 00:08:12have the same attributes as item that'll
- 00:08:14make all it all makes sense in a second
- 00:08:16so the way we do this is that fruit
- 00:08:19extends item item is the super class
- 00:08:23fruit is the subclass 2 item and it
- 00:08:27inherits the attributes and any other
- 00:08:29methods that are in this class item as
- 00:08:32its own so we don't have to come in here
- 00:08:35what is it string name String quty we
- 00:08:36don't have to come in here we know that
- 00:08:38the fruit has a name and a quantity
- 00:08:39right we don't have to come in here and
- 00:08:40do string name and private int quantity
- 00:08:44we don't have to do this because we are
- 00:08:46already inheriting that what we do is
- 00:08:48actually give it specific attributes
- 00:08:50like type is it a banana is it an apple
- 00:08:53is it an orange so then we're going to
- 00:08:56create the Constructor of fruit so we
- 00:08:58can initialize the fruit we're going to
- 00:09:00give it string type because that's the
- 00:09:02attribute that we have up here but we're
- 00:09:04also going to give it the same exact
- 00:09:06attributes that we are inheriting from
- 00:09:09item so what is that quantity and this
- 00:09:12super name quantity right here is
- 00:09:14actually how you assign the values being
- 00:09:15passed in as parameters over here from
- 00:09:18your super class so instead of doing
- 00:09:20this name equals name this quantity
- 00:09:23equals quantity because it is in your
- 00:09:24super class remember item is the super
- 00:09:27class to fruit fruit being the sub class
- 00:09:29and item containing these attributes
- 00:09:32this is just how you do it so now let's
- 00:09:33use this template and our Constructor to
- 00:09:36initialize a new object of fruit so item
- 00:09:39we don't need this let's just do one
- 00:09:41item we already have something for fruit
- 00:09:42so let's call this generic item just to
- 00:09:44kind of get things out of the way there
- 00:09:47and we're going to create an instance of
- 00:09:48fruit called fruit equals new fruit and
- 00:09:51the first deal is a type I believe yes
- 00:09:53it is so what type of apple should we do
- 00:09:56these are the types I'm thinking of uh I
- 00:09:58don't see a granny Smith up here let's
- 00:10:00go with the Fuji so the type is of Fuji
- 00:10:03the name I believe is next and that is
- 00:10:06Apple and we're going to have a quantity
- 00:10:09of 20 Fuji apples and since we changed
- 00:10:11the name up here as you can see it's not
- 00:10:13being used so we have to make that right
- 00:10:15and instead of add item item two we're
- 00:10:17going to add item fruit so now inventory
- 00:10:20should have generic item 10 and then
- 00:10:23fruit uh Fuji apple 20 when we run it
- 00:10:27and there we have it item generic item
- 00:10:2910 item Apple quantity 20 oh we are not
- 00:10:32printing out the correct stuff over in
- 00:10:34inventory now are we and the reason
- 00:10:36being is that I wanted to have
- 00:10:38polymorphism be its own lesson so I
- 00:10:40didn't do it along the way here so do I
- 00:10:43do polymorphism now or do I do the
- 00:10:46weapon now let me do the weapon and then
- 00:10:48we'll do polymorphism and then we'll be
- 00:10:50able to print things out properly and
- 00:10:52display inventory by using override
- 00:10:54we'll get to that in a second though so
- 00:10:56let's create Java class weapon and a lot
- 00:10:59of this is going to be exactly all of
- 00:11:01this should be exactly what we just saw
- 00:11:03we're going to have its own unique
- 00:11:05attributes we're going to have damage
- 00:11:07and method is also going to have a type
- 00:11:09so I guess in a way we could have put
- 00:11:10that into item. Java as opposed to in
- 00:11:13each individual weapon and fruit but
- 00:11:16that's okay we're going to create the
- 00:11:19Constructor weapon and I'm going to do
- 00:11:21this in a little bit different order I
- 00:11:22like to have the name first and then the
- 00:11:25quantity second and then we'll add in
- 00:11:28damage and then string type so then the
- 00:11:32type there which also makes me want to
- 00:11:34come through here and move string type
- 00:11:37here to the back which means I also have
- 00:11:41to move this type here to the back sorry
- 00:11:46okay perfect and before I do it take a
- 00:11:48guess what do we need to do to pull in
- 00:11:51the attributes that we are inheriting
- 00:11:53from item that's right super great
- 00:11:55answer guys and then we are going to do
- 00:11:58this. damage equals damage and then
- 00:12:00this. type equals type we're going to
- 00:12:02have get damage return damage and then
- 00:12:05we'll have public string git type
- 00:12:09wonderful and there we go so now that's
- 00:12:11weapon we've created the fruit we I've
- 00:12:14showed you how inheritance works by
- 00:12:17extending attributes and methods from
- 00:12:19the super class also note you can create
- 00:12:21Sub sub classes so specific types of
- 00:12:24weapon that extends weapons and then
- 00:12:25weapon extends item so you get the
- 00:12:28attributes and meth from all of the
- 00:12:30super classes above you now let's hop
- 00:12:32into polymorphism and this is just a
- 00:12:35single aspect of it as you see over in
- 00:12:37inventory we have item and then item.
- 00:12:39get name quantity item. getet quantity
- 00:12:43and it's going directly into um our item
- 00:12:46class however what we need to do is also
- 00:12:49have inventory let me show you over here
- 00:12:51inventory. display inventory display the
- 00:12:54sub classes of item so what we'll do is
- 00:12:57actually use two string
- 00:12:59so I don't know if you noticed earlier
- 00:13:01let me see if I can get it to do it
- 00:13:03again when I was typing out item here it
- 00:13:07recommended to do item. TW string and
- 00:13:10two string is actually a built-in method
- 00:13:12to Java for objects it returns a string
- 00:13:15representation of the object and the
- 00:13:17only reason I'm mentioning this is
- 00:13:18because of the sentence it is
- 00:13:20recommended that all subclasses override
- 00:13:23this method overriding and overloading
- 00:13:26are elements of polymorphism so what
- 00:13:29what we would have to do is come in here
- 00:13:31we're going to tag this with override
- 00:13:34public string to string and then we're
- 00:13:37going to return what we want which is
- 00:13:40effectively almost what this used to be
- 00:13:44not quite but what it used to be and I'm
- 00:13:46also just going to change this to item
- 00:13:47to string for the record because that's
- 00:13:50what we want whereas in here so we don't
- 00:13:52need all of this we just have name and
- 00:13:54we just have quantity since we have name
- 00:13:57and quantity in here already there're
- 00:13:59not the methods just the attributes and
- 00:14:02there we go so now when I come over to
- 00:14:04main because we already changed this to
- 00:14:07item two string because we have all of
- 00:14:09the other information right here in two
- 00:14:11string we run it we should get our
- 00:14:14inventory and there it is and that is
- 00:14:17overriding so we overrode the builtin
- 00:14:21two string and now we're going to
- 00:14:23override this item two string in fruit
- 00:14:27and in weapon so the two string in Fruit
- 00:14:29will output the fruit name the quantity
- 00:14:32and the type and in weapon we return the
- 00:14:34weapon quantity damage and type so now
- 00:14:38over in main. Java we can actually do
- 00:14:41this and I'm going to do it with the
- 00:14:42weapon as well weapon weapon equals new
- 00:14:46weapon what do we want this to be we
- 00:14:48want this to be a sword I think that
- 00:14:50would be cool let's say we have two
- 00:14:51swords we're dual wielding this sucker
- 00:14:54the damage is 75 and we have no context
- 00:14:58that could be really good really bad and
- 00:14:59it is a melee weapon and of course we
- 00:15:02have to inventory add item weapon and
- 00:15:06then display inventory right here should
- 00:15:09display all of this inventory in all of
- 00:15:11the specific Fields so there we go so
- 00:15:14this down here yeah it is just one
- 00:15:17concept of polymorphism and I should
- 00:15:19really clarify because technically what
- 00:15:21we've discussed with overriding is an
- 00:15:23example of runtime polymorphism which is
- 00:15:27dynamic and overriding things like this
- 00:15:29is how you achieve that type of
- 00:15:31polymorphism which allows a method to
- 00:15:34behave differently based on the object
- 00:15:37that invokes it so this method acts
- 00:15:40differently if item object invokes it
- 00:15:43and two string being the same method
- 00:15:45acts differently than that because it
- 00:15:47returns something different when the
- 00:15:48fruit object invokes it and then weapon
- 00:15:51even different than that even though it
- 00:15:53is accessed through a reference of the
- 00:15:55super class and I guess to clarify even
- 00:15:57further it's called run time
- 00:15:59polymorphism as opposed to compile time
- 00:16:01polymorphism which we'll talk about in a
- 00:16:03second with overloading runtime
- 00:16:05polymorphism occurs when a call to an
- 00:16:07overridden method overridden method is
- 00:16:10resolved at runtime rather than compile
- 00:16:13time so when you override this is
- 00:16:15happening at runtime when you overload
- 00:16:17that is happening at compile time so to
- 00:16:19call back to my last video about learn
- 00:16:22Java in 15 minutes world's shortest Java
- 00:16:24course when you compile the program you
- 00:16:27are creating bite code in these class
- 00:16:30files and then your class file goes to
- 00:16:33the jvm and runs in order to get all of
- 00:16:37our expected output like this compile
- 00:16:39time run time so now let's Implement
- 00:16:42overloading and I think we have a good
- 00:16:44opportunity to do this in our inventory
- 00:16:47class and the idea is that you have
- 00:16:50multiple methods with the same name that
- 00:16:53take different parameters and can do
- 00:16:56different things basically so where did
- 00:16:59we call this you remember we called this
- 00:17:01over in main Java when we went to add
- 00:17:03item item to inventory whereas item
- 00:17:06contains this information right here and
- 00:17:08actually what I'm about to do I think it
- 00:17:10makes it makes the code worse basically
- 00:17:13so I didn't find a really good example
- 00:17:16for overloading for polymorphism
- 00:17:18overloading in this code base but bear
- 00:17:21with me here we're just going to use
- 00:17:23this as an
- 00:17:24example and over in inventory we are
- 00:17:27going to create public void add item
- 00:17:31remember we are overloading this method
- 00:17:34to do other things than just item item
- 00:17:37items add item item okay take a shot
- 00:17:39every time I say item I'm just kidding I
- 00:17:41don't want you to die so you want to add
- 00:17:43in all of the parameters for fruit
- 00:17:46because right now what we're doing is we
- 00:17:49are bypassing the need because right now
- 00:17:52it is a need the need to store all of
- 00:17:55this information into a fruit object
- 00:17:58object here and instead we will call it
- 00:18:01all directly an add item which if you
- 00:18:03saw my last video you know that I don't
- 00:18:04like doing it this way it's typically
- 00:18:05not good I like to stored in something
- 00:18:07and then I can reuse this as much as
- 00:18:09possible but again overloaded example
- 00:18:12not best practice example let's move
- 00:18:15that over perfect it was a weird work it
- 00:18:17was weird wrap over here you know what I
- 00:18:19mean I don't like that there kind of
- 00:18:20it's ugly so boom and then I'm going to
- 00:18:22do exactly what this does but with this
- 00:18:24so items come on autocomplete come in
- 00:18:27handy please we're going to create new
- 00:18:29fruit and then we're going to do name
- 00:18:31quantity type perfect we can also do
- 00:18:33this with weapon actually cuz we have
- 00:18:35two sub classes for it add item yeah I'm
- 00:18:38not going to make you watch me type all
- 00:18:40that but also the word wrap is still
- 00:18:41annoying whatever anyway what you may
- 00:18:43notice is that this add item is being
- 00:18:45used and these ad items are grayed out
- 00:18:48they are not being used because when we
- 00:18:50are using it here we're only passing in
- 00:18:52one parameter however if we change it to
- 00:18:56fit the format as this ad item and this
- 00:19:01ad item then it'll know which ad item we
- 00:19:03are talking about and it'll use that
- 00:19:06specific method so over here let's do it
- 00:19:08instead of this fruit right here let me
- 00:19:10just comment this out instead of that
- 00:19:12fruit instead of that weapon what I'm
- 00:19:14going to do is actually I'm just going
- 00:19:15to copy and paste because that makes my
- 00:19:17life easy copy copy there we go and what
- 00:19:20should occur is that we have the same
- 00:19:23exact output as we had before boom
- 00:19:26because we are adding the same exact
- 00:19:28thing as before just in a different way
- 00:19:31in a in my opinion worse way we are
- 00:19:34doing new fruit here whereas before we
- 00:19:36did it right here and then we are giving
- 00:19:39the values right here in the add item
- 00:19:41which are then being passed in as
- 00:19:43parameters right here and back into here
- 00:19:47in order to add this um object fruit
- 00:19:52into items into
- 00:19:55inventory huh that's interesting it
- 00:19:57doesn't show is blue even though it's
- 00:19:59obviously being used that's weird anyway
- 00:20:02I could also do the same thing with
- 00:20:03display inventory like this right here
- 00:20:05what I'm doing is I'm only displaying
- 00:20:07items by type so if I come over here and
- 00:20:11I do exactly like this and I pass in the
- 00:20:13parameter a specific type which is Fuji
- 00:20:16in this instance is it'll display that
- 00:20:19specific fruit with that specific type
- 00:20:21and I mean this is pretty obvious but if
- 00:20:23I type in melee which is also a type
- 00:20:25it'll display that as well and that is
- 00:20:29method overloading which again allows
- 00:20:31multiple methods in the same class to
- 00:20:33have the same name but with different
- 00:20:35parameters and this is how you achieve
- 00:20:38compiled time polymorphism or in other
- 00:20:41words static polymorphism method
- 00:20:43overloading as well as operator
- 00:20:45overloading and why would we want to do
- 00:20:46something like this well one uh it makes
- 00:20:49it easier to read I mean at least for me
- 00:20:50it does compile time polymorphism with
- 00:20:53overloading allows for well errors to be
- 00:20:56caught in compile time as opposed to
- 00:20:58runtime because this doesn't run in
- 00:21:00runtime if we want to use this one or
- 00:21:02this one or this one all of that is done
- 00:21:04and decided in compiled time no
- 00:21:06additional checks during runtime so it
- 00:21:09also increases performance whereas back
- 00:21:11to overriding overriding does run at
- 00:21:13runtime and it it performs runtime
- 00:21:16polymorphism and that is when you want
- 00:21:18something to be more Dynamic more
- 00:21:20flexible and it also allows you to reuse
- 00:21:23these methods in whichever way you see
- 00:21:25fit
- 00:21:26polymorphism oh yeah just to click CL
- 00:21:28ify these overridden uh methods as well
- 00:21:32as the overloading of methods while our
- 00:21:36both ways to achieve polymorphism they
- 00:21:39are also a part of encapsulation because
- 00:21:41encapsulation is all about bundling the
- 00:21:44data being the
- 00:21:46attributes as well as the methods with
- 00:21:49two string being a method into a single
- 00:21:52unit that single unit being item all
- 00:21:55right so I did
- 00:21:57encapsulation I did inheritance I did
- 00:22:01polymorphism oh crap I didn't do uh
- 00:22:04abstraction so abstraction on like like
- 00:22:07a broad stroke the broad definition of
- 00:22:09abstraction is the concept of hiding
- 00:22:11complex implementation details and
- 00:22:13showing only the essential features of
- 00:22:16an object which as a programmer when you
- 00:22:18implement it it it helps to reduce
- 00:22:20complexity and and allows you to focus
- 00:22:22on interactions at a higher level and
- 00:22:24there are a few ways to implement it you
- 00:22:26can use abstract for a class you can use
- 00:22:30abstract for a method and we'll get to
- 00:22:31that in a second let's start with
- 00:22:32abstract for a class and let's do class
- 00:22:35item and what this does it it means that
- 00:22:37we can no longer use class item to
- 00:22:40create an object like why why would we
- 00:22:42want to do that well do we really want
- 00:22:45generic item over here or do we only
- 00:22:48want to be able to create the specific
- 00:22:50subclasses of item we want to do the
- 00:22:53ladder so we're going to make item
- 00:22:55abstract which is why this is yelling at
- 00:22:57us by the way because it's it's saying
- 00:22:59hey that's abstract you you can't you
- 00:23:01can't do that so I'm going to comment
- 00:23:03that out okay and that also means that
- 00:23:04we actually don't need this override
- 00:23:07method at all we need it in our weapon
- 00:23:09and in our fruit but we don't need the
- 00:23:12two- string method here because there
- 00:23:14are going to be no specific item objects
- 00:23:17only the subclasses of item objects and
- 00:23:20we can create an abstract method right
- 00:23:22here actually let me do public abstract
- 00:23:26void display
- 00:23:28info boom wait a second this is a method
- 00:23:32aren't there supposed to be parentheses
- 00:23:33and then you return something or at
- 00:23:35least do something like over in here
- 00:23:37you're adding item to item well that's
- 00:23:39the point of an abstract method is that
- 00:23:43you're not doing anything right here
- 00:23:46other than making the subclasses
- 00:23:49Implement display info which means we
- 00:23:53would overwrite it void
- 00:23:56display info like that but of course
- 00:23:59we'd have to put this into a system out
- 00:24:01printland just because that's proper
- 00:24:03formatting and that's why it's no longer
- 00:24:04yelling at us and why fruit is yelling
- 00:24:06at us because we aren't using display
- 00:24:09info in Fruit even though it's required
- 00:24:12based on this abstract class item and
- 00:24:14instead of void and changing all of this
- 00:24:16to system out print L we could have kept
- 00:24:19it string make this string and when I
- 00:24:21say kept it string I mean like two
- 00:24:23string was string and then we would
- 00:24:24return like this and we could have done
- 00:24:26it just like this it just depend depends
- 00:24:28on your use case this is better when you
- 00:24:31want to give the caller the flexibility
- 00:24:33to decide how to use this information
- 00:24:35you want to console log it you want to
- 00:24:37add it to the UI you want to system out
- 00:24:39print Lin you can do whatever you want
- 00:24:41however if you want to be more strict
- 00:24:43that's when you would do something like
- 00:24:45void because now we are forcing it to do
- 00:24:48system out print Lin which encapsulates
- 00:24:51the display Behavior within the class
- 00:24:53and then of course over in inventory
- 00:24:55instead of displaying item to string you
- 00:24:58would want to do actually since we're
- 00:25:00already doing system out print L within
- 00:25:02display info all you do is item. display
- 00:25:05info and well that's how you display
- 00:25:07inventory now however there's also
- 00:25:09another part of abstraction and that is
- 00:25:12an interface so with an abstract class
- 00:25:15like this you're able to have your
- 00:25:17private uh attributes right here you're
- 00:25:19able to have a Constructor you're able
- 00:25:21to have concrete methods so just public
- 00:25:24regular methods you're also able to have
- 00:25:26abstract methods however an interface is
- 00:25:30a little bit different if we want to
- 00:25:31come over here and do new uh I guess
- 00:25:34Java class interface there we go and
- 00:25:36we'll call this item stuff maybe we
- 00:25:38would change the item class abstract
- 00:25:41class to an interface but for this
- 00:25:43example we're just going to call this
- 00:25:44item stuff and create a new Java file
- 00:25:47and within the interface you're going to
- 00:25:48have common methods that all items must
- 00:25:52Implement so get name get quantity and
- 00:25:55display info as you notice these are the
- 00:25:58same methods that we actually have over
- 00:26:01here get name get quantity display info
- 00:26:03and you also May notice that we do not
- 00:26:05have the abstract keyword right here
- 00:26:08that is because everything in an
- 00:26:10interface is assumed to be abstract we
- 00:26:12don't have a Constructor like here we
- 00:26:14don't have all of these attributes like
- 00:26:16here all we have are methods that must
- 00:26:19be overridden so over in Fruit if we
- 00:26:21want to do it here instead of extending
- 00:26:24item because that is a class abstract or
- 00:26:27not that's how you do it you would
- 00:26:28actually
- 00:26:29Implement item stuff that is because
- 00:26:33it's not it's not inheriting anything
- 00:26:36what it's doing is it is forcing fruit
- 00:26:38to implement everything that item stuff
- 00:26:41has which are actually what we have over
- 00:26:44here so actually we already have display
- 00:26:45info I'm just going to copy and paste to
- 00:26:48make my life easy and then we have to
- 00:26:50add the attributes in here private int
- 00:26:53quantity private string name I did that
- 00:26:57out of order that that's better and also
- 00:26:59since we are not inheriting it we have
- 00:27:02to do this the oldfashioned way and
- 00:27:04there we are we have everything that we
- 00:27:06need from item stuff the stuff that it's
- 00:27:08making us do but we have to add all of
- 00:27:11those methods and overwrite all of these
- 00:27:13are overriding not that one but all of
- 00:27:15these are overriding there we go that
- 00:27:17makes more sense they're
- 00:27:18overriding this right here and we're
- 00:27:21also declaring all of our private
- 00:27:23attributes in here and then if we do
- 00:27:26this to weapon we would have to do the
- 00:27:27same exact thing for every single one
- 00:27:30that implements item stuff as opposed to
- 00:27:32having the common attributes in here and
- 00:27:34the common methods in here as well so
- 00:27:37why in the world would we want to do an
- 00:27:40interface where we have to rewrite all
- 00:27:41of this code every single time as
- 00:27:43opposed to having a class and just doing
- 00:27:45it once and then if we need anything
- 00:27:46abstract we can just do an abstract
- 00:27:47method in an abstract class well one
- 00:27:49interesting note is that when you
- 00:27:52implement things you can Implement as
- 00:27:54many as you want you would just do
- 00:27:55implements item other stuff pretend that
- 00:27:59that is an interface that we actually
- 00:28:00have and you can do this as many times
- 00:28:02as you want you could also do fruit
- 00:28:04extends item and implements item stuff
- 00:28:08so that's an interesting point but also
- 00:28:10if it doesn't have a clear hierarchical
- 00:28:13that's a difficult word hierarchical
- 00:28:15relationship so like I a weapon and a
- 00:28:17fruit is an item but what if I had
- 00:28:19something else that wasn't an item but I
- 00:28:21also wanted it to have item stuff then I
- 00:28:23could have a weapon implement or let's
- 00:28:25go with fruits and St there a fruit
- 00:28:27Implement item stuff
- 00:28:28as well as a a a horse a horse is not
- 00:28:31going to be something that you keep in
- 00:28:32your inventory I don't think so let's
- 00:28:34pretend it's not but let's pretend you
- 00:28:36also want the horse to have a git name
- 00:28:38git quantity and then display info you
- 00:28:40would Implement item stuff so that it
- 00:28:42forces that horse to have these um
- 00:28:45methods so that uh I hope that helped
- 00:28:47that's that's abstraction so now try to
- 00:28:49go build something yourself because of
- 00:28:51all you're doing is watching videos like
- 00:28:54this and watching the perfect way to do
- 00:28:57it instead of going through trial and
- 00:28:59error yourself and messing things up and
- 00:29:01then trying to figure out how to fix the
- 00:29:03things that you messed up and going like
- 00:29:05this until you finally get to the proper
- 00:29:09way to do it well you're not going to
- 00:29:11learn so this is your call to action
- 00:29:13right after well call to action like the
- 00:29:15video subscribe to the channel turn on
- 00:29:16the notification Bell but here's your
- 00:29:18call to action to start coding maybe you
- 00:29:21were following along coding this if so
- 00:29:23awesome now dive into the code and
- 00:29:27change things manipulate things try new
- 00:29:30things break it fix it break it again
- 00:29:33fix it again that's how you learn hope
- 00:29:36you enjoyed it there's so much more to
- 00:29:37Java that I can also discuss maybe in a
- 00:29:39future video y'all just let me know okay
- 00:29:42y'all have a good one
- Object-Oriented Programming
- Java
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
- ArrayList
- Code Examples
- Learning Java
- Software Development