Immutable Class - interview insights| java interview questions and answers for experienced/Freshers

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ก.ค. 2024
  • Let’s learn different techniques to protect our custom immutable class in java which is one of the hot core java interview questions. In this video, we will start from absolute scratch, i.e by creating a mutable class and understanding different loopholes of a mutable class.
    Then we will convert our mutable class to immutable and will understand how to create immutable classes in java. While creating custom immutable classes, we have
    take care of various scenarios, which are some top core java interview questions.
    Some top 10 java interview questions for this topic could be :
    What is a mutable class?
    How to create a custom immutable class?
    Why the immutable classes are final?
    Why Immutable classes don’t have any setter?
    How to protect your immutable classes if your class got some mutable objects reference?
    Why the hashmap keys are immutable?
    few of the questions from the above and some more questions we will discuss in the next video.
    Make sure these questions come under java interview questions and answers for experienced and java interview questions and answers for freshers category. So whether you got some experience or you are fresher; you can expect this java immutable vs mutable topic ( as java interview programs) in your java job interview.
    Time stamp:
    introduction : 00:00
    Immutable object definition : 01:25
    What is a mutable object? : 02:53
    Mutable Vs Immutable : 06:16
    Custom Immutable class : 06:58
    Mutable reference inside Immutable class : 15:03
    breaking java immutable classes: 21:27
    Protecting your immutable class (code fix) : 24:50
    hashcode() comparison: 35:15
    Important scenario based interview question : 39:10
    Using copy constructor : 45:00
    Using getInstance() method: 49:09
    Pros of Immutable class : 52:13
    Uses of immutable classes : 56:24
    for more java interview questions / java interview questions and answers for experienced /java interview questions and answers for freshers, consider subscribing to my channel.
    / @seleniumexpress
    #CoreJavaInterviewQuestion #FAQForfreshers #FAQForExperienced
    More core java interview questions :
    Why String is immutable?
    • Why string is immutabl...
    String Hashcode caching internal. Hashcode caching for immutable classes (important)
    • This is why string is ...
    Why hashmap works internally?
    • How HashMap works inte...
    java interview questions and answers for experienced / Freshers [playlist]
    • TOP 6 difference betwe...
    For more courses visit
    www.seleniumexpress.com
    Any time you are getting stuck with issues, Feel free to ask for support.
    You can send mail to seleniumexpress@gmail.com
    you can ping me on my Facebook page
    / seleniumexpress
    Make sure to join my private Facebook page (Ask me here)
    “SeleniumExpress - Support”
    / 187000222361579
    you can ask for support in my website forum
    www.seleniumexpress.com
    Follow me on Insta
    / selenium_express
    Music :
    -----
    credits : -
    (channel intro)
    Adventures by A Himitsu / a-himitsu
    Creative Commons - Attribution 3.0 Unported- CC BY 3.0
    creativecommons.org/licenses/b...
    Music released by Argofox • A Himitsu - Adventures...
    Music provided by Audio Library • Adventures - A Himitsu...

