Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.


+15 votes

In this case:
TypeA objA = new TypeB();

I said:
TypeB class has to be a super class of TypeA in order to create a object that is TypeA from TypeB.
Am I correct?

asked in CSC305 Fall 2022 by (8 points)

2 Answers

+10 votes

TypeA can also be an interface that TypeB implements. e.g.

List myList = new ArrayList();

Also, TypeA could be a superclass of a superclass of TypeB, etc.

answered by (8 points)

Or could TypeA be an abstract class which TypeB is a subclass of?

+9 votes

I think it's for interfaces though, like for instance:

List names = new ArrayList<>();

answered by (8 points)