Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A simple inquiry regarding object instantiation

Tags:

java

oop

Hello StackOverflowers.

This is almost certainly a very basic question regarding object instantiation but in the following code sample:

List myList = new LinkedList();

Is this a form of inheritance? In other words, would one read this has LinkedList "is a" List and therefore inherits the methods defined in the List class? If so, provided a user constructed two classes of his or her own and used the same syntax as above, would the answer be the same?

Thanks all.

Caitlin

like image 532
CaitlinG Avatar asked Dec 25 '22 22:12

CaitlinG


1 Answers

Is this a form of inheritance?

No.

would one read this has LinkedList "is a" List

Yes.

and therefore inherits the methods defined in the List class?

No. List is an interface and therefore cannot be extended/inherited (only implemented). But LinkedList still passes as IS-A because it implements all the methods required by the List interface.

If a user constructed two classes of his or her own with one being the base class and the other derived from it then yes it would be inheritance. But, the following

BaseType base = new SubType();

doesn't exactly demonstrate inheritance but polymorphism made possible by inheritance i.e. since the sub type IS-A base type as well it can be assigned to a base type reference.

like image 106
Ravi K Thapliyal Avatar answered Jan 12 '23 01:01

Ravi K Thapliyal