Polymorphism Problem

I studied about Object-Oriented Programming. I have a questions about Polymorphism:
1. Why Java don't have Polymorphism.
2. Give me an example about Polymorphism in C++. Then you can convert this code in C++ to code in Java.

Thanks a lot.
qugleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WelkinMazeCommented:
1. Java does have Polymorphism! It has not multiple inheritance.
2. here is one in Java. It's the same in C++. Just the syntax is a little bit different.

class A {
  public String cutString(){
    return something...
  }
}

class B extends A{
  public String cutString(){
    return something2....
  }
}

A temp = new B();
temp.cutString();

Although the temp variable is of type A, the cutString method of class B will be executed here.
0
gabesoSolution ArchitectCommented:
One way to think about polymorphism is that you can have a variable of one type in some highly general code and then point it at various objects with types that are derived from that base type.

The common example is BankAccount. If I have an account processor it can be given any class that inherits from BankAccount and it will work as long as the account processor only uses the BankAccount interface of the object. Account processor is polymorphic because it is working with 'many forms' all sharing a common interface.
0
qugleAuthor Commented:
Sorry, I have a bad English.
I agree: Java does have Polymorphism! It has not multiple inheritance.
Please give me an example that code in C++ has multiple inheritance. Then you convert it to code in Java.

Thank
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

gabesoSolution ArchitectCommented:
Java does not have multiple inheritance: C++ does - but an example of this in C++ will not translate into Java directly.

What you can do is use single inheritence and 'composition' where the functionality you would get from multiple inheritence is gained by using other objects rather than inheriting it.

I don't think there is any problem solved with multiple inheritance that cannot be solved using single inheritance and composition.
0
qugleAuthor Commented:
I don't understand why Java don't support multiple class inheritance but support multiple interface inheritance.
I understand that multiple class inheritance causes problems.
But Why multiple interface inheritance doesn't cause problems? I don't understand.
I think you should give me a clear example to explain above questions.

I have a following example:

public interface Dialog
{
     public void Display();
}

public class ModelessDlg implements Dialog
{
    boolean Modeless;
    public void Display(){ .... }
}

public class SplashScreenDlg implements Dialog
{
    boolean SplashScreen;
    public void Display(){ .... }
}

public class ResizableDlg implements Dialog
{
    boolean Resizable;
    public void Display(){ .... }
}

I want to create a class ModelessResizableDlg inherit from class ModelessDlg , SplashScreenDlg and ResizableDlg. But Java not allow. In this case, how to solve this problem? You can change this code to make a good design in your idea.

If you explain very good, I will increase to 250 points.
0
WelkinMazeCommented:
See these links:
http://www.javaworld.com/javaqa/2002-07/02-qa-0719-multinheritance.html
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html

The first one answers why there is not multiple inheritance in Java.

And here is the idea with interfaces:
"Java's designers chose to allow multiple interface inheritance through the use of interfaces, an idea borrowed from Objective C's protocols. Multiple interface inheritance allows an object to inherit many different method signatures with the caveat that the inheriting object must implement those inherited methods.

Multiple interface inheritance still allows an object to inherit methods and to behave polymorphically on those methods."

So, the idea is that with interfaces you may force your class to behave as several different classes.
You can typecast your class to any of the inherited interfaces and since you are forst to implement their methods it will behave as if it is of the type of the interface that it is casted to.

So the idea is that you save the problems of multiple inheritance in C++ but still have the possibility (through interfaces usage) to allow one object to behave in different ways.

Hope, you will understand it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WelkinMazeCommented:
sorry
read "forst" as "forced"
0
gabesoSolution ArchitectCommented:
Small examples can't really show the sort of problem that can occur in practice with multiple inheritence but the idea is this:

If you have a tree of class relationships and you only have single inheritence then for any leaf-node class at the end its methods are either implemented by itself or an ancestor so the search for an implementation is simple: look at the current class for an implementation, if not found then goto parent class and repeat this until method is found (or error!).

If you have multiple inheritence and multiple ancestors implement the same interfaces (methods) then how do you know which implementation to use (in the general case) - suddenly you don't have a simple search method anymore.

So what rules would you use to find the method in a graph (not a tree anymore) of multiple inheritence relationships?
0
qugleAuthor Commented:
I think you should give me an example about multiple class inheritance in C++ cause problems and solve it in Java by using multiple interface inheritance.
0
qugleAuthor Commented:
Thanks a lot. I had an answer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.