+ 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 Odpowiedź
+ 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