Java Video Tutorial 14

Java Video Tutorial 14In this Java video tutorial I cover Polymorphism, Inheritance, Protected, Final, Instanceof and a bunch more.

These topics are not complicated, but for some reason they are normally presented in complicated ways.

Use the code that follows the video to help you gain a complete understanding of these topics. If anything isn’t 100% clear leave a comment below and I’ll help.

If you like videos like this share it [nttfblike]

Code from the Video

ANIMAL.JAVA

CAT.JAVA

LESSONFOURTEEN.JAVA

27 thoughts on “Java Video Tutorial 14”

  1. Hello Derek,

    your videos are gorgeous!

    What’s the advantage of using the constructor of the super-class in line 42! Why not using the constructor of the child-class?

    🙂 , Pete

    1. Thank you 🙂 You use super(name, favFood) because the fields name and favFood are in the super class. At this point I’m just demonstrating oop concepts. Later on I show you why it is better to use super classes and subclasses. If you want to know why now, look at my design patterns video tutorial. I hope that helps

  2. Derek, thanks for the video, just tell me is android development the last stage after i complete watching all of your videos.

    1. I’m going to cover Android when I finish with Algorithms. I haven’t decided if it will come immediately because I ultimately want to design games for Android. If I think it will be easier to teach desktop game development first and then move to Android games I may do that. I may also decide to do it the other way around.

  3. Animal tabby = new Cat(“Tabby”, “Salmon”, “Ball”);

    CAN U PLZ ELABORATE THIS.does the animal class has all the methods and fields in cat class or reverse is the case.
    here tabby holds the refrence to cat class, but is defined as Animal tabby which means it is of type animal i.e, it has all methods and fields of animal class.PLZ EXPLAIN THIS.

    1. Yes the cat class inherits all the methods in animal and it will use them unless the cat class overwrites those classes. Because it is an animal though you’ll be able to take advantage of polymorphism

  4. Dear Tobby,

    Your videos are very nice. They are helping in my interview prep.
    Can you please explain
    Animal tabby = new Cat(“Tabby”, “Salmon”, “Ball”);
    Yes I read ur explanation above.

    But then in System.out.println(randAnimal.favToy);
    why does it throw an error??

    Thank you 🙂

    1. System.out.println(randAnimal.favToy) throws an error because randAnimal is an Animal object and the field favToy doesn’t exist in the Animal class.

      I hope that helps and I’m very happy you enjoy the videos 🙂

  5. Get video Derek but you didn’t spend much time explaining the protected access modifier. You say that it allows subclasss to access methods and fields but why would you choose protected over public? What else besides subclasses could access your methods or fields if you make it public?

    Thanks.

    1. Sorry about that. To make it simple, protected is like private except for the fact that classes in the same package can access each other as if they were public. Does that make sense?

  6. Hi and thank you for all your hard work. Can you explain why one would want to use Animal tabby = new Cat(); instead of Cat tabby = new Cat();? What is the benefit of mixing it up(polymorphism)?

    1. Yes polymorphism is the reason. It is very beneficial to be able to refer to many subclasses as their super class type. As the tutorial continues you’ll see why

      1. It keeps saying my comments are awaiting moderation, and you didn’t respond to my first one so I’ll try this. My original comment: Ok, so as I watched your videos and started thinking more, I realized I don’t know as much about the difference between public, private, and protected. I looked on the internet but everything I saw compared to what I had seen you do in your code seemed to contradict eachother. Whenever I thought I understood it I just looked back at your code and realized what I thought couldn’t be right. So if you could help to clarify this I’d appreciate it alot.

        1. Sorry about losing your comment. That happens some times for some weird reason?

          Lets say you have a class named Machine with a method named doStuff().

          If doStuff() was public, anyone could create and object of type Machine and call for doStuff to execute.

          If doStuff() was private, only methods inside the class Machine could call for doStuff() to execute.

          If it was protected only classes saved in the same package with Machine could call for doStuff() to execute.

          Does that help?

  7. Just making sure you notice my previous comment, it says its awaiting moderation so I’m not sure what that means.

    1. Every comment is labeled as awaiting moderation until I read it. For security reasons I can’t let people post directly to my site. Sorry about that, but I read and responded to your comment

  8. Ok, i tried adding my email because it said my comment is awaiting moderation, and you skipped over it, so I’m gonna try again. My original comment: Ok, so as I watched your videos and started thinking more, I realized I don’t know as much about the difference between public, private, and protected. I looked on the internet but everything I saw compared to what I had seen you do in your code seemed to contradict eachother. Whenever I thought I understood it I just looked back at your code and realized what I thought couldn’t be right. So if you could help to clarify this I’d appreciate it alot.

  9. thanks for this tutorial derek! i just ran into small doubt. I really didn’t understood the basics of generic constructor like you have used here “public Animal(){ }” why we have to use this ? I searched through some other sites but instead they confused me a lot as they are using some “” signs to declare generics. Can you highlight it what is it and why it is used?

    1. they are using some “” signs = less than and greater than signs. dunno why my signs inside quotes got deleted.

    1. Hi Sam,

      You basically use an abstract class only when you don’t want all methods to be abstract. Over time you’ll find you don’t often need abstract classes.

Leave a Reply

Your email address will not be published. Required fields are marked *