OOP: Abstraction

Learn the Basic Java Concepts Object Oriented Programming
15 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.44
List Price:  €93.49
You save:  €28.05
£56.04
List Price:  £80.06
You save:  £24.02
CA$95.74
List Price:  CA$136.79
You save:  CA$41.04
A$107.11
List Price:  A$153.03
You save:  A$45.91
S$95.36
List Price:  S$136.24
You save:  S$40.87
HK$547.90
List Price:  HK$782.75
You save:  HK$234.85
CHF 64.03
List Price:  CHF 91.47
You save:  CHF 27.44
NOK kr772.60
List Price:  NOK kr1,103.76
You save:  NOK kr331.16
DKK kr487.88
List Price:  DKK kr697.01
You save:  DKK kr209.12
NZ$117.84
List Price:  NZ$168.36
You save:  NZ$50.51
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,700.43
List Price:  ৳11,001.09
You save:  ৳3,300.65
₹5,837.33
List Price:  ₹8,339.41
You save:  ₹2,502.07
RM333.81
List Price:  RM476.90
You save:  RM143.08
₦92,489.99
List Price:  ₦132,134.22
You save:  ₦39,644.23
₨19,474.37
List Price:  ₨27,821.72
You save:  ₨8,347.35
฿2,588.61
List Price:  ฿3,698.17
You save:  ฿1,109.56
₺2,275.23
List Price:  ₺3,250.47
You save:  ₺975.23
B$358.08
List Price:  B$511.57
You save:  B$153.48
R1,312.98
List Price:  R1,875.77
You save:  R562.78
Лв127.91
List Price:  Лв182.73
You save:  Лв54.82
₩96,452.39
List Price:  ₩137,795.03
You save:  ₩41,342.64
₪267.73
List Price:  ₪382.49
You save:  ₪114.75
₱4,034.64
List Price:  ₱5,764.02
You save:  ₱1,729.38
¥11,065.06
List Price:  ¥15,807.91
You save:  ¥4,742.85
MX$1,201.03
List Price:  MX$1,715.83
You save:  MX$514.80
QR255.05
List Price:  QR364.38
You save:  QR109.32
P965.79
List Price:  P1,379.76
You save:  P413.97
KSh9,296.24
List Price:  KSh13,280.92
You save:  KSh3,984.67
E£3,349.82
List Price:  E£4,785.67
You save:  E£1,435.84
ብር3,985.10
List Price:  ብር5,693.24
You save:  ብር1,708.14
Kz58,375.85
List Price:  Kz83,397.65
You save:  Kz25,021.80
CLP$66,375.71
List Price:  CLP$94,826.51
You save:  CLP$28,450.80
CN¥507.11
List Price:  CN¥724.48
You save:  CN¥217.36
RD$4,113.13
List Price:  RD$5,876.15
You save:  RD$1,763.02
DA9,392.53
List Price:  DA13,418.47
You save:  DA4,025.94
FJ$158.35
List Price:  FJ$226.22
You save:  FJ$67.87
Q544.06
List Price:  Q777.27
You save:  Q233.20
GY$14,678.83
List Price:  GY$20,970.66
You save:  GY$6,291.82
ISK kr9,837.79
List Price:  ISK kr14,054.59
You save:  ISK kr4,216.80
DH708.72
List Price:  DH1,012.51
You save:  DH303.78
L1,246.75
List Price:  L1,781.16
You save:  L534.40
ден4,025.16
List Price:  ден5,750.48
You save:  ден1,725.31
MOP$565.76
List Price:  MOP$808.27
You save:  MOP$242.50
N$1,340.22
List Price:  N$1,914.68
You save:  N$574.46
C$2,582.07
List Price:  C$3,688.83
You save:  C$1,106.76
रु9,353.99
List Price:  रु13,363.42
You save:  रु4,009.42
S/263.57
List Price:  S/376.55
You save:  S/112.97
K270.82
List Price:  K386.91
You save:  K116.08
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,859.31
List Price:  ZK2,656.27
You save:  ZK796.96
L325.94
List Price:  L465.65
You save:  L139.71
Kč1,644.39
List Price:  Kč2,349.23
You save:  Kč704.84
Ft25,720.62
List Price:  Ft36,745.32
You save:  Ft11,024.70
SEK kr762.14
List Price:  SEK kr1,088.81
You save:  SEK kr326.67
ARS$61,174.36
List Price:  ARS$87,395.69
You save:  ARS$26,221.33
Bs486.59
List Price:  Bs695.16
You save:  Bs208.56
COP$277,528.94
List Price:  COP$396,486.91
You save:  COP$118,957.97
₡35,172.98
List Price:  ₡50,249.28
You save:  ₡15,076.29
L1,732.56
List Price:  L2,475.19
You save:  L742.63
₲520,857.74
List Price:  ₲744,114.38
You save:  ₲223,256.64
$U2,703.34
List Price:  $U3,862.08
You save:  $U1,158.74
zł282.38
List Price:  zł403.41
You save:  zł121.03
Already have an account? Log In

