As we all know that we cant instantiate an abstract class. But look into this code:
abstract class Bike
{
int limit=30;
Bike()
{
System.out.println("constructor is invoked");
}
void getDetails()
{
System.out.println("it has two wheels");
}
abstract void run();
}
class Honda extends Bike{
void run()
{
System.out.println("running safely..");
}
public static void main(String args[])
{
Bike obj = new Honda();
obj.run();
obj.getDetails();
System.out.println(obj.limit);
}
}
Here we are creating object of Honda class by using reference of Superclass(Abstract class). Again we know that when ever subclass object is created 1st the super class constructer is called. Now the super class constructor is called then we know that first object is created in memory then constructed in called. Then why here object is not created.