class and object | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

class and object

SuperClass s=new SubClass(); actually what is happening when this code runs. and what is the need to create an instance of subclass using the super class

27th Nov 2017, 3:22 PM
Santhosh Selvam
Santhosh Selvam - avatar
1 Answer
+ 2
You are creating a instance of SubClass and typecasting it to the SuperClass. It remains a SubClass instance and could be used as such. However, the main reason for this is having a second subclass both implementing different actions for the super class method. For example, calling speak of the super class language would end up speaking French or Spanish in the corresponding subclass.
27th Nov 2017, 4:20 PM
John Wells
John Wells - avatar