Covariant Return Type in Java | Tutorial for beginners

Поделиться
HTML-код
  • Опубликовано: 6 сен 2024
  • Java Covariant Return Type | Tutorial for beginners
    About this video:
    This video will explain what is covariant return type in simple way with practical example
    Links:
    Free core Java tutorials for beginners 👇
    • Free Java Basics Tutor...
    Method Overriding:
    • Method Overriding in J...
    Polymorphism:
    • Java Polymorphism and ...
    Notes:
    Topic : Covariant Return Type
    As we discussed in method overriding.
    If we have to override parent class method in it's child class then return type,method name and parameters of child class method must be exactly same as that of parent class method's return type.
    But Java version 5.0 onword,
    Java introduce new feature for method overriding and that is we can change return type of overriding method
    That means,
    Java version 5.0 onword,
    It become possible,to override any method by changing the return type only.
    But there is no straight forward way to change return type of overriding method.
    Some conditions are involved there such as
    If we have to change return type of overriding method then return type of parent class method must be non primitive type and non-primitive type means non-primitive data types
    and non-primitive data types in Java are Object / Classes, String and Arrays..
    That means,
    If return type of parent class method is non primitive type then and only then we can change the return type of overriding method otherwise compiler will raise an error..
    Now, let's have an example,
    class A{
    //overridden method
    A m1(){
    // create obj1
    A obj1 = new A();
    System.out.println("I am class A");
    return obj1;// return obj1
    }
    }
    class B extends A{
    // overriding method
    B m1(){
    B obj2 = new B();
    System.out.println("I am class B");
    return obj2;
    }
    }
    class Test{
    public static void main(String [ ] args){
    B obj = new B();
    obj.m1();
    }
    }
    Output : I am class B
    In above example, return type of paren class method m1() is A and A is super class and hence A is called super type.
    So basically,
    Return type of parent class method m1() is super type.
    And return type of child class method is B and B is sub class of A and hence B is called subtype so here return type of child class method is subtype of parent class method's return type so if we noticed here,we have changed return type of overriding method..
    So in above example,
    return type of parent class method is different and return type of overriding method is different that means both the method have different return type and this concept is nothing but the covariant return type and covariant means different..
    So basically,
    Covariant return type allows us to change the return type of the overriding method but return type of overriding method must be a subtype of parent class method's return type.
    __________End__________
    Thank you so much for watching! 🙏
    #Covariantreturntypeinjava
    #javacovarientreturntype
    #sgrutorial
    #javaforbeginners
    #javacovariantreturntypewithexample
    #freecorejavatutorialsforbeginners
    #javatutorialforbeginners
    #javabasicsforbeginners
    #corejavatutorialsforbeginners
    #javaforbeginners

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

  • @user-fs3do6yy5k
    @user-fs3do6yy5k 5 месяцев назад +1

    Such great explanation Mam...Please keep sharing such knowledgeable video on java...Thank you so much.

    • @sgtutorial22
      @sgtutorial22  5 месяцев назад

      Yes sure,thank you for your feedback.. happy learning..

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

    I went across so many channels,but you explained in aclear and crystal way than any other channels.Thankyou very much

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

      Thank you for your valuable feedback..keep learning...

  • @user-wr7nn7tz8n
    @user-wr7nn7tz8n 7 месяцев назад

    clear explanation thank you...keep posting videos to help understand concepts.

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

      Yes sure, thank you so much for your feedback..!

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

    AWSM explaination without any rush, Thanks alot

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

      Thank you so much for your valuable feedback..

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

    Cleared my covariant concepts thanks a lot 🎉

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

      Thank you for your feedback...keep learning!

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

    Thank you. Your lesson is very helpful!!!

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

      Thank you so much for your feedback.
      Keep learning..!

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

    Thank you for this wonderful explanation.

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

      Thank you so much for your feedback..🙏

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

    What is the link between class P and A.. How you use return type as P inside class A? Cz only Q extends P..

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

      Hello Isuru,
      There is no link between class P and class A but both the classes are in same package I.e in com.sg package and class P has default access modifier and that's why we can easily access or use class P or class P members in class A..
      Thank you!