+ 5

What is logic behind "parent p = new child( );" in Java?

Please tell me difference between "child p = new child( )" and "parent p = new child( );" in inheritance where class child extends parent.

9th Dec 2019, 5:26 PM
Vishwa Bharti
Vishwa Bharti - avatar
23 odpowiedzi
+ 5
In the 1st case Parent class is used as a reference so the methods and variables defined in parent class alone are accessible. In 2nd case Child class is used as a reference so that all methods of parent class can be accessed as well as the extra methods of child class(if present) can also be accessed.
9th Dec 2019, 5:45 PM
Avinesh
Avinesh - avatar
+ 8
You should also read the lesson about up- and downcasting: https://www.sololearn.com/learn/Java/2169/?ref=app Here is a small code: https://code.sololearn.com/cVMgmDu6uAmU/?ref=app
9th Dec 2019, 7:16 PM
Denise Roßberg
Denise Roßberg - avatar
+ 4
Also while learning java, start using terms as classes, references and instances instead of saying data types and objects. It can be confusing at times just like ~ swim ~ mentioned.
9th Dec 2019, 7:09 PM
Avinesh
Avinesh - avatar
+ 4
Polymorphism. While both ways creates new instance of Child class, it is preferred to declare as Parent datatype. The reason is that you can use ArrayList of Parent datatype to loop over different children. See this demo: https://code.sololearn.com/cdjGFVDGQ779/?ref=app
10th Dec 2019, 3:10 AM
Gordon
Gordon - avatar
+ 3
If you are creating a chatbot App that communicates with users of different nationalities and speak different languages, you use a reference to the generic chatbot and instantiate a language specific one based on the user's language. When providing answers to the user's questions, you use the generic super class methods that are overriden at run time by the language specific ones of the relevant instance. That's just an example where this construct maybe used. Hope it's not too unclear.
10th Dec 2019, 7:48 PM
Sonic
Sonic - avatar
+ 3
Though I do not sometimes like the way you talk but believe me I'm not that bad to down vote anybody's answer. So do not indirectly mention me. I did not even have to do this.
12th Dec 2019, 12:52 AM
Avinesh
Avinesh - avatar
+ 3
Disclaimer: Wasn't me either, I have no clue about Java. :) I want to remind, though, that downvotes, given the right circumstances, are sanctioned (one could infer recommended) by the guidelines. One of the valid reasons are wrong or misleading answers. And 'giving a reason' everytime you downvote, would make the anonymity quite pointless, wouldn't it? And it will frequently not lead to 'Oh, you're right', but only to an argument. Yes, I tried. So nobody has to like or give downvotes, but it seems silly to act as if there's something fundamentally wrong with them.
12th Dec 2019, 1:30 PM
HonFu
HonFu - avatar
+ 2
Its oops concept ... To create an object ... It is in th form .... Parent class reference = child class object Parent class methods can be accessed by child class.
9th Dec 2019, 8:16 PM
Mk7
+ 2
Mirielle🐶 [Inactive] The post of Bariş Özkaya and the example of a chatbot from Sonic has also a downvote. I don't know why...
11th Dec 2019, 7:41 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Just ignore those downvotes without any valid reasons being given.
11th Dec 2019, 8:19 PM
minirkk
minirkk - avatar
0
This kind of declerations are help you to update ur programe easly in the future. Creating ur object like child c = new child() saves the day , Creating like Parent c = new child() saves ur future.
9th Dec 2019, 10:03 PM
Barış Özkaya
Barış Özkaya - avatar
0
Parent is a non primative data type ,there is a class by this name p is object from child class this used to relate the children by there parents
11th Dec 2019, 5:15 PM
Omar Meto
Omar Meto - avatar