• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question related Objects assignment  RSS feed

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not understand properly the following code:

Animal animal = new Camel();

I would kindly ask you to explain this code and to clear my doubts.
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What don't you understand?  I'm assuming that Camel extends Animal.
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.


There is an other code:

What is the difference?

Also I do not understand the following code:
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urs Waefler wrote:. . . I do not understand the following code:
Nor does the javac tool. It will think maybe an Animal is a Dog, Cat or Lion rather than a Camel. Try that code and see whether it will even compile.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps the Oracle Tutorial on Inheritence is a good place to start -- as it might be better to have an understanding on the concept first, before trying to understand why certain assignments are allowed, and others not.

Henry
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This code does not compile. I need further explanations, I need the proper understanding.

I know the Oracle Tutorial on Inheritance. I am a SCJP 1.4. It's a long time back, I need to renew the knowledge, because I am interested in the Upgrade to Java SE 8 OCP (Java SE 6 and all prior versions). Please help me with this matter.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the Camel class subclasses from the Animal class, then instances of the Camel class IS-A Animal type... but ... instances of the Animal class is not IS-A Camel type.

I don't know how to elaborate any more than that, as this is a basic concept / definition / postulate (going all the way back to Java 1.0).... are you sure you don't want to review the tutorial?

Henry
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that helps. Still there is the following code that I do not understand properly:


It seems that it does not make any sense. I would just prefer to write:
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that's fine if all you want is a Camel object.  But what if you wanted Camels and Dogs and Cats and Bears?  If they all extend Animal, then you have a common interface that works with all of them.  Maybe it's a speak() method.  So you could iterate through a List of Animals and have them all speak, but what you'd get is different sounds if each animal overrides the speak method.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
https://www.tutorialspoint.com/java/java_inheritance.htm

This is another good place to read about inheritance, they actually have a section in this tutorial that goes over the Animal super class and the sub classes that extend to it.
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably I asked the wrong question. It seems to be a theme of polymorphisme.

Animal animal = new Camel();

animal is a reference of the type Animal to an object of the class Camel. I am not sure about the use of this object; I think you can apply the methods of the class Animal only.
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urs Waefler wrote:Probably I asked the wrong question. It seems to be a theme of polymorphisme.

Animal animal = new Camel();

animal is a reference of the type Animal to an object of the class Camel. I am not sure about the use of this object; I think you can apply the methods of the class Animal only.

That's correct. You want to treat the Camel as the simple Animal that it is

It might make more sense if you put it in the context of a method. The ride() method is common to all Animal objects in the code below:

Sorry for so many edits. Serves me right posting code that I didn't try to compile and run first.
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for this explanations. I think, there is one small mistake in your code; there I can not see a method getName() implemented. I think it should be getSpeed().

I do not understand the following code:



Is it an anonymous inner class? I would kindly ask you to explain this code.

Now I understand the initial code:

What is if I want to have it the other way around? I think there it needs a cast:



How do you explain this code?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urs Waefler wrote:. . . How do you explain this code?
You are taking a generic animal object and telling the compiler it is a camel. When you attempt to execute that cast at runtime, the runtime wil see that you don't have a camel object at all and will throw an exception.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!