• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

What topic to be learned/read to understand the below concept?

 
Ranch Hand
Posts: 221
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

From the below code I can understand that class B can access the instances and methods of class A. But I'm struggling to understand how the array of Class A allows to store the class B instances.
I googled before here but could not get the exact link, so do I need understand more in depth of IS-A and HAS-A concept  to get clear understanding ? My doubt is how class A instances and class B instances are same ? How jdk allows to treat that way ? Similarly, generics also allows to store the same way ? Can some shed some light on this please ? Thanks.

 
Saloon Keeper
Posts: 3443
149
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Mohammed,

I always remember this by remembering what I learned at primary school: a Cow is an Animal, but an Animal needs not be a Cow.

So, if we have class Animal and class Cow extends Animal, and we have an array Animal[] animals, would you say that

is oke? Does it make sense? And what about the reverse (having an array Cow[] cows):

does that look right to you?
 
Ranch Hand
Posts: 66
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mohammed Sardar. wrote:Hi All,

From the below code I can understand that class B can access the instances and methods of class A. But I'm struggling to understand how the array of Class A allows to store the class B instances.
I googled before here but could not get the exact link, so do I need understand more in depth of IS-A and HAS-A concept  to get clear understanding ? My doubt is how class A instances and class B instances are same ? How jdk allows to treat that way ? Similarly, generics also allows to store the same way ? Can some shed some light on this please ? Thanks.

you need to know array super type, subtype relationships
Example
Array of strings can be assignable to array of object type  because string is directly extends object class implicitly
And array of object type can be assigned to object class
So A is  superclass to B
Where ever superclass object is used a subclass object can be used
 
Mohammed Sardar.
Ranch Hand
Posts: 221
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Mohammed,

I always remember this by remembering what I learned at primary school: a Cow is an Animal, but an Animal needs not be a Cow.

So, if we have class Animal and class Cow extends Animal, and we have an array Animal[] animals, would you say that

is oke? Does it make sense? And what about the reverse (having an array Cow[] cows):

does that look right to you?



It makes sense to me Cow can be an Animal but Animal can not be a cow. !! Is that what you meant right ? Thanks so much.
 
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mohammed Sardar. wrote:It makes sense to me Cow can be an Animal but Animal can not be a cow. !! Is that what you meant right ?



A Cow IS an Animal. However, an Animal may not always be a Cow (it may also refer to a Dog, Cat, Horse, Duck, Whale, etc.).  

It's a matter of going from Specific to General and vice versa.  Another example: Dollar IS a Currency but not all Currency will be Dollar (Currency can also be Yen, Peso, Rupee, etc.)
 
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mohammed Sardar. wrote:. . . Cow can be an Animal but Animal can not be a cow. . . . .

We have cows around here, too.

An animal might not be a cow.

A cow IS‑AN animal, and always is. An animal might be a cow, or a dog, or a whale, or a crocodile, or… If you have an array of animals, you can be sure that a cow will fit into that array. But a plain simple animal, that might be a frog or a lion, and you can't put frogs and lions into a cow array. The compiler will only allow you to put anything in the cow array if it is sure it really is a cow. Or if you lie to it with a (Cow) cast.
 
Piet Souris
Saloon Keeper
Posts: 3443
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Mohammed,

well, everthing is neatly explained now. But there is one snag with arrays in java: these things are, what is called, covariant. That means that the array Cow[] is a subclass of Animal[], because Cow is a subclass of Animal.
So, say we have an array Cow[10] cows. This is thus a subclass of Animal[], so it is allowed to write:

And since a Bear is an Animal, it seems like we can do this:

But this would mean that we add a Bear to our cow array!
Try whether this compiles, and if so, whether it will run. What do you expect?

This so called covariance is not the case when we ave generic classes. For instance, can we do this:

just like we did with the array above?

 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!