Java encapsulation 💊

Поделиться
HTML-код
  • Опубликовано: 5 окт 2020
  • Java encapsulation getter and setter methods
    #java #encapsulation #getters #setters
  • НаукаНаука

Комментарии • 181

  • @garrickreynolds2015
    @garrickreynolds2015 2 года назад +61

    I’ve been watching your videos to help in my programming 3 course and I feel like you simplify everything so well, I’ve really had a breakthrough in my understanding of certain concepts after watching your explanations. Thank you for your work!!

  • @BroCodez
    @BroCodez  3 года назад +72

    //*************************************************
    public class Main {
    public static void main(String[] args) {

    // Encapsulation = attributes of a class will be hidden or private,
    // Can be accessed only through methods (getters & setters)
    // You should make attributes private if you don't have a reason to make them public/protected

    Car car = new Car("Chevrolet","Camaro",2021);

    car.setYear(2022);

    System.out.println(car.getMake());
    System.out.println(car.getModel());
    System.out.println(car.getYear());

    }
    }
    //*************************************************
    public class Car {

    private String make;
    private String model;
    private int year;

    Car(String make,String model,int year){
    this.setMake(make);
    this.setModel(model);
    this.setYear(year);
    }

    public String getMake() {
    return this.make;
    }

    public String getModel() {
    return this.model;
    }

    public int getYear() {
    return this.year;
    }

    public void setMake(String make) {
    this.make = make;
    }

    public void setModel(String model) {
    this.model = model;
    }

    public void setYear(int year) {
    this.year = year;
    }

    }
    //*************************************************

    • @farhangotham1898
      @farhangotham1898 2 года назад +3

      U are a legend

    • @michaeljagdharry
      @michaeljagdharry 2 года назад +1

      Don't forget to pin this comment!! Thank you so much for all your help! Your videos are amazing and crystal clear!

    • @shadmankhan1969
      @shadmankhan1969 Год назад +2

      just letting you know you forgot to bin this comment boss. thanks for everything

  • @frankjuuh
    @frankjuuh 2 года назад +7

    I've never felt more driven to like and comment on every video by any creator. Love your videos.

  • @RMJJ91
    @RMJJ91 Год назад +2

    You are a legend. You make learning to code so much easier!!

  • @magl1003
    @magl1003 3 года назад +6

    Your videos are amazing! Thank you so much for teaching me Java. I was struggling hard with everything and OOP was not making any sense to me. You deserve so many more views! Keep up the good work!

  • @jenniT613
    @jenniT613 2 года назад +1

    Your explanation of setters and getters was amazing! Thank you so much

  • @javlontursunov6527
    @javlontursunov6527 2 года назад +2

    Man I really appreciate what you are doing. Keep going.

  • @itdasturlashdunyosi5631
    @itdasturlashdunyosi5631 3 года назад +2

    I loved the way you explain! Thanks a lot

  • @surfacepro8208
    @surfacepro8208 Год назад

    By far, the best You-Tube tutorial channel!

  • @ladalll1303
    @ladalll1303 2 года назад +2

    Great explanation! It is super well structured and clear. I have to watch your videos while taking other Java course because you explain it so well! Thank you again!

  • @EMont-qe8qf
    @EMont-qe8qf 3 года назад +2

    Thanks, Bro. You're the best!
    Shoutouts from Brasil.

  • @oumelkheirkhayrieaa5424
    @oumelkheirkhayrieaa5424 2 года назад

    Thank you so much for this! We really appreciate it bro!

  • @mabelle105
    @mabelle105 2 года назад

    Thank you so much! It took me actually quite long to understand this, but finally I understood this method!

  • @unalysuf
    @unalysuf Год назад

    I've just started to learn java and j discovered your channel and you are pretty good. So I wanted to support your channel with my comments. Thank you so much again for your amazing effort.

  • @jagsingh1571
    @jagsingh1571 2 года назад

    Bro you are the fookinggggg best, you just helped me out for my assignment and seriously the best explaining everrrrr!!!!! thank you so much!!!❤

  • @cuca3243
    @cuca3243 Год назад

    Great explanation, unlike other videos I have seen. I am understanding now the meaning of encapsulation. Thank you.

  • @user-hj9zm8xe1d
    @user-hj9zm8xe1d Год назад

    THis really helps me a lot! I am such lucky to find this video.

  • @Joseph-gh5ib
    @Joseph-gh5ib 2 года назад

    great tutorials. thank you so much!!!

  • @danny.3036
    @danny.3036 3 года назад +3

    Thanks, Bro! ☕ You're awesome!

  • @IceBeam93
    @IceBeam93 3 года назад +1

    Thank you for enlightening me with this knowledge :)

  • @mohamadnazirissumalgy3699
    @mohamadnazirissumalgy3699 2 года назад

    Bro,you're the best!

  • @pavelkvasnicka6856
    @pavelkvasnicka6856 Год назад

    This is the best Java tutorial for beginners, so you can learn Java and English in one hit. Please keep going! I vote for Java advance tutorial. Thanks a lot Bro

  • @youvegotmail9385
    @youvegotmail9385 Год назад +7

    I've probably watched a million hours of coding videos already and just stumbled upon this guy today and the way he simplifies and explain things is next level. Even better than WebDev Simplified, Coding with John or dCode.

    • @PitbullSubedi
      @PitbullSubedi Год назад

      humm who are they? im looking to get something from them too hahaha. lil bit of knowlwdge

    • @bananinator2502
      @bananinator2502 11 месяцев назад +6

      1000000 hours = 114,155251 years. I wanted to say that this is not possible, but the oldest known living person is Maria Branyas of Spain, he aged 116 years, 189 days. The oldest known living man is Juan Vicente Pérez of Venezuela, aged 114 years, 105 days, so you must be Juan Vicente Pérez. This is unrealistic because he isnt alive anymore, The oldest living man is Edie Ceccarelli with 155 years and 216 days, your wrote the comment about 11 months ago, so it must be you Edie. Im really happy you still try to learn new things to this age. I do not only have too much time, but i wrote this comment also for the algorhytm to support Bro Code,

  • @imsabbar
    @imsabbar 3 года назад +1

    Well Explanation, Thanks teacher for these Videos 🙏 you really helped me a lot ♥️

  • @JoePelusoMedia
    @JoePelusoMedia Месяц назад

    Thank you for reminding me to relax :) Actually sat back and relax for the first time in a while.. Dx

  • @lzh00
    @lzh00 3 года назад +1

    Nice rhyme at the end, when you say ""hey you, yeah I'm talking to you...

  • @lalainaichane319
    @lalainaichane319 Год назад

    Clear and concise ! Thank you bro 🤜

  • @TheZae31
    @TheZae31 2 года назад

    Bro you just saved me like you won't believe thanks.

  • @santoshojha757
    @santoshojha757 10 месяцев назад

    I got know new technique of placing setter methods inside constructor. Thanks.

  • @marbles5590
    @marbles5590 Год назад

    thank you so much! I was quite confused with setters for a while, I thought they'd be useless if we already have constructors for initializing values but then you can just invoke the setters inside a constructor

  • @samymmohibby3508
    @samymmohibby3508 4 месяца назад

    You are an uncrowned king

  • @nambyrajao1736
    @nambyrajao1736 9 месяцев назад

    Yeah, another super video THAAAAAAANKS !

  • @Ducky_shades
    @Ducky_shades Год назад

    Thanks, it is very inspiring and informative.

  • @EyBossPusi
    @EyBossPusi 4 месяца назад

    Thank you!

  • @theBATfamiliar
    @theBATfamiliar 2 месяца назад

    Grat explanation, I salute you sir

  • @MrCEO-jw1vm
    @MrCEO-jw1vm 4 месяца назад

    Oh boy, I finally understood encapsulation. phew!! Thanks Bro!

  • @santialawiahputrisekar7237
    @santialawiahputrisekar7237 2 года назад +3

    In the constructor,
    What is different between using "this.setmake(make) " and not using "this."?

  • @tapiwakalitawo8013
    @tapiwakalitawo8013 2 года назад

    thanks, this was really helpful

  • @MrRiverman64
    @MrRiverman64 6 месяцев назад

    Thanks, bro! For real!

  • @zkillzixx6692
    @zkillzixx6692 Год назад

    I appreciate your effort Bro 💪💪💪

  • @mohamedbenkhalifa1299
    @mohamedbenkhalifa1299 2 года назад

    Thanks a lot bro!

  • @mddulalmiah8012
    @mddulalmiah8012 12 дней назад

    Legend bro..

  • @user-zq6yx7of5f
    @user-zq6yx7of5f Год назад

    Bro Thank you very much!!

  • @yusufnageh
    @yusufnageh 11 месяцев назад +1

    amazing

  • @azizarfaoui7038
    @azizarfaoui7038 7 месяцев назад

    this video is very helpfull

  • @user-is7yy9fb2c
    @user-is7yy9fb2c 3 года назад +8

    my teacher recommend you, very helpful

    • @BroCodez
      @BroCodez  3 года назад +6

      awesome! I'm thankful for the recommendation

  • @abdirizackmuhamud9203
    @abdirizackmuhamud9203 8 месяцев назад

    Legend.

  • @romanrubanovich9273
    @romanrubanovich9273 3 года назад

    Thanks, Bro!

  • @0ZANGETSUFLASH0
    @0ZANGETSUFLASH0 10 месяцев назад

    ur God at explaining!

  • @blendajeti6167
    @blendajeti6167 2 месяца назад

    King

  • @minhtetpaing1695
    @minhtetpaing1695 3 года назад

    Thank you so much sir.

  • @augischadiegils.5109
    @augischadiegils.5109 3 года назад

    Thanks !!

  • @vpenywise
    @vpenywise 2 года назад +1

    I never thought about replacing the parameters of the constructor with the Setters! :) What are the benefits of doing this?

  • @dolevdo
    @dolevdo 2 года назад

    Amazing

  • @baubaudinamo
    @baubaudinamo 3 года назад

    Good video Bro

  • @camerald8257
    @camerald8257 2 года назад

    As a 2nd year and currently learning Java, I am so happy because I did not understand these concepts during the lecture but you simplified them. What a steal!

    • @lroadzl1288
      @lroadzl1288 2 года назад

      For it being your seconds year, What things about programming do you know so far?

  • @userKanchanKidsZone
    @userKanchanKidsZone Год назад

    Best video

  • @christianvillanueva723
    @christianvillanueva723 3 года назад +16

    Happy teachers day/month bro
    I consider you as my teacher because I learn a lot
    Thanks

    • @BroCodez
      @BroCodez  3 года назад +6

      thank you Christian, I'm happy to help

  • @shaheenreid9939
    @shaheenreid9939 3 года назад +13

    Hi! Your videos are helping me excel in my Java class. Thank you so much! I have a question about setters. If I am using encapsulation but also using inheritance, how do I get around the fact that setting a variable in the parent class to private prevents a subclass from accessing/setting values for that private variable?

    • @shaheenreid9939
      @shaheenreid9939 3 года назад

      I guess I am just trying to understand the need for having constructors and setters to set values in the event I need to use both encapsulation and inheritance.

    • @BroCodez
      @BroCodez  3 года назад +9

      use the super constructor! 🦸
      ruclips.net/video/oKZnHNM9Ew4/видео.html

    • @shaheenreid9939
      @shaheenreid9939 3 года назад +7

      @@BroCodez Ahhh okay got it. Thanks Bro!

  • @vaika8817
    @vaika8817 Год назад

    thanks bro

  • @pa-305
    @pa-305 3 года назад

    best brooo

  • @daltonlagat9018
    @daltonlagat9018 Год назад

    dope

  • @proalexv
    @proalexv Год назад

    Also, great video!

  • @hamzachiguer642
    @hamzachiguer642 Год назад

    Thank you bro

  • @appstocks5956
    @appstocks5956 Год назад

    tysm.

  • @MashoriMentors
    @MashoriMentors 9 месяцев назад

    thank u bro

  • @huuloc8719
    @huuloc8719 2 года назад

    Nice.

  • @djspice6404
    @djspice6404 3 года назад

    !good!

  • @johanjaimes10
    @johanjaimes10 Год назад +1

    I have been thinking what is the point with encapsulation if you are returning the variable value anyway but watching your video I've realised that encapsulation is useful if you want to hide a variable value but you need that variable to calculated something. For example, you want to hide the percentage of revenue but you need it to calculate the final price of a product. You don't want that the percentageRevenue to be public.

  • @kemann3815
    @kemann3815 2 года назад

    Da best

  • @switkaren
    @switkaren Год назад

    seen in a problem set : "use the selector method to display the results".
    What is the selector method?

  • @bomapdich
    @bomapdich Год назад

    After we do the set method. Why do we need to write this.setMake(make) at the top again?

  • @PoorwayTraning
    @PoorwayTraning Год назад

    thanks

  • @dildil6815
    @dildil6815 Год назад

    thank you brooooooooooo

  • @harshnitnaware9143
    @harshnitnaware9143 2 года назад

    Bro Code very good video, easy to understand. Hey bro can you paste down the code in the comment section

  • @mariamcagadas6007
    @mariamcagadas6007 9 месяцев назад

    Thanks bro

  • @arunnteam1901
    @arunnteam1901 Год назад

    thank u

  • @bekzattursynbay5675
    @bekzattursynbay5675 Год назад

    great

  • @mudassirnaz1813
    @mudassirnaz1813 3 года назад

    Thanks

  • @wallstreetbets7741
    @wallstreetbets7741 2 года назад

    thx

  • @Blackilykat
    @Blackilykat 2 года назад

    I don't really understand what the point of using this is.. maybe to set a variable that can't be manually changed? but what about the setter method then?

  • @yigitsoyhanozu
    @yigitsoyhanozu 8 месяцев назад +1

    4:03 "Can we pretty please get" hhashadhashahahha

  • @mohamedelfadli3125
    @mohamedelfadli3125 Год назад

    understood bro.

  • @subid.majumdar
    @subid.majumdar 2 года назад +1

    all we're really doing is making the
    attributes of our class
    private so that they cannot be directly
    accessed from elsewhere within our
    program
    if you need to retrieve one of these
    values you can use a
    getter method if you need to change or
    set one of these values
    you can use a setter method so that you
    can indirectly either
    get or set these values and other
    classes do not have direct access

  • @Simis999
    @Simis999 Год назад

    Bro had a party yesterday :D fistbump

  • @champab5350
    @champab5350 2 года назад

    thanku

  • @cdjosh3568
    @cdjosh3568 3 года назад +3

    Good video Bro but I have a question. Is it really necessary to write "this" in the constructor? It works fine without it, so is it just good practice to write it like that?

    • @BroCodez
      @BroCodez  3 года назад +2

      I thought it might help clarify for teaching purposes

  • @basil9633
    @basil9633 Год назад

    W for helping with comp sci final

  • @rafaelsantana9946
    @rafaelsantana9946 Год назад

    Dropping a comment :)

  • @michaelandreisacaguing3848
    @michaelandreisacaguing3848 Год назад

    moreeee pleassseee

  • @dr.smoothlove
    @dr.smoothlove 2 года назад +1

    Comment for the algorithm, bro.

  • @robertwide6592
    @robertwide6592 2 года назад

    niceuuu

  • @luisgabrielmarinmunoz4814
    @luisgabrielmarinmunoz4814 Год назад

    👍🏻👍🏻

  • @vigneshrp1294
    @vigneshrp1294 6 месяцев назад

    I have a doubt from a long time and couldnt find the answer.......We use access modifiers to control the accessiblity within the program to ensure encapsulation.....so if we have a private variable and if still we can access it through a public getter method then what is the use of having a private variable.....it can be public instead right?

    • @apratimkundu2308
      @apratimkundu2308 4 месяца назад +1

      As far as I understand, It's mostly used to prevent potential misuse and unintended modifications. Suppose the data member year was public. And somewhere in the code we use car.year = -1990 which is not valid. What benefits would be there with setters now would be that we can enforce further validations on setter method so that scenarios such as this doesn't ruin data integrity. We can put an if check inside setter method to throw an exception whenever year is passed negative, thus providing us with an controlled way to modify the internal state of an object.

  • @soumelee5661
    @soumelee5661 Год назад

    🎁 good vid

  • @bluedude9567
    @bluedude9567 11 месяцев назад

    Why do you "return make" and not "return this.make" in the getter method?

  • @MrDaniWh0
    @MrDaniWh0 2 года назад

    💪👍

  • @harjotsingh9894
    @harjotsingh9894 3 года назад

    Hloo bro , i watched the oop playlist but now how can i practice my oop concepts can u plz suggest some practice problems.. thanks

  • @liliacfury
    @liliacfury 3 года назад +2

    How come you didn’t have to refer the class variables make, model, and year as this.make, this.model, and this.year throughout their class when referring to them? I though only if they were static they could do that. You only did so for the constructor, so I’m confused why it only applies there and not anywhere else.

    • @BroCodez
      @BroCodez  3 года назад +6

      you can do either. I forgot to add "this." for the getters. I probably should have kept it consistent
      If it was static it would be the name of the class Car.make for example

    • @liliacfury
      @liliacfury 3 года назад

      @@BroCodez thanks

  • @wilo9340
    @wilo9340 2 года назад

    plz someone tell me what i have to do to write setters and getters of attributes in one click i mean just by simple clicking without writing every single word i need a solution