Transcript

Hello there, and welcome back to this Java development course. So last time we went over a calculation and got some work to do, your job was to encapsulate our insect class and encapsulate our spider class as well. So here is essentially what should have done. So very simple. Just make the number of legs attribute private. And then I'd get her own setter method for them.

And say what spider just float number of eyes is private and getter and setter method. There we go. Very simple, or actually, yes, this would be int. Since I actually changed a little bit last time, yeah, there we go. So just int or float, you know, however you want to do it. Alright.

So they're essentially just in calculations. Alright, so today, we're going to be going over abstraction, as I've already said, and so essentially, the whole idea of an object is pretty simple. It's one of the more complex object oriented concepts there are, but once I get the hang of it, it becomes very rudimentary. So let's get into the lesson. Alright, so to understand abstraction, we're gonna need to do a couple things. First of all, it's going to create a new class.

So a new class will be called da L. So Dao is actually stands for database access objects. So we're going to be creating sort of like a database access object. If you don't know what a database access object is, don't worry about it. If you don't know what a database is, don't worry about it. It's just a place where we hold data and we're not actually going to be creating our database access object, it's not going to be connecting to any databases or anything. It's just for demonstration purposes.

Alright, so the da da, l class will be public. And to make a class abstract, we simply we simply add the abstract keyword. Okay, and abstract There we go. Alright, so the abstract keyword essentially just means that we will have abstract methods inside our class. And let's, let's delete it for now. So we're going to do this without the abstract, keyword and mouse Korean abstract methods.

So essentially, we're going to be do doing is have a method For getting a item from our database, and we're going to have a method for putting it out of inventory database, there's going to be two methods. And depending on the type of the item, we're going to have different sort of implementations of those methods. So if you wanted to put for example, a string into the database, then we would do, you know, put into database, da IO dot put into database and then a string with things we want to put in. If we wanted to put it into an integer database, then we would have another database of type integer and put it into there accordingly. So that's we're going to do so we're going to make an abstract, abstract, void, and we're gonna make it public. So public abstract void, put into database.

All right, there we go. And then we're going to take in a string as the object that we're actually going to put String put all right. And then we're just going to do System dot out dot print ln we are putting and then plus put plus into our database. All right, there we go. All right, now we have we have an error. So right here, when public abstract void we need to hover over.

So abstract methods do not specify a body. So what does this mean? Well, this means that an abstract method does not actually have a body implementation. So this right here, in between these quotes, is actually not allowed. So let's remove that. Now the abstract method is correct.

And if you hover over it, we actually have a different error. So we need to make type dow abstract, we just put that abstract keyword back into this class. All right. And then now, I'm gonna change this to object as well. Yeah. All right.

And now what we're going to do is we're going to do public abstract, void, remove. Actually, no, let's just say get from database. Alright, and then it's just gonna be an int, database, index. Okay. And there we go. So these are two abstract methods.

Now, we're going to need two separate classes. So we're going to have an integer database and a string database. Let's just say like that for now. What we're gonna do is we're just gonna go new class. And let's just click call the class int, db. So into database.

All right, there we go and our database we're going to extend extend data There we go. All right. Now if we have a class, a regular class that extends an abstract class, then we need to override or implement the methods in that class. So you'll see here now we have an error. So if you hover over it, the type in dB must implement the inherited abstract method, do dot put into database. So here we actually have an add unimplemented methods for Hindi.

But here, so if we just click it, it'll automatically add the unimplemented methods. So these are just the methods in here. So put into the database and get from database and database and get from database. There we go. Now this override annotation here it is sort of might be new, but essentially what we use whenever we have want to override a method, so overriding a method essentially means that we will not use that method and instead use this method. Alright, and so now inside here, we can actually do system, dot out dot print ln.

We are putting, putting, plus put plus into plus into our database. Now here we can actually put here, we can actually put a body method body. Alright, and then we're going to do the same exact thing here. What appears to be the problem. Hmm, well, that's odd. It's good to see.

There we go. That was a weird, weird error. All right, so here we you see what actually put a party a body. So in our abstract void input into database, we can't add a body so we get an error then, as you can see, but if we have a regular class, then we can actually add a body in a regular method as well. All right, and so now we're going to do in Get from dB. We're going to System dot out dot print ln we are getting getting item item number plus DB index from our database.

All right, and there we go. All right, now let's go ahead and try to do this. So what we did was he did, okay, index from a lowercase letter here is also from a lowercase There we go. All right. Um so now we have a database access object, abstract class. And then I have an integer database class that implement that extends database access object, abstract class.

