Assume that class B extends from class A.
So what is the advantage of writing something like this:
A myClassA = new B();
What’s the difference to:
B myClassA = new B();
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.
This is the concept of programming to interfaces(or supertype)
we use supertype reference so that it can hold any sub type instance
eg: assume we have Class B & C extending A
& some method which accepts A as parameter
void print(A a) {System.out.println(a);}
this method canbe invoked by passing any instance of A or its subtype