ความคิดเห็น • 91

  • @petitexplorer23
    @petitexplorer23 3 ปีที่แล้ว +32

    Hey Abhilash! The day I watched this video just after receiving your channel update, the very next day I had my interview and guess what the interviewer asked me this particular question and he had given me the exact same example (every bit was similar) and I also gave him the same explanation and guess what I cleared that round. The moment I heard this question I was like thank you , thank you Abhilash for this video, it saved one of my opportunities. You are fab man! Thanks a ton :) Keep teaching, I will keep learning :) Bless you

  • @ayushsaxena3511
    @ayushsaxena3511 2 ปีที่แล้ว +6

    Just randomly watched this video and ended up liking and subscribing the channel.
    It's so good to see that you guys are contributing so much in training and educating the developer's community.

  • @udaypandey5659
    @udaypandey5659 ปีที่แล้ว +1

    Lovely explanation! But still there is a catch which can break this I think : in student constructor you are directly assigning adress ex : this.address = address;
    It's should be something like this.addres = new Address ( adress.city , adress.state, adress.country) .
    Because when you created adress in main method , you still know that refrence . So alway clone before assigning in constructor

  • @mridulupadhyay5058
    @mridulupadhyay5058 3 ปีที่แล้ว +5

    You are doing awesome Job, I have literally cracked interviews of many companies by watching your Spring concepts, thanks so much for your valuabe teaching,also please if possible create video for microservices with spring boot also.

  • @vakkalahari5449
    @vakkalahari5449 ปีที่แล้ว

    Your teaching like feed with spoon for students, The best lecturer in TH-cam, he must get more subscribers !!!!!!

  • @anjanakarthiktangirala2430
    @anjanakarthiktangirala2430 3 ปีที่แล้ว

    Clean... Clear... Crisp.... This is one of the best explanation i ever heard. Kudos to you all.

  • @ratansarkar7444
    @ratansarkar7444 6 หลายเดือนก่อน

    Your video defines how much effort you have puted when you have learnt this concepts

  • @manidixit6713
    @manidixit6713 3 ปีที่แล้ว +2

    I went to your website and saw Spring boot course and it costs 15000 !!!!
    Why man why.. U r the best trainer for me .. Everyone can't offord such high price course. I request u to add your course in udemy with much less cost. ☺🙂

  • @amolmehlawat8197
    @amolmehlawat8197 3 ปีที่แล้ว

    Great to have you back Sir. Eagerly waiting for the follow-up video. 👍🏼

  • @deepa8177
    @deepa8177 5 หลายเดือนก่อน

    again watching this video literally so great informative video..

  • @deepa8177
    @deepa8177 2 ปีที่แล้ว +1

    superb, i wasn't aware of so many other possibilities, Thanku so much. definitely it will be very helpful in the interviews & thanks for sharing the real time approach also as it wouldn't be a new thing in future coj of your informative videos. Really each & every aspect is covered.

  • @wisdomeducation6884
    @wisdomeducation6884 3 ปีที่แล้ว

    Brother im completely impressed. I never understand this topic before like that

  • @MohitKumar-cq2rj
    @MohitKumar-cq2rj 3 ปีที่แล้ว +1

    Sir, I am very helpful and lucky that i am able to learn n understand Java / spring by your uploaded youtube video. I request you to provide spring boot and spring microservice video tutorial in upcoming days asap. please...!

  • @nareshKumar-ej7nk
    @nareshKumar-ej7nk 3 ปีที่แล้ว

    Increasing confidence on java.. Thank you 😍

  • @brijeshpatra1629
    @brijeshpatra1629 ปีที่แล้ว

    Damn man once you start watching his videos there is no go back ♥️

  • @venkatashanmukha348
    @venkatashanmukha348 3 ปีที่แล้ว +1

    Happy to see after longdays

  • @mukulkopulwar
    @mukulkopulwar 5 หลายเดือนก่อน

    Clear and Crisp Explanation Abhilash. Thanks for sharing.

  • @sibinthink7736
    @sibinthink7736 2 ปีที่แล้ว

    Abhilash I got selected just watching your tutorials. THANKS A LOT TON CRORE...subscriber 🙏

  • @MostafaTavassoli
    @MostafaTavassoli 2 ปีที่แล้ว

    Amazing video, Abhilash.

  • @PankajKumar-qk8oh
    @PankajKumar-qk8oh 2 ปีที่แล้ว

    Great explanations....really helpful for interview

  • @shubhamthakare7694
    @shubhamthakare7694 3 ปีที่แล้ว

    One of best video on Immutable class

  • @raceyat
    @raceyat 3 ปีที่แล้ว

    Is been a while. Good to here from you.

  • @youssefmoussa2523
    @youssefmoussa2523 3 ปีที่แล้ว

    Hi Mr Abhilash i miss you soo much. I miss also your videos. You are the best teach in youtube you must know that.

  • @madhuri5371
    @madhuri5371 2 ปีที่แล้ว

    very well explained sir.Thank you for this video.

  • @jeevantesting2501
    @jeevantesting2501 3 ปีที่แล้ว

    Hi Abhi,
    The point you mentioned during explanation of advantages of Immutable objects - "thread safety is guaranteed because data inconsistency is avoided in a multi-threaded environment".
    How can data inconsistency arise for a immutable object when it cannot be modified in the first place? or did I miss something?

  • @pandeyjii
    @pandeyjii 2 ปีที่แล้ว

    Hi Abhilash Thnaks for this video, that's very great each and exery step explained well, can you please make one video on complete Data JPA

  • @ramanandkumar7780
    @ramanandkumar7780 2 ปีที่แล้ว +1

    Nicely explained....I have one double ,what is the use of making the class final as already all the data members are final ,no setter method, get method is not returning the original object.

  • @aycricoder6395
    @aycricoder6395 2 ปีที่แล้ว +1

    Love u bro... u r a motivation an inspiration... the reason i started finding interest in coding

  • @NITISHKUMAR-nx9mk
    @NITISHKUMAR-nx9mk ปีที่แล้ว

    Very Nice Explanation!!

  • @rishibharadwaj68
    @rishibharadwaj68 ปีที่แล้ว

    I have a question, what if the Address object that is being used to pass to the Student class constructor is manipulated. The method, where we are creating the Address object has the reference to it. Now though the student object cannot be used to manipulate Address, but the address reference can still manipulate the Address Object inside the Student Object. I think we should clone the passed Address Object as well and then assign it to the Student address member.

  • @sameer9368
    @sameer9368 2 ปีที่แล้ว +1

    You deserve a beer bro...superb explanation 💪💪

  • @ratunawartejas
    @ratunawartejas 2 ปีที่แล้ว

    This was really helpful content. Thanks Abhilash

  • @PhaniKancharla18
    @PhaniKancharla18 3 ปีที่แล้ว

    Hi Abhilash, it's very clear and nice video. I have doubt can we give clone object of addres obj in the get method of student instead copy constructor. As implementing colnable interface in address class

  • @thangaduraik5364
    @thangaduraik5364 ปีที่แล้ว

    Big thanks man. Way of teaching is great.

  • @pratheeban3650
    @pratheeban3650 3 ปีที่แล้ว

    Simply awesome

  • @suseesenthil417
    @suseesenthil417 8 หลายเดือนก่อน

    Could you please upload Java core videos (I am very excited to learn the fundamentals from you. You are covering the in-depth knowledge which on one covered in my knowledge so far..)

  • @santhoshchandran6952
    @santhoshchandran6952 2 ปีที่แล้ว

    Abhi, try to add more live interview..it's interesting to watch and helpful that how to present during interview. Nice explanation.

  • @vinaykumar-no2zh
    @vinaykumar-no2zh 2 ปีที่แล้ว +1

    Hi Abhilash, In this Student Class constructor, i think we need this.addr = new Address(addr.city,addr.state); this statement instead of directly assigning the reference like this this.addr = addr i hope this is the correct way.correct me if it is wrong.

  • @jagrutipatil1020
    @jagrutipatil1020 ปีที่แล้ว

    Thanks Abhilash ☺️

  • @rahulpant7795
    @rahulpant7795 3 ปีที่แล้ว

    good explanation. thanks.

  • @truecompiler
    @truecompiler หลายเดือนก่อน

    💫

  • @yoshitamahajan353
    @yoshitamahajan353 3 ปีที่แล้ว

    Thank u so much!

  • @rajajodhani
    @rajajodhani 2 ปีที่แล้ว

    Amazing explaination

  • @hackstreet781
    @hackstreet781 2 ปีที่แล้ว

    Good stuff

  • @vishnupriya739
    @vishnupriya739 2 ปีที่แล้ว

    super..

  • @hinayadav6460
    @hinayadav6460 2 ปีที่แล้ว

    Quality content 🔥🔥

  • @marotikudale2890
    @marotikudale2890 2 ปีที่แล้ว

    Very nice quations

  • @Srdc-hw5qc
    @Srdc-hw5qc 3 ปีที่แล้ว

    Hi sir finally I got job in mnc company after seeing your videos so many thanks to you

    • @SeleniumExpress
      @SeleniumExpress  3 ปีที่แล้ว

      Congratulations 🎊 All the best !!

  • @SriHariChukka1210
    @SriHariChukka1210 ปีที่แล้ว

    Great stuff 🙂

  • @rkm7184
    @rkm7184 3 ปีที่แล้ว +1

    Appreciate your teaching ways with explanation but if you make it within 10 minutes video not 40, 50 minutes for a small topic. That would be great for learner otherwise will skip

  • @Phanikishore_adda
    @Phanikishore_adda 2 ปีที่แล้ว

    Hi Abhilash
    Can you take concurrency Concepts in Java and make videos?

  • @HITENDRAsingh-kh4ho
    @HITENDRAsingh-kh4ho ปีที่แล้ว

    Hey
    in which video have you explained why to declare immutable class as final?
    please link it

  • @rohitsharma-xt8qe
    @rohitsharma-xt8qe 2 ปีที่แล้ว

    Everything is good Abhilash the only thing I wanted to say, some portion of the video is a little exaggerated which is not required, the video could have been a little short.

  • @vaibhavsavner2452
    @vaibhavsavner2452 3 ปีที่แล้ว

    can you make a vedio of spring mvc with firebase push notification.

  • @nguyenquan4836
    @nguyenquan4836 ปีที่แล้ว

    ❤❤❤

  • @devireddyjayanthreddy839
    @devireddyjayanthreddy839 3 ปีที่แล้ว

    hi brother, plz make the video in ......."debugging concepts" .....in java

  • @softwaredevelopmentandtest8058
    @softwaredevelopmentandtest8058 3 ปีที่แล้ว

    pls upload the video for spring boot snd microservices

  • @digvijay1228
    @digvijay1228 8 หลายเดือนก่อน

    Hi @Selenium Express,
    is the below code too violation of immutable class. If yes, please help in correcting.
    Address address = new Address("MAA", "Tamil Nadu");
    Student student = new Student("Me", 20, address);
    System.out.println(student);

    address.setCity("BLR");
    address.setState("Karnataka");

    System.out.println(student);

  • @shasha6538
    @shasha6538 2 ปีที่แล้ว

    I think one scenario is missing. If I have list object, how will you deal with that? This scenario should be covered.

  • @danijelmacej7483
    @danijelmacej7483 3 ปีที่แล้ว

    Abilas we waiting for spring boot course over 1 year, and now we must to pay. That course is most important to us for job.

    • @nicolucian6604
      @nicolucian6604 3 ปีที่แล้ว

      A tip: you can watch series on flixzone. I've been using them for watching lots of of movies lately.

    • @ahmedizaiah9199
      @ahmedizaiah9199 3 ปีที่แล้ว

      @Nico Lucian Yup, been watching on Flixzone for since november myself :D

  • @parmodprajapati
    @parmodprajapati 2 ปีที่แล้ว

    Hi
    I have one query:
    Can we create a duplicate object of the immutable class using serializable or reflection API?

  • @worldofwomen3726
    @worldofwomen3726 ปีที่แล้ว

    Student1 s=new Student1(1, "gayathri");
    System.out.println(s);
    s=new Student1(2,"devi");
    System.out.println(s);
    with this the values we can change right even though the variables are final

  • @venkatashanmukha348
    @venkatashanmukha348 3 ปีที่แล้ว

    Waiting for spring boot microservices video

  • @aashishpundir3019
    @aashishpundir3019 ปีที่แล้ว

    but what if we change the content of Address class object using homeAddress object here we are creating separate copy so it works as expected.
    try this example:
    Address homeAddress=new Address("delhi", "Delhi", "India");
    ImmutableClasses ic=new ImmutableClasses(1,"Aashish",homeAddress);
    System.out.println(ic+" "+ic.hashCode());
    homeAddress.setCity("Kotdwar");
    homeAddress.setState("Uttarakhand");
    System.out.println(ic+" "+ic.hashCode());
    Rest everything is fine we can add below lines to the constructor of Immtable class->
    public ImmutableClasses(int id, String name,Address address) {
    this.id = id;
    this.name = name;
    Address clone=new Address(address);
    this.address=clone;
    }
    it will change the content of homeAddress object. @selenium express please correct me if i am wrong.

  • @youssefmoussa2523
    @youssefmoussa2523 3 ปีที่แล้ว

    Please give me a favor i need to learn spring boot can you post some videos about it. Sorry for my English

    • @ParmanandKumar-fj9ct
      @ParmanandKumar-fj9ct 3 ปีที่แล้ว

      So you will be creating new object for each get method. Is this a correct approach

  • @pavankrishnab1
    @pavankrishnab1 ปีที่แล้ว +1

    You missed FINAL keyword before class consequences

  • @RamKumar-rg3gr
    @RamKumar-rg3gr 3 ปีที่แล้ว

    Is that penny from big bang theory

  • @anshupanda2149
    @anshupanda2149 3 ปีที่แล้ว

    When is the next session??

  • @neerajchavan9760
    @neerajchavan9760 3 ปีที่แล้ว

    Hey Abhilash! You really have put on some weight.😂

  • @rkm7184
    @rkm7184 3 ปีที่แล้ว +1

    Your example has 1 bug. If you do any changes in homeAdress object then it will break immutable class concept. Don't take it otherwise. I have been learning very good concepts from your videos

    • @gundamaiahannamaina9273
      @gundamaiahannamaina9273 2 ปีที่แล้ว

      Yes. To fix this, you need to change the student constructor

    • @gundamaiahannamaina9273
      @gundamaiahannamaina9273 2 ปีที่แล้ว

      I used cloning for address object

    • @gundamaiahannamaina9273
      @gundamaiahannamaina9273 2 ปีที่แล้ว

      In Address class, implement Clonable and add clone method. @Override protected Address clone(){return new Address(this.city,this.state,this.country); }

    • @gundamaiahannamaina9273
      @gundamaiahannamaina9273 2 ปีที่แล้ว

      And in Student constructor
      public Student(int id, String name, Address address) {this.id = id; this.name = name; this.address = address.clone(); }

  • @mils3318
    @mils3318 10 หลายเดือนก่อน

    Isn't this kind of copying things called "defensive programming"?! Correct me if i'm wrong

  • @maan9011
    @maan9011 3 ปีที่แล้ว

    You put on weight u were good when u was lean🤣
    Your knowledge directly proportional to weight

  • @sarthakjain6075
    @sarthakjain6075 2 ปีที่แล้ว

    Boss sorry but your videos are too lengthy, really difficult to watch such a long video for small topics..

  • @busyday2023
    @busyday2023 ปีที่แล้ว +1

    Why are u repeating the same thing too many times and wasting our time. It's a 10 min video. Bro Better don't do videos seriously frustrating and wasting time by repeating

  • @himanshutiwari6579
    @himanshutiwari6579 2 ปีที่แล้ว +1

    pahle toh apna pronunciation correct karo...Please dont try to sound like foreigner .Be you ,be original

    • @SeleniumExpress
      @SeleniumExpress  2 ปีที่แล้ว

      Hi Himanshu ! Thank you for taking time and writing a feedback.🙂

  • @muralitekumatla4699
    @muralitekumatla4699 2 ปีที่แล้ว

    Hi Abhilash,
    Very impressive explanation :)
    But I have a doubt - Immutability means " Once an object is initialized, we are not allowed to change its content(value). And when we try to change its content, if there is any change in the content then new object will be created, if there is no change in the content then the original object should be returned" . Just given String behavior example below....
    package com.corejava.string;
    public class StringTest {
    public static void main(String[] args) {

    String str1 = new String("Murali");
    String str2 = new String("Murali");
    String str3 = new String("Murali Tekumatla");
    String str4 = str1.replace('M', 'M');
    String str5 = str1.replace('M', 'T');

    System.out.println("str1 ======" + str1 + "=======" + str1.hashCode());
    System.out.println("str2 ======" + str2 + "=======" + str2.hashCode());
    System.out.println("str3 ======" + str3 + "=======" + str3.hashCode());
    System.out.println("str4 ======" + str4 + "=======" + str4.hashCode());
    System.out.println("str5 ======" + str5 + "=======" + str5.hashCode());
    System.out.println("str1 ======" + str1 + "=======" + str1.hashCode());

    }
    }
    Output:
    str1 ======Murali=======-1978977868
    str2 ======Murali=======-1978977868
    str3 ======Murali Tekumatla=======1155040398
    str4 ======Murali=======-1978977868
    str5 ======Turali=======-1778573811
    str1 ======Murali=======-1978977868
    1. In above example, str1 created.
    2. str2 object created with same content hence it return original object as hashcode is same as str1.
    3. str3 created with differenct content hence new obj is created with diff hashcode.
    4. str4 - trying replace char 'M' with 'M', no content change hence original obj is returned.
    5. str5 - trying replace char 'M' with 'T', content change is there hence new obj is created.
    I think we are missing this behavior in our Student-Address example in this video - can you please comment on this.
    - Thanks