473,396 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

where did all the polymorphism go???

Can someone please explain the motivation behind the following
restriction in the language:

I define an interface and two classes implementing it:

public interface InterA {}

public class B implements InterA {}

public class C implements InterA {}

I then define an interface that has the method foo that takes an
instance of a class implemeting the InterA interface.

public interface Inter {
public void foo(InterA x);
}

Since classes B and C are the only classes implementing InterA, I'm
trying to define D to implement Inter by providing the following
methods:

public class D implements Inter {
public void foo(B x) {}
public void foo(C x) {}
}

But the language won't let me do this. It requires me to implement a
method with the signature foo(InterA x). The problem is that it
doesn't let me specify a different behaviour for foo() based on the
types that implement InterA. So it seems that I lose polymorphism
here. It shouldn't be difficult for a compiler to make sure that D
failthfully implements Inter by implemeing the methods, subclasses,
and subinterfaces specified in Inter for any class that implements
Inter. Therefore I can't understand why the language should place this
restriction.

Thanks,

Mayer
Jul 17 '05 #1
3 2111
hi
i think you can write your class D as

public class D implements Inter {
public void foo(InterA x)
{
check if x isInstanceOf class B
{
class B specific code
}
else check if x isInstanceOf class C
{
class C specific code
}
}
}

regards
amey
gm****@cs.bgu.ac.il (Mayer Goldberg) wrote in message news:<19**************************@posting.google. com>...
Can someone please explain the motivation behind the following
restriction in the language:

I define an interface and two classes implementing it:

public interface InterA {}

public class B implements InterA {}

public class C implements InterA {}

I then define an interface that has the method foo that takes an
instance of a class implemeting the InterA interface.

public interface Inter {
public void foo(InterA x);
}

Since classes B and C are the only classes implementing InterA, I'm
trying to define D to implement Inter by providing the following
methods:

public class D implements Inter {
public void foo(B x) {}
public void foo(C x) {}
}

But the language won't let me do this. It requires me to implement a
method with the signature foo(InterA x). The problem is that it
doesn't let me specify a different behaviour for foo() based on the
types that implement InterA. So it seems that I lose polymorphism
here. It shouldn't be difficult for a compiler to make sure that D
failthfully implements Inter by implemeing the methods, subclasses,
and subinterfaces specified in Inter for any class that implements
Inter. Therefore I can't understand why the language should place this
restriction.

Thanks,

Mayer

Jul 17 '05 #2
gm****@cs.bgu.ac.il (Mayer Goldberg) wrote in message news:<19**************************@posting.google. com>...
[snipped...]
But the language won't let me do this. It requires me to implement a
method with the signature foo(InterA x). The problem is that it
doesn't let me specify a different behaviour for foo() based on the
types that implement InterA. So it seems that I lose polymorphism
here.
You don't loose polymorphism, because strictly speaking that isn't
what polymorphism is about. One way to think about polymorphism is
as a way if defining different behaviours, accessible through a
standard interface - in the case of Java interfaces, the interface
defines *what* will be implemented, and the concrete classes which
use that interface defines *how* it will be implemented. (A simpl-
istic view, yes - but one which will do for the purposes of answering
your question.)

You want overloaded copies of the foo() method because you want to
have different behaviour depending upon what type of class gets
passed in - but these differences in behaviour should be encapsulated
inside each implementing class, not on the outside in code which
works with the class.

If we adopted your scheme we would loose 'scalability', because the
'accepting' method would have to have overloaded versions for every
implementation of an interface or subclass. Example: LayoutManager(2)
is an interface in AWT/Swing, which allows a class to be associated
with a container for the purposes of child layout. There are many
different types of LayoutManager - imagine if the Container class had
to have overloaded methods for every single one? Worse still - how
could we ever create our own layout manager, without modifying the
source code for Container, adding a method to accept it?

It shouldn't be difficult for a compiler to make sure that D
failthfully implements Inter by implemeing the methods, subclasses,
and subinterfaces specified in Inter for any class that implements
Inter. Therefore I can't understand why the language should place this
restriction.


As explained in the layout manager example above, the beauty of poly-
morphism is that we can collapse many different types of functionality
down to just one type, via means of a recognised signature/interface.
The reason AWT containers can accept any type of layout manager, and
indeed any type of component as a child, is because they adhere to
polymorphism: the differences between implementations are entirely
encapsulated inside each implementation, and no implementation-specific
code exists outside.

If you really have to have code 'outside' which treats different
implementations apart, you should either use 'instanceof', or define
overloaded methods which explicitly accept instances of individual
implementing classes (but then you loose the magic of polymorphism,
which is the ability to substitute any given implementation, providing
it adheres to the given interface/sig.)
-FISH- ><>
Jul 17 '05 #3
"Mayer Goldberg" <gm****@cs.bgu.ac.il> wrote in message
news:19**************************@posting.google.c om...
Can someone please explain the motivation behind the following
restriction in the language:

I define an interface and two classes implementing it:

public interface InterA {}

public class B implements InterA {}

public class C implements InterA {}

I then define an interface that has the method foo that takes an
instance of a class implemeting the InterA interface.

public interface Inter {
public void foo(InterA x);
}

Since classes B and C are the only classes implementing InterA, I'm
trying to define D to implement Inter by providing the following
methods:

public class D implements Inter {
public void foo(B x) {}
public void foo(C x) {}
}

But the language won't let me do this. It requires me to implement a
method with the signature foo(InterA x). The problem is that it
doesn't let me specify a different behaviour for foo() based on the
types that implement InterA. So it seems that I lose polymorphism
here. It shouldn't be difficult for a compiler to make sure that D
failthfully implements Inter by implemeing the methods, subclasses,
and subinterfaces specified in Inter for any class that implements
Inter. Therefore I can't understand why the language should place this
restriction.

Thanks,

Mayer

You've gotten some good answers, so I'll just add that this question would
be better discussed in comp.lang.java.programmer where more people could see
it. This is not an "official" newsgroup, and not all servers carry it.
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
35
by: JKop | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en- us/vccore98/HTML/_core_using_strict_type_checking.asp Pay particular attention to: The types WPARAM, LPARAM, LRESULT, and void *...
3
by: E. Robert Tisdale | last post by:
polymorph just means "many form(s)". The definition in plain English http://www.bartleby.com/61/66/P0426600.html and narrower definitions in the context of computer programming ...
11
by: richard pickworth | last post by:
Can anyone explain polymorphism?(very simply). thanks richard
4
by: LP | last post by:
Hi, I understand the concept/definition of polymorphism. But what does the term "runtime polymorphism" mean? I was asked to define it during a technical interview. I gave a guy vanilla definition...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
2
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt...
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.