Learn Java Object-Oriented Programming (with actual code)

00:29:44
https://www.youtube.com/watch?v=TiccevwEVe8

概要

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.

ビデオをもっと見る

AIを活用したYouTubeの無料動画要約に即アクセス!
字幕
en
オートスクロール:
  • 00:00:00
    so my only goal with this video is to
  • 00:00:01
    have you understand object-oriented
  • 00:00:03
    programming at the end of it that's it
  • 00:00:06
    it's a lot more simple than how I feel
  • 00:00:08
    like a lot of people make it sound
  • 00:00:09
    there's a lot of big words encapsulation
  • 00:00:12
    abstraction inheritance polymorphism
  • 00:00:15
    those are the four main principles of
  • 00:00:17
    object-oriented programming and instead
  • 00:00:19
    of me sitting here talking about it what
  • 00:00:21
    I'm going to do is code it and explain
  • 00:00:24
    how each one of these things are working
  • 00:00:27
    as we code I think that's the best way
  • 00:00:28
    to go about it so in order to talk about
  • 00:00:30
    those four principles and we're going to
  • 00:00:31
    start off with encapsulation we first
  • 00:00:33
    have to make a class so I'm going to
  • 00:00:35
    create an inventory system like you
  • 00:00:37
    would find in a video game and what does
  • 00:00:39
    that inventory hold items so we're going
  • 00:00:41
    to create an a Java class and we're
  • 00:00:43
    going to call it item so you can think
  • 00:00:45
    of a Java class as a template or a
  • 00:00:48
    blueprint to create new items and every
  • 00:00:52
    new item that you make is an object
  • 00:00:55
    hence the term objectoriented
  • 00:00:58
    programming It's Not Over compc Li at
  • 00:01:00
    it's quite literally an object like the
  • 00:01:02
    phone on your desk the cup on your desk
  • 00:01:03
    The Sword in your inventory those are
  • 00:01:06
    objects and what of those objects need
  • 00:01:08
    well they need some attributes so
  • 00:01:11
    they're going to need a name so we'll
  • 00:01:12
    save that name as a string and they're
  • 00:01:14
    also going to have a quantity like you
  • 00:01:16
    can have 30 apples in your inventory so
  • 00:01:19
    that is going to be an integer and we
  • 00:01:22
    went over data types and everything
  • 00:01:23
    leading up to this point in the last
  • 00:01:25
    video about learning Java in 15 minutes
  • 00:01:27
    the world's shortest Java course
  • 00:01:29
    whatever I named it we didn't go over
  • 00:01:31
    private though so private as opposed to
  • 00:01:33
    public means that they cannot be
  • 00:01:36
    directly accessed from outside this
  • 00:01:39
    class in order to access them and assign
  • 00:01:42
    values to name and assign values to
  • 00:01:44
    quantity which again will be different
  • 00:01:46
    for every single item which is why we're
  • 00:01:48
    not assigning a value of of item or
  • 00:01:51
    anything to it right now the way to do
  • 00:01:54
    this is via getter methods string get
  • 00:01:57
    name and we're going to return name and
  • 00:01:59
    then public int get quantity and we're
  • 00:02:03
    going to return quantity these two
  • 00:02:05
    methods allow you to access and modify
  • 00:02:08
    these private protected attributes and
  • 00:02:12
    this right here in this instance these
  • 00:02:14
    two attributes and these two methods
  • 00:02:16
    that allow you to access and modify
  • 00:02:18
    these private attributes is
  • 00:02:20
    encapsulation it keeps our details
  • 00:02:22
    inside this class safe and provides
  • 00:02:25
    controlled ways to access or modify it
  • 00:02:28
    now in order to be able to use all of
  • 00:02:30
    this remember this is uh so far it's not
  • 00:02:32
    a complete blueprint but it is the class
  • 00:02:35
    is a blueprint in order to create an
  • 00:02:37
    actual object using this blueprint we
  • 00:02:40
    are going to create a Constructor which
  • 00:02:42
    is not called Constructor it is it is a
  • 00:02:44
    special method that you call the same as
  • 00:02:46
    your class and this name this name this
  • 00:02:49
    quantity this quantity and the this
  • 00:02:51
    keyword is just Java allowing you to
  • 00:02:54
    have your parameters be the same name as
  • 00:02:57
    your attributes instead of having to
  • 00:02:59
    name it like like name two quantity two
  • 00:03:02
    and then you'd have to come and remove
  • 00:03:04
    this and then add two to each one of
  • 00:03:07
    those it it's the same thing just uh a
  • 00:03:10
    much better and cleaner way to go about
  • 00:03:12
    it like this now we're going to call
  • 00:03:13
    this Constructor the special method in
  • 00:03:15
    order to create an object using this
  • 00:03:17
    template but first we have to create our
  • 00:03:19
    class inventory because inventory is
  • 00:03:22
    what's going to be holding these items
  • 00:03:25
    and displaying these items so we're
  • 00:03:27
    going to call it inventory so in
  • 00:03:29
    inventory we have to store those items
  • 00:03:30
    so we're going to create another private
  • 00:03:32
    field but we're going to use something
  • 00:03:33
    we haven't discussed before and that is
  • 00:03:35
    an array list an array list of item
  • 00:03:38
    items so an array list is a collection
  • 00:03:41
    which is a specialized subset of
  • 00:03:43
    non-primitive data types it stores and
  • 00:03:45
    manages groups of objects and it is
  • 00:03:47
    predefined as you can see up here the
  • 00:03:49
    automatic Import in java.util.arrays so
  • 00:03:52
    Java has it built in now let me to
  • 00:03:54
    repeat part of that it is to store and
  • 00:03:57
    manage groups of objects whereas an
  • 00:03:59
    array for example could store strings it
  • 00:04:03
    can store integers but it cannot store
  • 00:04:06
    objects whereas the array list can
  • 00:04:09
    restore objects and is also dynamically
  • 00:04:12
    resizable whereas arrays by definition
  • 00:04:15
    are fixed even though in JavaScript and
  • 00:04:17
    other languages they're not fixed they
  • 00:04:18
    are resizable in Java they're fixed they
  • 00:04:20
    don't store objects so we have to use an
  • 00:04:22
    array list to store items objects now
  • 00:04:25
    what I have to do is create the
  • 00:04:27
    Constructor 4 inventory whereas we don't
  • 00:04:30
    have any parameters that we're passing
  • 00:04:31
    through we're just creating a new array
  • 00:04:33
    list called items and just to clarify
  • 00:04:36
    that statement this is where we are
  • 00:04:38
    creating or declaring the array list
  • 00:04:41
    called items of item or in other words
  • 00:04:43
    item objects and within our inventory
  • 00:04:46
    Constructor we are assigning a value of
  • 00:04:49
    new empty array list to that array list
  • 00:04:52
    of items because we're going to use a
  • 00:04:54
    method to add items to this items array
  • 00:04:58
    list and we do that with public void add
  • 00:05:01
    item item item items add item but the
  • 00:05:04
    way this works is that when you use this
  • 00:05:06
    method you're passing in a parameter of
  • 00:05:08
    item which we're going to create over
  • 00:05:10
    here in just a second and then that is
  • 00:05:13
    going to be added to this items array
  • 00:05:17
    list that we created or in other words
  • 00:05:20
    this method is adding the item into the
  • 00:05:23
    inventory but also we want to be able to
  • 00:05:25
    display that inventory we also want to
  • 00:05:27
    remove item but I'm not going to do that
  • 00:05:29
    right now want to display inventory
  • 00:05:32
    we're going to use a for Loop for this
  • 00:05:34
    item item system out printland item and
  • 00:05:36
    I could make this a little bit neater
  • 00:05:38
    hold on so this is how I make it
  • 00:05:39
    prettier we're going to have item print
  • 00:05:40
    out and then the name of the item and
  • 00:05:42
    then the quantity print out and then the
  • 00:05:44
    actual quantity of the item oh all right
  • 00:05:46
    now that we have our inventory class and
  • 00:05:48
    with our private attributes and then our
  • 00:05:51
    public methods that is encapsulation
  • 00:05:53
    allowing us to have access to these
  • 00:05:55
    private attributes and then we have our
  • 00:05:57
    Constructor right here that all allows
  • 00:05:59
    us to use this class template in order
  • 00:06:02
    to create a new inventory object as well
  • 00:06:05
    as the same thing over in item well how
  • 00:06:08
    about we create those so we're going to
  • 00:06:09
    start off with creating inventory and
  • 00:06:12
    we're going to call it inventory equals
  • 00:06:14
    new inventory that may be a little
  • 00:06:16
    confusing because everything has the
  • 00:06:17
    same name but think of it just like
  • 00:06:19
    string string equals string just like
  • 00:06:23
    this so what's happening is that we're
  • 00:06:24
    creating an inventory object called
  • 00:06:28
    inventory just like here we creating a
  • 00:06:31
    string called string which we're
  • 00:06:33
    assigning the value of string which
  • 00:06:36
    we're assigning the value of an empty
  • 00:06:39
    inventory object with this inventory
  • 00:06:42
    right here being the Constructor and by
  • 00:06:44
    the way if you don't know how to drill
  • 00:06:45
    in you just control click and it'll take
  • 00:06:47
    you to exactly what it is and then this
  • 00:06:49
    inventory over here is this class this
  • 00:06:53
    entire class so then we're going to do
  • 00:06:55
    the same thing with item so we're going
  • 00:06:57
    to call Item we're going to call this
  • 00:06:58
    item one cuz we're going create two
  • 00:07:00
    items new item and we're going to call
  • 00:07:02
    this item apples and we're going to give
  • 00:07:04
    it a quantity of 20 we have 20 apples
  • 00:07:07
    and then we're going to do item two but
  • 00:07:09
    instead of apples we're going to make it
  • 00:07:10
    a sword instead of 20 we're going to
  • 00:07:12
    make it two because we're dual wielding
  • 00:07:14
    these suckers we have two swords well in
  • 00:07:16
    our inventory not in our hands actually
  • 00:07:17
    that's not true we have to add these
  • 00:07:19
    items these new objects that we just
  • 00:07:21
    created into our inventory and the way
  • 00:07:24
    to do that is add item one to inventory
  • 00:07:27
    add item two to inventory and and then
  • 00:07:29
    we want to display set inventory with
  • 00:07:32
    our display inventory method and what
  • 00:07:34
    should happen is that well we should be
  • 00:07:38
    displaying these two pieces of inventory
  • 00:07:41
    right here item apples quantity 20 item
  • 00:07:43
    sword quantity 2 and if you recall that
  • 00:07:47
    is exactly the format that we have over
  • 00:07:49
    here in our for Loop item Apple's
  • 00:07:52
    quantity 20 now let's hop into
  • 00:07:54
    inheritance so we're going to create a
  • 00:07:56
    new class and that is going to be called
  • 00:07:58
    let's go with fruit so fruit for this
  • 00:08:01
    instance is going to be a very basic
  • 00:08:03
    example and then we're going to do
  • 00:08:05
    weapons after this so you can see just
  • 00:08:07
    how fruit and weapons differ from each
  • 00:08:10
    other but also are similar because they
  • 00:08:12
    have the same attributes as item that'll
  • 00:08:14
    make all it all makes sense in a second
  • 00:08:16
    so the way we do this is that fruit
  • 00:08:19
    extends item item is the super class
  • 00:08:23
    fruit is the subclass 2 item and it
  • 00:08:27
    inherits the attributes and any other
  • 00:08:29
    methods that are in this class item as
  • 00:08:32
    its own so we don't have to come in here
  • 00:08:35
    what is it string name String quty we
  • 00:08:36
    don't have to come in here we know that
  • 00:08:38
    the fruit has a name and a quantity
  • 00:08:39
    right we don't have to come in here and
  • 00:08:40
    do string name and private int quantity
  • 00:08:44
    we don't have to do this because we are
  • 00:08:46
    already inheriting that what we do is
  • 00:08:48
    actually give it specific attributes
  • 00:08:50
    like type is it a banana is it an apple
  • 00:08:53
    is it an orange so then we're going to
  • 00:08:56
    create the Constructor of fruit so we
  • 00:08:58
    can initialize the fruit we're going to
  • 00:09:00
    give it string type because that's the
  • 00:09:02
    attribute that we have up here but we're
  • 00:09:04
    also going to give it the same exact
  • 00:09:06
    attributes that we are inheriting from
  • 00:09:09
    item so what is that quantity and this
  • 00:09:12
    super name quantity right here is
  • 00:09:14
    actually how you assign the values being
  • 00:09:15
    passed in as parameters over here from
  • 00:09:18
    your super class so instead of doing
  • 00:09:20
    this name equals name this quantity
  • 00:09:23
    equals quantity because it is in your
  • 00:09:24
    super class remember item is the super
  • 00:09:27
    class to fruit fruit being the sub class
  • 00:09:29
    and item containing these attributes
  • 00:09:32
    this is just how you do it so now let's
  • 00:09:33
    use this template and our Constructor to
  • 00:09:36
    initialize a new object of fruit so item
  • 00:09:39
    we don't need this let's just do one
  • 00:09:41
    item we already have something for fruit
  • 00:09:42
    so let's call this generic item just to
  • 00:09:44
    kind of get things out of the way there
  • 00:09:47
    and we're going to create an instance of
  • 00:09:48
    fruit called fruit equals new fruit and
  • 00:09:51
    the first deal is a type I believe yes
  • 00:09:53
    it is so what type of apple should we do
  • 00:09:56
    these are the types I'm thinking of uh I
  • 00:09:58
    don't see a granny Smith up here let's
  • 00:10:00
    go with the Fuji so the type is of Fuji
  • 00:10:03
    the name I believe is next and that is
  • 00:10:06
    Apple and we're going to have a quantity
  • 00:10:09
    of 20 Fuji apples and since we changed
  • 00:10:11
    the name up here as you can see it's not
  • 00:10:13
    being used so we have to make that right
  • 00:10:15
    and instead of add item item two we're
  • 00:10:17
    going to add item fruit so now inventory
  • 00:10:20
    should have generic item 10 and then
  • 00:10:23
    fruit uh Fuji apple 20 when we run it
  • 00:10:27
    and there we have it item generic item
  • 00:10:29
    10 item Apple quantity 20 oh we are not
  • 00:10:32
    printing out the correct stuff over in
  • 00:10:34
    inventory now are we and the reason
  • 00:10:36
    being is that I wanted to have
  • 00:10:38
    polymorphism be its own lesson so I
  • 00:10:40
    didn't do it along the way here so do I
  • 00:10:43
    do polymorphism now or do I do the
  • 00:10:46
    weapon now let me do the weapon and then
  • 00:10:48
    we'll do polymorphism and then we'll be
  • 00:10:50
    able to print things out properly and
  • 00:10:52
    display inventory by using override
  • 00:10:54
    we'll get to that in a second though so
  • 00:10:56
    let's create Java class weapon and a lot
  • 00:10:59
    of this is going to be exactly all of
  • 00:11:01
    this should be exactly what we just saw
  • 00:11:03
    we're going to have its own unique
  • 00:11:05
    attributes we're going to have damage
  • 00:11:07
    and method is also going to have a type
  • 00:11:09
    so I guess in a way we could have put
  • 00:11:10
    that into item. Java as opposed to in
  • 00:11:13
    each individual weapon and fruit but
  • 00:11:16
    that's okay we're going to create the
  • 00:11:19
    Constructor weapon and I'm going to do
  • 00:11:21
    this in a little bit different order I
  • 00:11:22
    like to have the name first and then the
  • 00:11:25
    quantity second and then we'll add in
  • 00:11:28
    damage and then string type so then the
  • 00:11:32
    type there which also makes me want to
  • 00:11:34
    come through here and move string type
  • 00:11:37
    here to the back which means I also have
  • 00:11:41
    to move this type here to the back sorry
  • 00:11:46
    okay perfect and before I do it take a
  • 00:11:48
    guess what do we need to do to pull in
  • 00:11:51
    the attributes that we are inheriting
  • 00:11:53
    from item that's right super great
  • 00:11:55
    answer guys and then we are going to do
  • 00:11:58
    this. damage equals damage and then
  • 00:12:00
    this. type equals type we're going to
  • 00:12:02
    have get damage return damage and then
  • 00:12:05
    we'll have public string git type
  • 00:12:09
    wonderful and there we go so now that's
  • 00:12:11
    weapon we've created the fruit we I've
  • 00:12:14
    showed you how inheritance works by
  • 00:12:17
    extending attributes and methods from
  • 00:12:19
    the super class also note you can create
  • 00:12:21
    Sub sub classes so specific types of
  • 00:12:24
    weapon that extends weapons and then
  • 00:12:25
    weapon extends item so you get the
  • 00:12:28
    attributes and meth from all of the
  • 00:12:30
    super classes above you now let's hop
  • 00:12:32
    into polymorphism and this is just a
  • 00:12:35
    single aspect of it as you see over in
  • 00:12:37
    inventory we have item and then item.
  • 00:12:39
    get name quantity item. getet quantity
  • 00:12:43
    and it's going directly into um our item
  • 00:12:46
    class however what we need to do is also
  • 00:12:49
    have inventory let me show you over here
  • 00:12:51
    inventory. display inventory display the
  • 00:12:54
    sub classes of item so what we'll do is
  • 00:12:57
    actually use two string
  • 00:12:59
    so I don't know if you noticed earlier
  • 00:13:01
    let me see if I can get it to do it
  • 00:13:03
    again when I was typing out item here it
  • 00:13:07
    recommended to do item. TW string and
  • 00:13:10
    two string is actually a built-in method
  • 00:13:12
    to Java for objects it returns a string
  • 00:13:15
    representation of the object and the
  • 00:13:17
    only reason I'm mentioning this is
  • 00:13:18
    because of the sentence it is
  • 00:13:20
    recommended that all subclasses override
  • 00:13:23
    this method overriding and overloading
  • 00:13:26
    are elements of polymorphism so what
  • 00:13:29
    what we would have to do is come in here
  • 00:13:31
    we're going to tag this with override
  • 00:13:34
    public string to string and then we're
  • 00:13:37
    going to return what we want which is
  • 00:13:40
    effectively almost what this used to be
  • 00:13:44
    not quite but what it used to be and I'm
  • 00:13:46
    also just going to change this to item
  • 00:13:47
    to string for the record because that's
  • 00:13:50
    what we want whereas in here so we don't
  • 00:13:52
    need all of this we just have name and
  • 00:13:54
    we just have quantity since we have name
  • 00:13:57
    and quantity in here already there're
  • 00:13:59
    not the methods just the attributes and
  • 00:14:02
    there we go so now when I come over to
  • 00:14:04
    main because we already changed this to
  • 00:14:07
    item two string because we have all of
  • 00:14:09
    the other information right here in two
  • 00:14:11
    string we run it we should get our
  • 00:14:14
    inventory and there it is and that is
  • 00:14:17
    overriding so we overrode the builtin
  • 00:14:21
    two string and now we're going to
  • 00:14:23
    override this item two string in fruit
  • 00:14:27
    and in weapon so the two string in Fruit
  • 00:14:29
    will output the fruit name the quantity
  • 00:14:32
    and the type and in weapon we return the
  • 00:14:34
    weapon quantity damage and type so now
  • 00:14:38
    over in main. Java we can actually do
  • 00:14:41
    this and I'm going to do it with the
  • 00:14:42
    weapon as well weapon weapon equals new
  • 00:14:46
    weapon what do we want this to be we
  • 00:14:48
    want this to be a sword I think that
  • 00:14:50
    would be cool let's say we have two
  • 00:14:51
    swords we're dual wielding this sucker
  • 00:14:54
    the damage is 75 and we have no context
  • 00:14:58
    that could be really good really bad and
  • 00:14:59
    it is a melee weapon and of course we
  • 00:15:02
    have to inventory add item weapon and
  • 00:15:06
    then display inventory right here should
  • 00:15:09
    display all of this inventory in all of
  • 00:15:11
    the specific Fields so there we go so
  • 00:15:14
    this down here yeah it is just one
  • 00:15:17
    concept of polymorphism and I should
  • 00:15:19
    really clarify because technically what
  • 00:15:21
    we've discussed with overriding is an
  • 00:15:23
    example of runtime polymorphism which is
  • 00:15:27
    dynamic and overriding things like this
  • 00:15:29
    is how you achieve that type of
  • 00:15:31
    polymorphism which allows a method to
  • 00:15:34
    behave differently based on the object
  • 00:15:37
    that invokes it so this method acts
  • 00:15:40
    differently if item object invokes it
  • 00:15:43
    and two string being the same method
  • 00:15:45
    acts differently than that because it
  • 00:15:47
    returns something different when the
  • 00:15:48
    fruit object invokes it and then weapon
  • 00:15:51
    even different than that even though it
  • 00:15:53
    is accessed through a reference of the
  • 00:15:55
    super class and I guess to clarify even
  • 00:15:57
    further it's called run time
  • 00:15:59
    polymorphism as opposed to compile time
  • 00:16:01
    polymorphism which we'll talk about in a
  • 00:16:03
    second with overloading runtime
  • 00:16:05
    polymorphism occurs when a call to an
  • 00:16:07
    overridden method overridden method is
  • 00:16:10
    resolved at runtime rather than compile
  • 00:16:13
    time so when you override this is
  • 00:16:15
    happening at runtime when you overload
  • 00:16:17
    that is happening at compile time so to
  • 00:16:19
    call back to my last video about learn
  • 00:16:22
    Java in 15 minutes world's shortest Java
  • 00:16:24
    course when you compile the program you
  • 00:16:27
    are creating bite code in these class
  • 00:16:30
    files and then your class file goes to
  • 00:16:33
    the jvm and runs in order to get all of
  • 00:16:37
    our expected output like this compile
  • 00:16:39
    time run time so now let's Implement
  • 00:16:42
    overloading and I think we have a good
  • 00:16:44
    opportunity to do this in our inventory
  • 00:16:47
    class and the idea is that you have
  • 00:16:50
    multiple methods with the same name that
  • 00:16:53
    take different parameters and can do
  • 00:16:56
    different things basically so where did
  • 00:16:59
    we call this you remember we called this
  • 00:17:01
    over in main Java when we went to add
  • 00:17:03
    item item to inventory whereas item
  • 00:17:06
    contains this information right here and
  • 00:17:08
    actually what I'm about to do I think it
  • 00:17:10
    makes it makes the code worse basically
  • 00:17:13
    so I didn't find a really good example
  • 00:17:16
    for overloading for polymorphism
  • 00:17:18
    overloading in this code base but bear
  • 00:17:21
    with me here we're just going to use
  • 00:17:23
    this as an
  • 00:17:24
    example and over in inventory we are
  • 00:17:27
    going to create public void add item
  • 00:17:31
    remember we are overloading this method
  • 00:17:34
    to do other things than just item item
  • 00:17:37
    items add item item okay take a shot
  • 00:17:39
    every time I say item I'm just kidding I
  • 00:17:41
    don't want you to die so you want to add
  • 00:17:43
    in all of the parameters for fruit
  • 00:17:46
    because right now what we're doing is we
  • 00:17:49
    are bypassing the need because right now
  • 00:17:52
    it is a need the need to store all of
  • 00:17:55
    this information into a fruit object
  • 00:17:58
    object here and instead we will call it
  • 00:18:01
    all directly an add item which if you
  • 00:18:03
    saw my last video you know that I don't
  • 00:18:04
    like doing it this way it's typically
  • 00:18:05
    not good I like to stored in something
  • 00:18:07
    and then I can reuse this as much as
  • 00:18:09
    possible but again overloaded example
  • 00:18:12
    not best practice example let's move
  • 00:18:15
    that over perfect it was a weird work it
  • 00:18:17
    was weird wrap over here you know what I
  • 00:18:19
    mean I don't like that there kind of
  • 00:18:20
    it's ugly so boom and then I'm going to
  • 00:18:22
    do exactly what this does but with this
  • 00:18:24
    so items come on autocomplete come in
  • 00:18:27
    handy please we're going to create new
  • 00:18:29
    fruit and then we're going to do name
  • 00:18:31
    quantity type perfect we can also do
  • 00:18:33
    this with weapon actually cuz we have
  • 00:18:35
    two sub classes for it add item yeah I'm
  • 00:18:38
    not going to make you watch me type all
  • 00:18:40
    that but also the word wrap is still
  • 00:18:41
    annoying whatever anyway what you may
  • 00:18:43
    notice is that this add item is being
  • 00:18:45
    used and these ad items are grayed out
  • 00:18:48
    they are not being used because when we
  • 00:18:50
    are using it here we're only passing in
  • 00:18:52
    one parameter however if we change it to
  • 00:18:56
    fit the format as this ad item and this
  • 00:19:01
    ad item then it'll know which ad item we
  • 00:19:03
    are talking about and it'll use that
  • 00:19:06
    specific method so over here let's do it
  • 00:19:08
    instead of this fruit right here let me
  • 00:19:10
    just comment this out instead of that
  • 00:19:12
    fruit instead of that weapon what I'm
  • 00:19:14
    going to do is actually I'm just going
  • 00:19:15
    to copy and paste because that makes my
  • 00:19:17
    life easy copy copy there we go and what
  • 00:19:20
    should occur is that we have the same
  • 00:19:23
    exact output as we had before boom
  • 00:19:26
    because we are adding the same exact
  • 00:19:28
    thing as before just in a different way
  • 00:19:31
    in a in my opinion worse way we are
  • 00:19:34
    doing new fruit here whereas before we
  • 00:19:36
    did it right here and then we are giving
  • 00:19:39
    the values right here in the add item
  • 00:19:41
    which are then being passed in as
  • 00:19:43
    parameters right here and back into here
  • 00:19:47
    in order to add this um object fruit
  • 00:19:52
    into items into
  • 00:19:55
    inventory huh that's interesting it
  • 00:19:57
    doesn't show is blue even though it's
  • 00:19:59
    obviously being used that's weird anyway
  • 00:20:02
    I could also do the same thing with
  • 00:20:03
    display inventory like this right here
  • 00:20:05
    what I'm doing is I'm only displaying
  • 00:20:07
    items by type so if I come over here and
  • 00:20:11
    I do exactly like this and I pass in the
  • 00:20:13
    parameter a specific type which is Fuji
  • 00:20:16
    in this instance is it'll display that
  • 00:20:19
    specific fruit with that specific type
  • 00:20:21
    and I mean this is pretty obvious but if
  • 00:20:23
    I type in melee which is also a type
  • 00:20:25
    it'll display that as well and that is
  • 00:20:29
    method overloading which again allows
  • 00:20:31
    multiple methods in the same class to
  • 00:20:33
    have the same name but with different
  • 00:20:35
    parameters and this is how you achieve
  • 00:20:38
    compiled time polymorphism or in other
  • 00:20:41
    words static polymorphism method
  • 00:20:43
    overloading as well as operator
  • 00:20:45
    overloading and why would we want to do
  • 00:20:46
    something like this well one uh it makes
  • 00:20:49
    it easier to read I mean at least for me
  • 00:20:50
    it does compile time polymorphism with
  • 00:20:53
    overloading allows for well errors to be
  • 00:20:56
    caught in compile time as opposed to
  • 00:20:58
    runtime because this doesn't run in
  • 00:21:00
    runtime if we want to use this one or
  • 00:21:02
    this one or this one all of that is done
  • 00:21:04
    and decided in compiled time no
  • 00:21:06
    additional checks during runtime so it
  • 00:21:09
    also increases performance whereas back
  • 00:21:11
    to overriding overriding does run at
  • 00:21:13
    runtime and it it performs runtime
  • 00:21:16
    polymorphism and that is when you want
  • 00:21:18
    something to be more Dynamic more
  • 00:21:20
    flexible and it also allows you to reuse
  • 00:21:23
    these methods in whichever way you see
  • 00:21:25
    fit
  • 00:21:26
    polymorphism oh yeah just to click CL
  • 00:21:28
    ify these overridden uh methods as well
  • 00:21:32
    as the overloading of methods while our
  • 00:21:36
    both ways to achieve polymorphism they
  • 00:21:39
    are also a part of encapsulation because
  • 00:21:41
    encapsulation is all about bundling the
  • 00:21:44
    data being the
  • 00:21:46
    attributes as well as the methods with
  • 00:21:49
    two string being a method into a single
  • 00:21:52
    unit that single unit being item all
  • 00:21:55
    right so I did
  • 00:21:57
    encapsulation I did inheritance I did
  • 00:22:01
    polymorphism oh crap I didn't do uh
  • 00:22:04
    abstraction so abstraction on like like
  • 00:22:07
    a broad stroke the broad definition of
  • 00:22:09
    abstraction is the concept of hiding
  • 00:22:11
    complex implementation details and
  • 00:22:13
    showing only the essential features of
  • 00:22:16
    an object which as a programmer when you
  • 00:22:18
    implement it it it helps to reduce
  • 00:22:20
    complexity and and allows you to focus
  • 00:22:22
    on interactions at a higher level and
  • 00:22:24
    there are a few ways to implement it you
  • 00:22:26
    can use abstract for a class you can use
  • 00:22:30
    abstract for a method and we'll get to
  • 00:22:31
    that in a second let's start with
  • 00:22:32
    abstract for a class and let's do class
  • 00:22:35
    item and what this does it it means that
  • 00:22:37
    we can no longer use class item to
  • 00:22:40
    create an object like why why would we
  • 00:22:42
    want to do that well do we really want
  • 00:22:45
    generic item over here or do we only
  • 00:22:48
    want to be able to create the specific
  • 00:22:50
    subclasses of item we want to do the
  • 00:22:53
    ladder so we're going to make item
  • 00:22:55
    abstract which is why this is yelling at
  • 00:22:57
    us by the way because it's it's saying
  • 00:22:59
    hey that's abstract you you can't you
  • 00:23:01
    can't do that so I'm going to comment
  • 00:23:03
    that out okay and that also means that
  • 00:23:04
    we actually don't need this override
  • 00:23:07
    method at all we need it in our weapon
  • 00:23:09
    and in our fruit but we don't need the
  • 00:23:12
    two- string method here because there
  • 00:23:14
    are going to be no specific item objects
  • 00:23:17
    only the subclasses of item objects and
  • 00:23:20
    we can create an abstract method right
  • 00:23:22
    here actually let me do public abstract
  • 00:23:26
    void display
  • 00:23:28
    info boom wait a second this is a method
  • 00:23:32
    aren't there supposed to be parentheses
  • 00:23:33
    and then you return something or at
  • 00:23:35
    least do something like over in here
  • 00:23:37
    you're adding item to item well that's
  • 00:23:39
    the point of an abstract method is that
  • 00:23:43
    you're not doing anything right here
  • 00:23:46
    other than making the subclasses
  • 00:23:49
    Implement display info which means we
  • 00:23:53
    would overwrite it void
  • 00:23:56
    display info like that but of course
  • 00:23:59
    we'd have to put this into a system out
  • 00:24:01
    printland just because that's proper
  • 00:24:03
    formatting and that's why it's no longer
  • 00:24:04
    yelling at us and why fruit is yelling
  • 00:24:06
    at us because we aren't using display
  • 00:24:09
    info in Fruit even though it's required
  • 00:24:12
    based on this abstract class item and
  • 00:24:14
    instead of void and changing all of this
  • 00:24:16
    to system out print L we could have kept
  • 00:24:19
    it string make this string and when I
  • 00:24:21
    say kept it string I mean like two
  • 00:24:23
    string was string and then we would
  • 00:24:24
    return like this and we could have done
  • 00:24:26
    it just like this it just depend depends
  • 00:24:28
    on your use case this is better when you
  • 00:24:31
    want to give the caller the flexibility
  • 00:24:33
    to decide how to use this information
  • 00:24:35
    you want to console log it you want to
  • 00:24:37
    add it to the UI you want to system out
  • 00:24:39
    print Lin you can do whatever you want
  • 00:24:41
    however if you want to be more strict
  • 00:24:43
    that's when you would do something like
  • 00:24:45
    void because now we are forcing it to do
  • 00:24:48
    system out print Lin which encapsulates
  • 00:24:51
    the display Behavior within the class
  • 00:24:53
    and then of course over in inventory
  • 00:24:55
    instead of displaying item to string you
  • 00:24:58
    would want to do actually since we're
  • 00:25:00
    already doing system out print L within
  • 00:25:02
    display info all you do is item. display
  • 00:25:05
    info and well that's how you display
  • 00:25:07
    inventory now however there's also
  • 00:25:09
    another part of abstraction and that is
  • 00:25:12
    an interface so with an abstract class
  • 00:25:15
    like this you're able to have your
  • 00:25:17
    private uh attributes right here you're
  • 00:25:19
    able to have a Constructor you're able
  • 00:25:21
    to have concrete methods so just public
  • 00:25:24
    regular methods you're also able to have
  • 00:25:26
    abstract methods however an interface is
  • 00:25:30
    a little bit different if we want to
  • 00:25:31
    come over here and do new uh I guess
  • 00:25:34
    Java class interface there we go and
  • 00:25:36
    we'll call this item stuff maybe we
  • 00:25:38
    would change the item class abstract
  • 00:25:41
    class to an interface but for this
  • 00:25:43
    example we're just going to call this
  • 00:25:44
    item stuff and create a new Java file
  • 00:25:47
    and within the interface you're going to
  • 00:25:48
    have common methods that all items must
  • 00:25:52
    Implement so get name get quantity and
  • 00:25:55
    display info as you notice these are the
  • 00:25:58
    same methods that we actually have over
  • 00:26:01
    here get name get quantity display info
  • 00:26:03
    and you also May notice that we do not
  • 00:26:05
    have the abstract keyword right here
  • 00:26:08
    that is because everything in an
  • 00:26:10
    interface is assumed to be abstract we
  • 00:26:12
    don't have a Constructor like here we
  • 00:26:14
    don't have all of these attributes like
  • 00:26:16
    here all we have are methods that must
  • 00:26:19
    be overridden so over in Fruit if we
  • 00:26:21
    want to do it here instead of extending
  • 00:26:24
    item because that is a class abstract or
  • 00:26:27
    not that's how you do it you would
  • 00:26:28
    actually
  • 00:26:29
    Implement item stuff that is because
  • 00:26:33
    it's not it's not inheriting anything
  • 00:26:36
    what it's doing is it is forcing fruit
  • 00:26:38
    to implement everything that item stuff
  • 00:26:41
    has which are actually what we have over
  • 00:26:44
    here so actually we already have display
  • 00:26:45
    info I'm just going to copy and paste to
  • 00:26:48
    make my life easy and then we have to
  • 00:26:50
    add the attributes in here private int
  • 00:26:53
    quantity private string name I did that
  • 00:26:57
    out of order that that's better and also
  • 00:26:59
    since we are not inheriting it we have
  • 00:27:02
    to do this the oldfashioned way and
  • 00:27:04
    there we are we have everything that we
  • 00:27:06
    need from item stuff the stuff that it's
  • 00:27:08
    making us do but we have to add all of
  • 00:27:11
    those methods and overwrite all of these
  • 00:27:13
    are overriding not that one but all of
  • 00:27:15
    these are overriding there we go that
  • 00:27:17
    makes more sense they're
  • 00:27:18
    overriding this right here and we're
  • 00:27:21
    also declaring all of our private
  • 00:27:23
    attributes in here and then if we do
  • 00:27:26
    this to weapon we would have to do the
  • 00:27:27
    same exact thing for every single one
  • 00:27:30
    that implements item stuff as opposed to
  • 00:27:32
    having the common attributes in here and
  • 00:27:34
    the common methods in here as well so
  • 00:27:37
    why in the world would we want to do an
  • 00:27:40
    interface where we have to rewrite all
  • 00:27:41
    of this code every single time as
  • 00:27:43
    opposed to having a class and just doing
  • 00:27:45
    it once and then if we need anything
  • 00:27:46
    abstract we can just do an abstract
  • 00:27:47
    method in an abstract class well one
  • 00:27:49
    interesting note is that when you
  • 00:27:52
    implement things you can Implement as
  • 00:27:54
    many as you want you would just do
  • 00:27:55
    implements item other stuff pretend that
  • 00:27:59
    that is an interface that we actually
  • 00:28:00
    have and you can do this as many times
  • 00:28:02
    as you want you could also do fruit
  • 00:28:04
    extends item and implements item stuff
  • 00:28:08
    so that's an interesting point but also
  • 00:28:10
    if it doesn't have a clear hierarchical
  • 00:28:13
    that's a difficult word hierarchical
  • 00:28:15
    relationship so like I a weapon and a
  • 00:28:17
    fruit is an item but what if I had
  • 00:28:19
    something else that wasn't an item but I
  • 00:28:21
    also wanted it to have item stuff then I
  • 00:28:23
    could have a weapon implement or let's
  • 00:28:25
    go with fruits and St there a fruit
  • 00:28:27
    Implement item stuff
  • 00:28:28
    as well as a a a horse a horse is not
  • 00:28:31
    going to be something that you keep in
  • 00:28:32
    your inventory I don't think so let's
  • 00:28:34
    pretend it's not but let's pretend you
  • 00:28:36
    also want the horse to have a git name
  • 00:28:38
    git quantity and then display info you
  • 00:28:40
    would Implement item stuff so that it
  • 00:28:42
    forces that horse to have these um
  • 00:28:45
    methods so that uh I hope that helped
  • 00:28:47
    that's that's abstraction so now try to
  • 00:28:49
    go build something yourself because of
  • 00:28:51
    all you're doing is watching videos like
  • 00:28:54
    this and watching the perfect way to do
  • 00:28:57
    it instead of going through trial and
  • 00:28:59
    error yourself and messing things up and
  • 00:29:01
    then trying to figure out how to fix the
  • 00:29:03
    things that you messed up and going like
  • 00:29:05
    this until you finally get to the proper
  • 00:29:09
    way to do it well you're not going to
  • 00:29:11
    learn so this is your call to action
  • 00:29:13
    right after well call to action like the
  • 00:29:15
    video subscribe to the channel turn on
  • 00:29:16
    the notification Bell but here's your
  • 00:29:18
    call to action to start coding maybe you
  • 00:29:21
    were following along coding this if so
  • 00:29:23
    awesome now dive into the code and
  • 00:29:27
    change things manipulate things try new
  • 00:29:30
    things break it fix it break it again
  • 00:29:33
    fix it again that's how you learn hope
  • 00:29:36
    you enjoyed it there's so much more to
  • 00:29:37
    Java that I can also discuss maybe in a
  • 00:29:39
    future video y'all just let me know okay
  • 00:29:42
    y'all have a good one
タグ
  • Object-Oriented Programming
  • Java
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction
  • ArrayList
  • Code Examples
  • Learning Java
  • Software Development