And so in result, we have to override the methods inside database access object. class. Now, if we go ahead and go into classes and objects, right, and if we try to do da o, t equals new DL, right, we'll actually get an error, as you can see cannot instantiate the type D eo. So it can't actually instantiate the DAO class, since it is abstract. So abstract classes cannot be instantiated with mate, which makes sense, what would these methods even be? So what they put into database method do?

We don't know. And so that's why we can't actually instantiate it. But if we have a class that extends the abstract class, then we can't that we can, then we can instantiate it. So if we do in dB, t equals new int, db. There we go. Now that works, no problem.

And so now we can do t dot put it into database and get from database as well. All right, but before we do that, let's go ahead and create Another class so the new class is going to be called new class, it's going to be string dB. There we go. And it's also going to extend, extend, yo. And we're going to add the unimplemented methods as always. And now here, we're going to have put into database we're going to have System dot out dot print ln putting string and then plus, put plus into whoops in quotation marks into database.

There we go. And now we can do get from database we're gonna do the same thing as integer database. Alright, except here, let's go ahead and do this. And then let's also inside integer database. What we're going to do is we're going to do if Put instance of integer. So you remember what instance of de so instance of just checks whether or not a class, an object is an instance of a class.

In this case, integer is actually a class that's built into Java, which is just the complex class for integers for so for intz. So we can't actually do instance of int. Let's give us an error. Both can do integer. All right, all right. There we go.

And now we're gonna do the same exact thing here. So we're going to do if put instance of string, only then are we going to execute this. So same thing here. Alright, and there we go. So now we have these two classes, so put into the database and put into the database string and for integer databases, and we will print them only if we put in the correct values. So now let's go and actually execute this, let's try to run this.

So we're gonna have to insert a database. So we're going to do t dot put into database. And then we're going to have, we know we're going to put an integer into the database of new integer. And remember, integer is just a built in class file. So just an integer soup, it's pretty much the same thing as doing int except this an object as well. All right, now, if you go ahead and actually run this, you'll see we are putting five into our database.

And there we go. So it works. But if we try to put something else in like let's say a string, then it does nothing, since it won't execute if it's not an instance of integer. So there we go. So that's essentially abstraction. So abstraction is sort of a very important part.

And I don't actually expect you to get it at this point. So there's a lot a lot more to go over in abstraction. There's actually, I'm sure I could actually create a whole separate course for abstraction in Java and how that really works. In fact, there's a lot of frameworks that are built framework essentially just sort of like a, a tool in a language. It's like a. It's like a different programming language altogether.

But it's built on one language. So yeah, anyway, that actually a lot of frameworks that are built on Java, and the whole idea of abstraction. And so yeah, so abstraction is really its own separate topic. But this is really the very basics. And then this is really what you need to know, in order to sort of be able to just on your own, try to figure out the more advanced stuff. So a couple of details as well with abstraction, we can only extend one class.

So we cannot do extends Dao and then extends Dao and then you know, extend something else. Let's say in dB. So we can't do that. We can't do this either, comma int, db, either. So we can only extend one class and I'm sure Wondering what if you want to extend multiple classes? What do we do?

Well, you can't you can't extend multiple classes, there actually is such a thing as a interface, which is sort of a different topic altogether. It's actually kind of like an abstract class, except it isn't a class interface. And it can't have regular sort of methods, you can only have abstract methods. And you can actually extend multiple interfaces, although it would be implementing, you know, extending so you'll see here how I'm already probably confusing you with what I'm saying. But this is what I said about how it does it is more advanced abstraction is a whole separate sort of topic on its own. Alright, and then another detail is that we have abstract class like the DAO class, which is the problem here, like the DAO class, we can have regular methods as well so we can do public void.

I'll say no int was to void void print one. So in this method, we can do system dot out dot print ln print one. Actually, let's just do one without the print, there we go. So we can have regular methods as well. And inside our string DB now, here, if we actually do t dot t dot print one, it'll also work. There we go.

That was the prints one. Since in our demo class, we have a regular method print one, and we don't need to override that method. All Alright, so there we go. That essentially are all we're going to go over abstraction for this lesson. There are a lot more topics that we can go over and we can dive into it a lot deeper. But again, there's only so much time that we have in this course and feel free to do so on your own.

It really isn't all that that difficult if you really just spend the time understanding the concepts behind it. Alright, so Without further ado, then let's say some homework. Alright, so for homework, first of all your job is to replicate what I did here, create a demo class. And also create another abstract class, which will be an abstract animal class, which will have two methods. So it's going to have get weight. And I don't really say get animal species name, so get species name.

And then your job is to go and create two animals. So like a zebra and a gazelle, for example, which would implement those that abstract class accordingly. So for get species name is gonna return a zebra for zebra and then a gazelle for the cause. So pretty simple, very simple stuff. I'm going to do it and it shouldn't take you that long if you're just getting traction, so yeah, without further ado, then I wish you luck and I'll see you next time.

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.