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.... .
i understood completely only here after watching several videos of all international youtubers
Your a Good Teacher
For giving Confidence Levels
Thank you Sir
I follow many videos for java but Durga sir explains everything nicely.
You are awsome in explain any thing...
U r really good teacher..
Sir I am a big fan of your tutorials. Keep up the good work.
Thanks a lot; Your classes are spectacular! It was denying your credit if I wouldn't be subscribed!
Excellent presentation on coding to interfaces/subtype concept.
No anyone teach you sir. Rally u teach very well thanks a lots sir
You are great sir.. finally I found it.
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.
Lov you Durga sir:) You are the best teacher:)
Very very thankful to you
Sir your way of enplane is very good..
Very well explained the concept... Salutes!
Superb Sir, Thank you so much
I was asked what is polymorphism in the interview today.I wish i had watched this video yesterday
Thanks for uploading it
I am big fan Durga sir
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
Shareef Hiasat what you mean by applying generic api framewkrk for your work
fantastic I say..super explanation
Durga sir,pls upload the networking classes
Well explained. Thanks sir.
You’re excellent mr. Durga
Nice sir 👍
Thanku soo much
Very nice
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
+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.
This is Polymorphic reference in which method of sub class is called and data of superclass is called.
vraiment une explication tres claire. merci beaucoup!
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
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
Sir without u.. I cannot learn java... I saw all 199 videos... I want to learn spring frame work... Can u explain sir...
If you learnt Java off 199 videos, you can learn Spring online too. High motivation. Congrats
sir....plz add complete tutorial on generics
+shreya verma yea
Thnx a lot
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.
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.
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.
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
What is runtime object ?
thank you sir !!
Very good explanation....
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
thanks a lot sir..
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?
Hi sir, if array list is able to hold all type of objects then why we use Object class ref to hold the same?
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
How can I handle exception handling in multicast delegates c#
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
+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..
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...
this is right way of doing dynamic polymorphism.
yes, bro u r right & we can also do downcasting .
30+ faculty to train Java. Reminds me of Kota Factory.
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
I have more dout
why overriding concept not applicable of variable
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.
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
Reason is overriding with dynamic dispatch method
Run time polymorphism
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????
You can always type cast
why we cant call child members using parent class reference ???
parent class reference holds the address of child class object.
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.
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;
}
}
P p = new P();
can say this is the static polymorphism ?
What you wrote is not polymorphism.
sir please upload videos on threading.
+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.
What if we have overridden method in child that is in base with P p= new C();
Why child function is called?
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!
what is an object and why required to create an object
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...
}
parent class reference variable can store child class object but child class reference variable can,t store parent class object why?
sreenivasa reddy parent class has the access to hold the child class object. Whereas child class don't.
8:45
Can possible to create Object for Parent class with child class ref... ex: Child c = new Parent();
no
Hi team I am using udemy please upload latest videos on that related to java 8 and share link with me
Can we use child c = new parent ()
no
3rd point yhi to bola sir ne
Thanks
please in Telugu