Difference between Parent p = new Child(); and Child c = new Child();

Поделиться
HTML-код
  • Опубликовано: 8 сен 2024
  • DURGASOFT is INDIA's No.1 Software Training Center offers
    online training on various technologies like JAVA, .NET ,
    ANDROID,HADOOP,TESTING TOOLS , ADF, INFORMATICA, SAP...
    courses from Hyderabad & Bangalore -India with Real Time Experts.
    Mail us your requirements to durgasoftonlinetraining@gmail.com
    so that our Supporting Team will arrange Demo Sessions.
    Ph:Call +91-8885252627,+91-7207212428,+91-7207212427,+91-8096969696.
    durgasoft.com
    durgasoftonline...
    / durgasoftware
    durgajobs.com
    / durgajobsinfo.... .

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

  • @satvikjha8932
    @satvikjha8932 3 года назад +4

    i understood completely only here after watching several videos of all international youtubers

  • @mohammedsajeed7795
    @mohammedsajeed7795 7 лет назад +8

    Your a Good Teacher
    For giving Confidence Levels
    Thank you Sir

  • @manindrasikder2673
    @manindrasikder2673 6 лет назад +3

    I follow many videos for java but Durga sir explains everything nicely.

  • @mallikarjunbejugam1574
    @mallikarjunbejugam1574 8 лет назад +8

    You are awsome in explain any thing...

  • @developersstation9532
    @developersstation9532 7 лет назад +7

    U r really good teacher..

  • @sankarsan
    @sankarsan 5 лет назад +3

    Sir I am a big fan of your tutorials. Keep up the good work.

  • @firewabebe1576
    @firewabebe1576 8 лет назад +5

    Thanks a lot; Your classes are spectacular! It was denying your credit if I wouldn't be subscribed!

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

    Excellent presentation on coding to interfaces/subtype concept.

  • @maheshpunde9775
    @maheshpunde9775 4 года назад

    No anyone teach you sir. Rally u teach very well thanks a lots sir

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

    You are great sir.. finally I found it.

  • @westindiaworks
    @westindiaworks 6 лет назад +1

    very nice approach to teach sir. And very good topic you have chose this time to teach us. I was finding for this one on google and you was the first result there on. With you good luck for future tutorials.

  • @DavinderSingh-kn8qz
    @DavinderSingh-kn8qz 3 года назад

    Lov you Durga sir:) You are the best teacher:)

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

    Very very thankful to you

  • @pankajthorat4606
    @pankajthorat4606 5 лет назад

    Sir your way of enplane is very good..

  • @MayankGuptaMayankGupta
    @MayankGuptaMayankGupta 4 года назад

    Very well explained the concept... Salutes!

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

    Superb Sir, Thank you so much

  • @DeepakKumar2105
    @DeepakKumar2105 6 лет назад +4

    I was asked what is polymorphism in the interview today.I wish i had watched this video yesterday

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

    Thanks for uploading it

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

    I am big fan Durga sir

  • @shareefhiasat9746
    @shareefhiasat9746 9 лет назад +1

    some times you may use instanceof to guess the child ovject type but you use polymorphism to apply generic API frame work for you work (for less code) and decoupling please enhance the concepts of design i would love to see this

    • @jonassx100
      @jonassx100 6 лет назад

      Shareef Hiasat what you mean by applying generic api framewkrk for your work

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

    fantastic I say..super explanation

  • @saranyanelluru5871
    @saranyanelluru5871 8 лет назад +3

    Durga sir,pls upload the networking classes

  • @shankars4281
    @shankars4281 4 года назад

    Well explained. Thanks sir.

  • @sekharreddy3236
    @sekharreddy3236 5 лет назад

    You’re excellent mr. Durga

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

    Nice sir 👍

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

    Thanku soo much

  • @ayanmukherjee5066
    @ayanmukherjee5066 4 года назад

    Very nice

  • @ycce17
    @ycce17 8 лет назад +1

    Hello Durga Sir,
    I tried the above concept but it is now satisfying the 2nd difference where if we use Parent reference and Child Object..i.e Parent p=new Child() then only Parent method got called and we cant call Child method.
    PFB code with output.
    *** Correct me if I am wrong.
    public class Parent{
    public void message(){System.out.println("Parent Called");}}
    public class Child extends Parent{
    public void message(){System.out.println("Child Called");}}
    public class MainClass{
    public static void main(String args[]){
    Parent p=new Child();
    p.message();
    }}
    Output: Child Called

    • @codeWithNoComments
      @codeWithNoComments 8 лет назад +1

      +rahul nikhare The child is overriding the Parent Method. If you have "CHILD SPECIFIC" methods you won't be able to call. Thats what this means. Change the name of the method message in child and retry.

    • @LambodarThakur
      @LambodarThakur 8 лет назад +1

      This is Polymorphic reference in which method of sub class is called and data of superclass is called.

  • @malabar747
    @malabar747 5 лет назад +1

    vraiment une explication tres claire. merci beaucoup!

  • @BhargavPatel4466
    @BhargavPatel4466 8 лет назад +3

    Hi Sir.... I am agree with @azarraxx1
    We can call the child calls method using the parent reference if we override it.
    correct me if i am wrong.
    Thanks

    • @nizamcool99
      @nizamcool99 5 лет назад

      You are right . We can call the child class methods when we override it. I have also given an example in my comment:
      Program:
      package com.oops.concepts;
      public class MethodOverriidingExample {
      public static void main(String[] args) {
      Animal obj1 = new Animal();
      obj1.animalmethod();
      obj1.bob();

      Dog obj2 = new Dog();
      obj2.animalmethod();
      obj2.gally();

      Animal obj3 = new Dog();
      obj3.animalmethod();
      obj3.bob();
      }
      }
      class Animal{

      public void animalmethod()
      {
      System.out.println("This is animalmethod from Animal Class");
      }

      public void bob()
      {
      System.out.println("This is bob method in Animal class");
      }
      }
      class Dog extends Animal{

      public void animalmethod()
      {
      System.out.println("This is animalmethod from Dog Class");
      }

      public void gally()
      {
      System.out.println("This is gally method in Dog class");
      }
      }
      Result:
      This is animalmethod from Animal Class
      This is bob method in Animal class
      This is animalmethod from Dog Class
      This is gally method in Dog class
      This is animalmethod from Dog Class
      This is bob method in Animal class

  • @shashikumarmacha9972
    @shashikumarmacha9972 5 лет назад +1

    Sir without u.. I cannot learn java... I saw all 199 videos... I want to learn spring frame work... Can u explain sir...

    • @letvarconst
      @letvarconst 5 лет назад

      If you learnt Java off 199 videos, you can learn Spring online too. High motivation. Congrats

  • @shreyaverma2947
    @shreyaverma2947 9 лет назад +2

    sir....plz add complete tutorial on generics

  • @mauryar16
    @mauryar16 4 года назад

    Thnx a lot

  • @mail2jimmy747
    @mail2jimmy747 9 лет назад +1

    Hello Durga Sir,
    I have a confusion about the topic you are covering here. After looking at your video, I wrote the program to implement the same thing you are discussing in this video, but I am not getting the output as per the explanation you are giving in this video. In this turorial you are saying that if we use
    Parent P = new Child();
    then we will not be able to call methods of Child class, but using the same approach, I am able to call the method of Child class. Here is my program and output
    class Parent {
    String whoAmI() {
    return "I am Parent";
    }
    }
    class Child extends Parent {
    String whoAmI() {
    return "I am Child";
    }
    }
    public class Test {
    public static void main(String[] args) {
    Parent p1 = new Parent();
    Parent p2 = new Child();
    System.out.println(p1.whoAmI());
    System.out.println(p2.whoAmI());
    }
    }
    ****************************************
    OUTPUT:
    I am Parent
    I am Child
    ****************************************
    I know that either I am not able to understand this topic properly or you are missing some information about this topic in this video. Please clear my doubt. I will be very thankful to you.

    • @amitmohanty2679
      @amitmohanty2679 9 лет назад +1

      Hi Jimmy its behave differently because of method overriding.becoz the method present in both and parent and child class and your object creation is in polymorphic form so during execution it checks the object type and execute the method from child class.
      If its a simple "parent object" then it only execute from parent class..it doesn't know what is inside child class.

    • @sandeepnandagiri9015
      @sandeepnandagiri9015 5 лет назад

      He clearly told that we can't call child class specific method, it is parent method only overridden in child class. So we can call child class overridden methods which are parent class methods. Suppose if you mention other than parent class method which is child specific then that method will not be called.

  • @nizamcool99
    @nizamcool99 5 лет назад +3

    I want to add one more point for the second Point - If there is a method which is Overridden in the Child Class, then the overridden method from Child class will be called. Please find the example for more clarity.
    Example:
    public class MethodOverriidingExample {
    public static void main(String[] args) {
    Animal obj1 = new Animal();
    obj1.animalmethod();
    obj1.bob();

    Dog obj2 = new Dog();
    obj2.animalmethod();
    obj2.gally();

    Animal obj3 = new Dog();
    obj3.animalmethod();
    obj3.bob();
    }
    }
    class Animal{

    public void animalmethod()
    {
    System.out.println("This is animalmethod from Animal Class");
    }

    public void bob()
    {
    System.out.println("This is bob method in Animal class");
    }
    }
    class Dog extends Animal{

    public void animalmethod()
    {
    System.out.println("This is animalmethod from Dog Class");
    }

    public void gally()
    {
    System.out.println("This is gally method in Dog class");
    }
    }
    Result:
    This is animalmethod from Animal Class
    This is bob method in Animal class
    This is animalmethod from Dog Class
    This is gally method in Dog class
    This is animalmethod from Dog Class
    This is bob method in Animal class

  • @firdaousslotfi9616
    @firdaousslotfi9616 4 года назад

    thank you sir !!

  • @jayesh5003
    @jayesh5003 8 лет назад

    Very good explanation....

    • @DURGAEDUCATION
      @DURGAEDUCATION  8 лет назад +1

      Hello,
      We are very Happy to say Now you can access All DURGA Sir's
      Core Java with OCJP/SCJP Video Sessions(200 Videos,21 Chapters,200 hours ). To
      get Maximum Benefit from these video sessions, compulsory you have to write
      notes also. Sir will dictate each and every point clearly.
      Demo Sessions on
      RUclips:
      1.LanguageFundamentals(16Videos) ruclips.net/video/eTXd89t8ngI/видео.html
      2.Operators&Assignments(7Videos) ruclips.net/video/1JGOPhIyhAM/видео.html
      3.FlowControl(6Videos) ruclips.net/video/X6rS9vD9qM/видео.html
      4.ExceptionHandling(10Videos) ruclips.net/p/PLd3UqWTnYXOnSkaum39zWYI6bPb3i7liz
      Please give your valuable feedback and it is very important
      and helpful for us.
      Note:-If you are interested for full version of videos
      please revert back for more information.
      Fee : 6000/-
      Duration: 6-months
      DURGASOFT Online
      Training Team,
      Mobile : +91-8885 25
      26 27, +91-7207 21 24 28
      Mail ID : durgasoftonline@gmail.com ,
      durgasoftonlinetraining@gmail.com

  • @rohitnegi1678
    @rohitnegi1678 7 лет назад

    thanks a lot sir..

  • @ykishore4340
    @ykishore4340 6 лет назад

    You dive deep under the hood. This explanation I heard for the 1st time and got a lot of doubts cleared. Just one question. P p=new c(). Can't we overload new c() with new methods?

  • @prashant8587
    @prashant8587 4 года назад +1

    Hi sir, if array list is able to hold all type of objects then why we use Object class ref to hold the same?

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

      ArrayList is able to hold only array type object but if we want to hold differnt object the we can refer LIST
      List L = new arraylist();
      For more detailed video watch @technicalTalk

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

    How can I handle exception handling in multicast delegates c#

  • @lakshminandoor4533
    @lakshminandoor4533 9 лет назад +1

    P p=new c();
    here we can access only parent class methods only dn what actually happening inside jvm
    i mean dt reference z of parent so it will load only parent class method in child object
    i want d clear picture wt happening exactly

    • @AkashJaiswalAkash7771
      @AkashJaiswalAkash7771 8 лет назад +2

      +lakshmi nandoor Look dear,,
      Parent p = new Child(); // When you write this ..
      Parent p ---> JVM will create a reference variable p in STACK MEMORY OF MAIN METHOD..OK
      Now,
      new Keyword will assign memory in HEAP Section... (Its the job of New Keyword .. whenever you write New .. JVM will assign memory in HEAP..)
      AGAIN.. new Child();
      Now Child() Constructor is called. ..// Look when you create a class it by default has a constructor with the name same as class name ..
      e.g class parent{
      //some variable
      //This is a constructor .. if you don't write it explicitly .. then by default it is created by JVM..
      parent(){
      }
      //some method
      }
      Ok .. now child () is called and it will allocate memory for all the instance variable..Contiguously .. at memory location say 1691000 .
      SO
      We have a Parent Class ka reference variable p . Now p will get the memory address ..i.e 1691000 ..
      Ok..

  • @shubhendumaurya6248
    @shubhendumaurya6248 7 лет назад +1

    class Bike{
    void run(){System.out.println("running");}
    }
    class Splender extends Bike{
    void run(){System.out.println("running safely with 60km");}
    }
    class GFG{
    public static void main(String args[]){
    Bike b = new Splender();//upcasting
    b.run(); //running safely with 60km
    }
    }
    Contradiction to your lecture...

    • @davidlakhotia3854
      @davidlakhotia3854 6 лет назад

      this is right way of doing dynamic polymorphism.

    • @VARUNSINGH-vi2dd
      @VARUNSINGH-vi2dd 6 лет назад

      yes, bro u r right & we can also do downcasting .

  • @desilad3912
    @desilad3912 4 года назад

    30+ faculty to train Java. Reminds me of Kota Factory.

  • @prabhatsingh8606
    @prabhatsingh8606 8 лет назад

    class A{ int i = 0; void getMethod(){ System.out.println("A"); }} class B extends A{ int i = 2; void getMethod(){ System.out.println("B"); } public static void main(String arg[]){ A obj = new B(); System.out.println(obj.i); // Prints 0 --- Inttance Variable of A obj.getMethod(); // Calls getMethod of B } }
    Query how parent class refrence object is able to call child class method,contradicting with your above lecture sir,please tag me with answer,
    Thnaking you.
    Prabhat Singh

  • @meenakshipatel8931
    @meenakshipatel8931 5 лет назад

    why overriding concept not applicable of variable

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

    Hello , Very nice lecture and beautifully explained . One doubt , If in child class we override a method and then do Parent p = new Child() which implementation will be called ( parent or child ) . As per concept , parent class implementation should be called but when I code child class implementation gets called.

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

      In case of overriding method calling is based on object of class
      So if you give parent reference and create object of child. It will call child method beacause its overriding

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

      Reason is overriding with dynamic dispatch method

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

    Run time polymorphism

  • @Dawnsunflower156
    @Dawnsunflower156 4 года назад

    but why we cannot access child's personal methods by parent reference variable,as it is holding the reference Id of the child,,and every part of a class can be accessed via reference Id????

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

    why we cant call child members using parent class reference ???
    parent class reference holds the address of child class object.

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

      Child has everything Parent has , so Child can use parents money as well as his own money.
      But Parent has only his earned money.
      Now , if Parent asks from child , child will not give his money , Child will he'll say Oye..parent you have so much money why take from me?
      Basically , Child inherits all the methods of Parent so child can use both his methods and Parent's methods. [ class Child extends Parent{ ... } ]
      But Parent doesn't inherit Child's methods so Parent can't call child's methods.

  • @rashichauhan1139
    @rashichauhan1139 6 лет назад

    Sir please explain me the output of this program. why is the output coming out to be 1sub2 ??
    why get1() of arabic class is being called??
    public class Greek{
    int i=1;
    public int get1(){
    System.out.print("Super");
    return i;
    }
    public static void main(String args[]){
    Greek ga = new Arabik();
    System.out.print(ga.i);
    System.out.print(ga.get1());
    }
    }
    class Arabik extends Greek{
    int i=2;
    public int get1(){
    System.out.print("Sub");
    return i;
    }
    }

  • @sathishyelle8208
    @sathishyelle8208 4 года назад

    P p = new P();
    can say this is the static polymorphism ?

    • @ibrahimsm5283
      @ibrahimsm5283 4 года назад

      What you wrote is not polymorphism.

  • @saksham41592
    @saksham41592 10 лет назад

    sir please upload videos on threading.

    • @DURGAEDUCATION
      @DURGAEDUCATION  9 лет назад

      +Practical Programing Forum
      Demo Sessions on youtube,
      Language Fundamentals(16 Videos)ruclips.net/video/eTXd89t8ngI/видео.html,
      Operators & Assignments(7 Videos)ruclips.net/video/1JGOPhIyhAM/видео.html,
      Flow Control(6 Videos)ruclips.net/video/X6rS9vD9-qM/видео.html.

  • @akshitbansal1983
    @akshitbansal1983 6 лет назад +2

    What if we have overridden method in child that is in base with P p= new C();
    Why child function is called?

    • @letvarconst
      @letvarconst 5 лет назад +1

      Child method is called because when you override any method present in Parent or Base class, the Child class now contains that overridden method physically (earlier it was only available in Parent's class). So whenever you call the method, it is always available in child object and it doesn't have to go to the parent to call that method. Hope it helps!

  • @jeevag7995
    @jeevag7995 6 лет назад

    what is an object and why required to create an object

    • @vnaveenkumar982
      @vnaveenkumar982 5 лет назад +3

      ok let me explain this ...
      think in this way... class is just like having the body parts like face,hands,and legs, you are creating everything in the class but to get that parts into use that body need to be (alive or it need to have life) ... that's where the object comes into picture..
      if you are creating an object then it means you are giving life to the class.. that means legs,hands,face every works then only right...
      class body{
      int leg,hand,face;
      }
      class temp{
      public static void main(String args[ ]){
      body b=new body();
      //here the object is created that means you are giving life to the body
      // that means it will be created ... in the jvm.. in the usage of the program
      b.leg=20;
      //here you are giving the length of leg that means now you are using the leg...
      }

  • @sreenivasareddy3741
    @sreenivasareddy3741 6 лет назад

    parent class reference variable can store child class object but child class reference variable can,t store parent class object why?

    • @priyamj5134
      @priyamj5134 6 лет назад

      sreenivasa reddy parent class has the access to hold the child class object. Whereas child class don't.

  • @javadevharikrishna
    @javadevharikrishna 4 года назад

    8:45

  • @Kushal99999
    @Kushal99999 6 лет назад

    Can possible to create Object for Parent class with child class ref... ex: Child c = new Parent();

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

    Hi team I am using udemy please upload latest videos on that related to java 8 and share link with me

  • @shreyaraut9270
    @shreyaraut9270 5 лет назад

    Can we use child c = new parent ()

  • @krishnamurthynaidu8901
    @krishnamurthynaidu8901 7 лет назад

    please in Telugu