I believe this question needs some illustration.
So, here is a sample code.
// Animal - abstract class (Parent class)
// Dog - inherited class (Sub class)
Animal puppy = new Dog(); // creating an instance.
So my question is, how is using both the class names when instantiating an object is right? My pardon If this comes as a silly question. Can someone explain the breakdown of this weird instantiation?
new Dog()
creates a new instance of Dog and returns a reference to that Dog
Animal puppy
creates a variable (a named thing that can hold a reference to an object) that is capable of referring to any object that is an Animal
=
initializes the variable on the left to the value on the right. Since a Dog is an Animal, a reference to a Dog is a reference to an Animal, and therefore the initialization is legal.