Don't Use Inheritance Until You Watch This!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
there is a rule in software design called prefer composition over inheritance in this video I'm going to explain this rule with an actual example and show you one Flow by the Java API by violating this principle friends and welcome back to the channel so there is a nice book I suggest reading which is Effective Java by Joshua blos this is really amazing book it contains a lot of characteristics and things about Java right one actual rule it's called favor composition over inheritance all right so what do we mean by that let's pretend we have class A and then I'm having the class B okay let's say I want to extend this class A right in order to reuse some of the things because inheritance is for usability main okay you would do a like following and start doing things right so this one should be like that all right now the real question you should ask yourself whether is really B is an a it's an actual super type it's not like if you need something for a let's say you need a field a function a variable I don't know then you are going to use the extent okay because this is really a hard relationship between b and a so what you can do instead let's say you want to use something from a it's not to use inheritance simply you delete that you create a private field of a internally like that and then you can use it to do all the things you need okay let's take an example there is an actual problem in the stock APA in jav right so so you can create stack right this famous example and let's pretend a stack of for example string all right so this tag come from java of course this is Java now you know with the stack I can put things into the Java or push yes so I can put a For example B and C okay so the latest element is C nice so what I can do right now I can for example keep popping which is can do why false so you can do stack if it is not empty while it is not empty I'm going to Output to the stack like that and do the okay so if you run this example this simple example we should get CB and a you can see that CB and a that's awesome this actually the good behavior of a now one problem with the stock API is the following I can do an insert at specific place which is insert element add for example let's say I'm going to insert for example hello and then I'm going to put it in the first position guess what if I run the same example it will be c b hello and a okay this is really a big flow of the stack API why is that simply because the stack is inheriting a vector okay now why did Java developers in the beginning use this Vector here to extend Vector because Vector allow for many things okay it's allot for adding elements it's AOW I think adding elements is in the vector exactly it's allow for adding elements like L of things are going to reuse from the vector okay because the like actual implementation internal implementation is there why bothering creating the implementation here let's extend the vector and do whatever we want but this actually result in flow because we are exposing the vector stuff internal implementation outside so people can use it in order to do all sort of things okay and this is really problematic how you can Implement that in a better way simply you can create your simple class for example stack two right and instead of extending what you are going to do you are going to create a private Val for example and then let's make it Vector internally okay this is simple Vector I'm going to init it init it here once I'm initialization of course and here let's pretend I'm having simple string Vector string not generics of course yes like the following and then for every function I'm going to create for example let's have function that is push for example and let's accept the string all right we are going to create two main functions right push and also the pop the pop should return a string right now in the push let me see what how they are doing it they are using add elements Right add elements from the vector so we can do the same here in the push we can do vector. add element and string like follow awesome and then we need to see the pop how they are doing the pop okay the pop is remove okay here it is the peak there is the peak function because they are remove moving things okay we need to create the peak also all right let me just copy this one the peak and paste it here because I need it yes convert it please here the size now put everything as string here the size comes from what come from the vector do size pretty much it and element come from Vector do element at that's everything you need same thing let me just copy the pop code in here okay I'm not doing anything special I'm just reusing and show you how you can reuse this thing in order to do it so Vector do size and here for the peak of course that you be like that and vector. remove element at that's basically it now you can expect the same thing if I use my stack to here just delete that exactly here you won't see this thing right because you created and designed your API in a much better way okay so it is much safe without any flows and this is the beauty if there is flaws in the implementation of the super class if you want to remove those flaws and not transfer them to actual subass you can use composition of course so we won't have any problem like that ah okay we missed the is empty uh let's check is empty okay is empty is implemented with the size okay that's pretty much simple are using the size so we can create it like following yeah it's a boan and return Vector Dot side is empty okay we can do that yeah nice nice now we can run the same exact thing this is actual my stack and it will return the exact same output as you can see so this is how you can convert and use our favor composition over inheritance okay even if you need stuff from the super class in some manner don't extend it directly try to favor composition see if you can work with this private field internally that way you result in much much better and robust design and again please read the book of Effective Java by Joshua blush because actually the example I copied it from also this book there is actually another problem I think properties the properties extend from hash table and it shouldn't do that yeah exactly it's extended from hash table you can see it's object object well I think it's object object it's not generic because this one is predating the generics the creation of this class was before generics come to Java because yeah exactly since Java 1.0 this is really old and of course since people are dependent on such class you can just refactor it same thing with this uh stack because you may say well why they didn't just fix this class they can't do that because a lot of people are dependent on this implementation exactly so it will be much harder but actually it's not also duplicated because while you can use the stack and just be aware of the functions that are in the vector okay so that's pretty much it for this video thanks a lot for watching this video the end don't forget to subscribe to my productivity newsletter which is the productive developer each week I share one productive tip to help you grow as productive developer thank you very much and see you in the next videos
Info
Channel: Charfaoui Younes
Views: 592
Rating: undefined out of 5
Keywords: mobile development for beginners, mobile development roadmap, mobile development vs web development, Jetpack Compose, Android development, Permissions, App development, Tutorial., UI design, Android Studio, Kotlin, Mobile development, Best practices, testing, UI testing, unit testing, app quality assurance, Android app development, productivity tips, software engineering, best practices., test, private, mutation
Id: cTBWs1fPtbE
Channel Id: undefined
Length: 7min 57sec (477 seconds)
Published: Tue Apr 23 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.