• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

polymorphism by using interface and inheritance

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Polymorphism is forms of one type.Is this polymorphism because of this?


How can you see polymorphism in this code:


From SCJP study guide by Richard F. Raposa
 
Ryan Beckett
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I see inheritance, not polymorphism.



That is polymorphism. eat() has many different forms since it's overridden by whoever extends Pet. When referencing each instance with its super type, the correct override is chosen based on the instance type at run time.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See "How my dog learned polymorphism."
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


pet is an instance of a Cat,Because of new Cat,Then Why does it need to cast?
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:

pet is an instance of a Cat,Because of new Cat,Then Why does it need to cast?



At compile time all the compiler knows is it is a Pet. A Pet does not know how to sleep, the method is specific to Cats. (Please don't quote this without context ;-)).
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:pet is an instance of a Cat,Because of new Cat,Then Why does it need to cast?


It's the other way around, really: if pet wasn't an instance of Cat, you wouldn't be able to cast.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


These returns true, Then again: Why does it need to cast?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible one object has two kinds?! Because in that code pet has two kinds! Again: Why does it need cast?

 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:These returns true, Then again: Why does it need to cast?


Casting is relevant when you have a variable with one reference type, which is referring to an object which is actually an instance of a subclass. For example:
All the compiler knows about animal is that is referencing an Animal. So as long as we only need to use features of an Animal, this is fine.
But what if we want to use a feature specific to Cat? The compiler doesn't know that it's a Cat. So it can't allow us to do this:
In this case, we need to tell the compiler "trust me - I know the reference type says Animal, but it's definitely containing a Cat". Normally the compiler protects us from accidentally treating a Dog like a Cat, so we need to override it. That's what casting does:
Then, if we're wrong, the run-time will throw a ClassCastException.

So, casting is needed when we need to tell the compiler at compile time that something will be true that it can't check until run time.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:Is it possible one object has two kinds?! Because in that code pet has two kinds!


Yes, because that's what inheritance means. A Cat is an Animal. If something is a Cat, it must also be an Animal. But the other way around is not necessarily true, and that's why casting is sometimes necessary.
 
Campbell Ritchie
Marshal
Posts: 80653
476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:Is it possible one object has two kinds?! Because in that code pet has two kinds! Again: Why does it need cast?. . . .

By "two", I presume you mean three.

Matthew Brown wrote:A Cat is an Animal. If something is a Cat, it must also be an Animal.

That makes two.


But we are writing in Java™, so a Cat is an Animal and an Animal is an Object. That makes three


And some objects have more types; for example an instance of this class has 7 types.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:
view plaincopy to clipboardprint?
Pet pet = new Cat(“Alley”, 7);
pet.eat(); //no cast needed
((Cat) pet).sleep(); //cast is needed


pet is an instance of a Cat,Because of new Cat,Then Why does it need to cast?


At compile time all the compiler knows is it is a Pet. A Pet does not know how to sleep, the method is specific to Cats. (Please don't quote this without context ;-)).



َand in Runtime, Pet is a Cat?
 
Campbell Ritchie
Marshal
Posts: 80653
476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is type extension and there is functional extension and I can never remember which is which. But one means adding new methods/behaviour in subclasses, and the other means not adding new methods.
Whichever way it is you are adding new methods, it means that only certain subclasses have certain behaviour. For example, java.util.ArrayList has three methods which are not in the List interface: trimToSize, ensureCapacity and removeRange. If you need those, you would have to castSo when you want 10000000 numbers, you get a List large enough to hold them. Those methods (trimToSize and ensureCapacity) are not in LinkedList, because they are not necessary there.

The instanceof operator tests whether you have an ArrayList or one of its subclasses, so the cast can go ahead safely. I think that is an awkward programming construct, however, and it should be used only rarely. And it will be awkward if you find another type of List which also has an ensureCapacity() method or a trimToSize() method.
Another awkward construct there is the use of an output parameter; the List is passed in order to have its state changed. If you didn't cast the Pet object to (Cat)tiddles or similar before invoking the sleep() method, the compiler would only know it is a Pet and would not know there is an accessible sleep() method, so it would never compile. And I am surprised only Cat has a sleep() method; have you never seen a dog asleep?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



b is eating
b is eating



Why are these same?
 
Campbell Ritchie
Marshal
Posts: 80653
476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:


b is eating
b is eating



Why are these same?

Polymorphism. It's still a "b".
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



When we use inheritance, Super class and Subclass has same type.Then cast must working here.It must print:

a is eating


Because Of type cast, Why it doesn't?
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:

It must print:

a is eating


Because Of type cast, Why it doesn't?



Because that's not how it works. It's a "b" object, the overridden method is always called.

Henry
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome back Sir!
When we use inheritance, Super class and Subclass has same type

((a)test).eat();

By this order, test became a.

a.eat must print a is eating
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:
By this order, test became a.

a.eat must print a is eating



Casting doesn't change the object. It only (may) changes how the compiler generates code to call it. It's a "b" object, the overridden method is always called.

Henry
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Exception in thread "main" java.lang.ClassCastException: inheritancetest.a cannot be cast to inheritancetest.b

Subclass and superclass has same type.But it is not possible to cast subclass to super class, Because an a object may be a "b" object, But not necessarily.Right?


 
Campbell Ritchie
Marshal
Posts: 80653
476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote: . . . But it is not possible to cast subclass to super class, . . ..Right?

No.

It is always possible to cast from subclass to superclass; it is often not possible to cast from superclass to subclass.
And the casts vanish at runtime. Try this codeCompile that code and print the bytecode with javap -c AnimalDemo and you will see no sign of the cast. So the Cat object is still a Cat object.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It shows polymorphism too. Right?

If you compare this code to another:https://coderanch.com/forums/posts/list/531088#2408422
Which one is better?Cast or create new instance?
 
Campbell Ritchie
Marshal
Posts: 80653
476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:

Does it really? I don't think it does. Have you tried it?

It shows polymorphism too. Right?

Yes, but . . .

If you compare this code to another:https://coderanch.com/forums/posts/list/531088#2408422
Which one is better?Cast or create new instancne?

What you have shown is very different from Ryan Beckett's code you quoted earlier. You should create as many objects as you need, and you should avoid casting as much as possible.

And please post code which compiles; your "smart" quotes won't compile. Use a text editor, not a word processor.
 
reply
    Bookmark Topic Watch Topic
  • New Topic