Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes polymorphism by using  interface and inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "polymorphism by using  interface and inheritance" Watch "polymorphism by using  interface and inheritance" New topic
Author

polymorphism by using interface and inheritance

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


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

Joined: Feb 22, 2009
Posts: 192


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

Joined: Aug 31, 2004
Posts: 11343

See "How my dog learned polymorphism."


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


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

Joined: Nov 18, 2008
Posts: 433
    
    2
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

Joined: Apr 06, 2010
Posts: 4388
    
    8

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

Joined: Sep 06, 2007
Posts: 635


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

Joined: Sep 06, 2007
Posts: 635
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

Joined: Apr 06, 2010
Posts: 4388
    
    8

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

Joined: Apr 06, 2010
Posts: 4388
    
    8

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
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
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

Joined: Sep 06, 2007
Posts: 635
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
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
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

Joined: Sep 06, 2007
Posts: 635



b is eating
b is eating


Why are these same?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
abalfazl hossein wrote:


b is eating
b is eating


Why are these same?
Polymorphism. It's still a "b".
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635



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
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
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
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

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

Joined: Sep 06, 2007
Posts: 635


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
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
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

Joined: Sep 06, 2007
Posts: 635


It shows polymorphism too. Right?

If you compare this code to another:http://www.coderanch.com/forums/posts/list/531088#2408422
Which one is better?Cast or create new instance?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
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:http://www.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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: polymorphism by using interface and inheritance