SuperClass object = new SubClass();
Why use a superclass to instantiate a subclass object like above? Because the only way i learnt to instantiate an object is to:
SubClass object = new SubClass();
I’m learning java.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You may have a method that only takes an instance of
SuperClass. SinceSubClassis aSuperClass, you can use an instance ofSubClassand treat it asSuperClass.The same behavior is used when working with interfaces:
That’s the beauty of polymorphism. It allows you to change out the implementation of the class’ internals without breaking the rest of your code.