wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Polymorphism" Watch "Polymorphism" New topic
Author

Polymorphism

Smita Chopra
Ranch Hand

Joined: Jun 27, 2005
Posts: 45
I have read that polymorphism is about overriding but is it about overloading as well.
Thanks
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Polymorphism is a concept of OOPS.

When you apply the concept of polymorphism in same class, Java calls it Overloading.
When you apply the concept of polymorphism in inheritance, Java calls it Overriding.


My blood is tested +ve for Java.
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
I was always thinking that Polymorphism is about Overriding and never include Overloading in Polymorphism. However, I saw a lot of diffinition of Polymorphism and in all cases there was nothing about Orevloading.

Chetan Parekh, can you name you source which says that Overloading is a polymorphism in same class?


SCJP 1.4 (100%) Done.<br />SCJD (URLyBird 1.2.3 Started)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
See this recent discussion: http://www.coderanch.com/t/400110/java/java/Compile-time-Runtime-polymorphism


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
Ilja, thanks. It seems much clear now about "what should I call Polymorphism".

Let me ask you one more question about OO paradigms.

Is this code breaking Encapsulation paradigm? (One object is accessing private member of another object.)

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Java and C++ have class-based encapsulation. Some OO languages (Ruby, if I recall, and perhaps Python?) have object-based encapsulation. I personally don't think one is "better" than the other; class-based encapsulation certainly makes it easy to implement operations like clone() and equals() that are harder to do in an object-encapsulation language, although object-based encapsulation is perhaps more intuitive.


[Jess in Action][AskingGoodQuestions]
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
Ernest Friedman-Hill, Thank you.


class-based encapsulation certainly makes it easy to implement operations like clone() and equals()...


I've read about "object can access private member of another object of the same class" during my SCJP 1.4 preparation in chapter about equals() overriding.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by George Bolyuba:
Is this code breaking Encapsulation paradigm?


Only minimally, in my view.

First, you are probably referring to what I would call information hiding. Encapsulation, in my understanding, refers to putting things together that are closely coupled, such as data and the operations on that data. Information hiding is about hiding internal details from the outside.

Second, we need to take a look at *why* we want information hiding. The main reason for hiding fields is to manage source code dependencies - for example, we want to minimize the number of places we have to touch should we need to change something (because we well have to regularly). In your example, when "i" needs to be changed, we still don't have to touch any other classes but A, so change impact is already quite localized.

Of course by using a getter in even this case, we could minimize the impact of some changes, such as the introduction of lazy initialization for "i" even more. On the other hand, information hiding (as well as all the other OO principles such as encapsulation, decoupling etc.) isn't something that you want to do "100%" - it needs to be balanced against all the other needs of the code, such as functionality, simplicity etc.

After all, a class that conforms 100% to information hiding and/or decoupling would be useless, as it couldn't provide an interface to its clients...

Does that help?
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162

Does that help?


Yes. It seems clear and helpfull. Thanks.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Smita Chopra:
I have read that polymorphism is about overriding but is it about overloading as well.
Thanks


TRUE!

So did you have an actual question, or did you visit to just tell us this fact?


Java API Documentation
The Java Tutorial
Smita Chopra
Ranch Hand

Joined: Jun 27, 2005
Posts: 45
Originally posted by Layne Lund:


TRUE!

So did you have an actual question, or did you visit to just tell us this fact?


Head First Java says that overloading has nothing to do with polymorphism while elsewhere I have read (can't recall the resource right now) both overloading and overriding are examples of polyporphism, hence the confusion.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Overloading has nothing to do with polymorphism. The example you gave does violate encapsulation and will bring along with it all the negative effects of that violation. Most promanently, you can't change the inside of one class without it affecting the behavior of another class.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by CL Gilbert:
Overloading has nothing to do with polymorphism.


Yes, it has. The more generic definition of polymorphism includes method overloading as a form of "compile time polymorphism". Check the thread I referenced above.

The example you gave does violate encapsulation and will bring along with it all the negative effects of that violation. Most promanently, you can't change the inside of one class without it affecting the behavior of another class.


I don't see how his example has this property, as the field is still private and therefore can only be used inside the declaring class.
Smita Chopra
Ranch Hand

Joined: Jun 27, 2005
Posts: 45
Page 187, Head First Java

There is no polymorphism involved with overloaded methods.

Is the book incorrect when it says, no polymorphism with overloaded methods?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Smita Chopra:
Is the book incorrect when it says, no polymorphism with overloaded methods?


Strictly speaking, I would say it is, yes.

I don't have the book at hand, though. Perhaps earlier in the book there is some definition of the term that clarifies what the authors mean when they use it?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Polymorphism
 
Similar Threads
Method doesn't recognize array
polymorphism
Difference between Factory method pattern and dynamic polymorphism
PolyMorphisn
Any idea about questions for interview.