?
Solved

Polymorphism Problem

Posted on 2006-04-14
10
Medium Priority
?
357 Views
Last Modified: 2013-11-25
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.
0
Comment
Question by:qugle
  • 4
  • 3
  • 3
10 Comments
 
LVL 11

Expert Comment

by:WelkinMaze
ID: 16451990
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
 
LVL 9

Expert Comment

by:gabeso
ID: 16452115
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
 

Author Comment

by:qugle
ID: 16453326
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 9

Expert Comment

by:gabeso
ID: 16454102
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
 

Author Comment

by:qugle
ID: 16485869
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
 
LVL 11

Accepted Solution

by:
WelkinMaze earned 150 total points
ID: 16485936
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
 
LVL 11

Expert Comment

by:WelkinMaze
ID: 16485943
sorry
read "forst" as "forced"
0
 
LVL 9

Expert Comment

by:gabeso
ID: 16485972
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
 

Author Comment

by:qugle
ID: 16513473
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
 

Author Comment

by:qugle
ID: 16644825
Thanks a lot. I had an answer.
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
No other job is as rewarding and demanding as building an iPhone app is. It is not really in the hands of the developer for the success of an iPhone app. Many factors operate jointly for every iOS application's success in the market.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Progress

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question