Java Video Tutorial 15

preview_player
Показать описание


In this part of the Java Video Tutorial I cover interfaces, abstract classes, abstract methods and more.

You need interfaces and abstract classes because Java doesn't allow you to inherit from more than one other class. You would use an interface when you want to force the user of the interface to create every method in the interface.

You use an abstract method when you need some flexibility.
Рекомендации по теме
Комментарии
Автор

An interface is a way of grouping classes. The reason for this is that any class that implements that interface must "do" all the methods that are written in that interface. So if there is an interface called "animal" which has methods: eat, sleep, walk, run, jump, then we have a bunch of classes for different types of animals, we would "group" all the different animal classes together by making them all implement the interface which means they must all "do" the methods in the interface, because those methods are common to them all. If that sounds exactly like inheritance with an abstract parent class that's because it is. The reason that interfaces are sometimes used instead of regular abstract class inheritance is because in Java a class can only have one parent class but can have man interfaces. If a class could have multiple parent classes then there would be little point in interfaces. The interface also "guarantees" how to communicate with the class, so that there is set standard that all the classes must follow to make everything straightforward and streamlined.

erikmueller
Автор

I lack the requisite vocabulary of how grateful I am that I ran across your videos. Not only did they help keep me informed throughout my semester, but they also helped keep me declared as a CS major which was a big deal for me. Thanks for making it incredibly simple to comprehend this seemingly difficult subject!!

SN: The vegan recipe videos are awesome as well!!

mymysf
Автор

They are useful when you want to force those that implement it to implement certain methods like an interface. The difference between them and an interface is that you can define specific code in some methods which you can't do in an interface. They aren't as commonly used as regular classes and interfaces like you said.

derekbanas
Автор

The Vehicle class is used as a blueprint to create vehicles. Those vehicles that are created are vehicle objects. To reference a vehicles variable or field you use this. this is shorthand for the objects variables, fields, or methods. Does that make sense?

derekbanas
Автор

Thank you :) I wouldn't say it came naturally. I learned how to program in the early 80s when most people didn't know what a computer was. I learned pretty much 100% from experimenting. I still pretty much learn that way today. I have been trying to figure out how to make tutorials that feel more like fun experiments for a while now. I think I may finally be getting it with the Android tutorials, but they are very hard to make. Thanks for checking out my videos :)

derekbanas
Автор

You're very welcome. Stick with it and you'll get it. Most programmers learn at about 4 stages. Everything will just make sense one day

derekbanas
Автор

Yes you could do that. Try it out. If you want to see crazy stuff look up my java reflection tutorial

derekbanas
Автор

I can't tell you how helpful your channel has been to me to supplement my online college class where I have just felt lost reading the textbook. Thank you so much! Straightforward and approachable - couldn't ask for more!

kendallchristlieb
Автор

Thank you :) Yes you can drop the this if the variable names are different. I was using this to focus on the class fields in the beginning of this tutorial series

derekbanas
Автор

We force the user to use getter and setter methods so that they on't have direct access to the variables. Lets say you have a variable named weight. If someone tried to change the value to a negative number that wouldn't make sense. Our setter method could stop the user from making that change. If we provided them direct access they could give it any value at all. This could potentially cause errors in the program. Does that make sense?

derekbanas
Автор

These videos are so good. Timeless content. Thank you for putting this together 11 years ago!

maywakeTV
Автор

Thank you :) I guess what I should have said is that I don't get many views in comparison to the other tutorial guys with a bunch of videos.

I think I have the best community of people watching my videos. I feel like I know many of you guys to a certain extent. I also think that 99% of you are very kind and intelligent :)

derekbanas
Автор

Stage 1 : Learn how to write procedural programs and understand the core language
Stage 2 : Start using classes, but still write procedural programs
Stage 3 : Start writing true OO code, but still stick to procedural ideas
Stage 4 : Learn to write programs using object oriented design principles and design patterns

derekbanas
Автор

Thank you for taking the time to write such a nice message :) You're very welcome. I revisit these topics in the first 2 videos in my design patterns tutorial. They may help explain anything I didn't cover well here.

derekbanas
Автор

You use interfaces when you want to force the user to implement ever method in the class. You use an abstract when you only want the user to implement some of the methods. Abstract classes just provide more freedom. Does that make sense?

derekbanas
Автор

Over 11 years later and this is still a helpful video!

Dom-qocf
Автор

Yes I love making fun little programs as well. I'll be making many more Android tutorials. I'm just taking a short look at C for fun right now.

derekbanas
Автор

You could watch the first 2 parts of my Design Pattern tutorial. I cover everything better there I think. It is a complex topic that I come back to again and again through out this tutorial. Basically we use things like abstract classes and interfaces so that objects aren't so dependent on each other. We try to avoid situations in which if one class changes it then breaks a bunch of other classes.

derekbanas
Автор

Great! I'm glad they are helping. Always feel free to ask questions

derekbanas
Автор

You're very welcome :) Check out the first 2 videos in my Object Oriented Design tutorial. It takes a while to completely understand how to convert ideas into code

derekbanas