Abstraction

4 minutes
Share the link to this page
Copied
  Completed
In java, abstraction is achieved by interfaces and abstract classes. We can achieve 100% abstraction using interfaces. Abstract classes and Abstract methods : An abstract class is a class that is declared with abstract keyword. An abstract method is a method that is declared without an implementation.

Transcript

Hello, in this programming video, we are going to look at abstraction in the form of classes and objects. So what you can do is create abstract methods and abstract classes. And what this simply means is, if you have an abstract class, it cannot be instantiated, which means you can't create an object of it. The only way to actually utilize it is for another class to inherit from it. So this is extremely useful if you're trying to create so like a base class, there are a variety of your other classes. So for example, if you have let's say you're making a game I Need for Speed.

You have a vehicle class so you don't really have car I mean, do you have caught in the speed on me or biting speed? Have there ever been any speed we both have our thing so correct me if I'm wrong, maybe one of the mobile one potentially have, but you're another person a racing game. We have a vehicle class which has stuff like position the call speed No, those are basic properties now generic and you credit class might be a great class for other markets name call, maybe this needs to be game of going all over the place and you have a class for air helicopters as well a class or submarine like any sort of vehicle, the vehicle you will never create a object on because it's just in it's like a blueprint attempt template. But the individual classes you would make, we've already got this sort of enemy super enemy sort of stuff right here.

And what I'm going to do is this class that carry tonic creates a new file for new class. I'm gonna call it I'm going to, we can put the keyword abstract to vote and select that and click Finish is created character and it will be moments already defined, already defined. And he asked what we'll do we just get rid of the content, or we'll cut the content up from here. This shouldn't be defined anymore. Okay, yeah, that's still runs, which is cool. So this extends from character.

This also extends from character as well, and you can get all of its property. Let me show you something if I go to solar system with Java, and if I were to include the character class. Well, that corrector character like that, and if I was to do see one equals new character Oh, yeah, oh yeah, you won't work because we made the abstract. So see if we can instantiate it. If for example, I'll remove this keyword. And when that sound system Java that disappears.

So if you want a base class that a variety of other classes will inherit from, and you never want that base class. To have an object creator of it, you want to make it abstract. And you can go a step further. You can actually make methods abstract as well. So the reason you will make a method abstract is if you want to force the developer yourself or somebody else to actually implement the method in one of the inherited classes. So this reduce house method in enemy is the main change.

In Super anime. We've overridden it, but we want all of the classes to override it to go to character for example, and before the actual data type, the return type, we put here and This should give us an error is moment, because we can't have a body. So we would get rid of that. Now, if I would go to here, run this, we should get another error. And now this error will be an enemy. It's saying that we haven't actually implemented it as an add on implemented methods.

So we don't necessarily need to put the keyword override, but it's good to have it there. It just tells us what, you know, date and overridden method. So in here, I think you'll just tell minus minus equals the regular enemy, we'll just do that. So if I run it, there we go. It's still working fine. So that's it for abstraction in classes and objects within Java.

If you have any questions, feel free to reach out, and I look forward to seeing you in the next awesome Java programming video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.