polymorphism

Why java doesn't allow to make an instance method of parent class as more restrictive in child class

Polymorphism allows the programmer either to inherit, override or to overload an instance method of Parent Class. But, it won't allow to make an instance method of parent class as more restrictive in child class. i.e it wont allow to use same name of parent class instance method, to declare as private in the child class. Also JVM iden...

Inherit a Static Variable in Java

I want to have the following setup: abstract class Parent { public static String ACONSTANT; // I'd use abstract here if it was allowed // Other stuff follows } class Child extends Parent { public static String ACONSTANT = "some value"; // etc } Is this possible in java? How? I'd rather not use instance variables/met...

How can this be implemented(elegantly) without using RTTI?

I was coding this up in C# and the quickest solution to come to mind used the "as" or "is" keywords. I began wondering how I could implement it neatly in C++(without RTTI)... or even in C# without the aforementioned keywords. Here is the problem (simplified): There is a class Command which contains a stream of so called "tokens". clas...

foreach(Derived obj in new List<Base>())

What does the following code do? class Base { } class Derived : Base { } class Test { void Foo(List<Base> list) { foreach (Derived obj in list) { // ... } } } I didn't expect it to even compile, but it does. ...

How to call child object's overloading function in polymorphism?

Consider the following simple polymorphism ... class Parent { public: someFunc() { /* implementation A */ }; }; class Child : public Parent { public: someFunc() { /* implementation B */ }; }; int main () { Parent* ptr; ptr = new Parent(); ptr->someFunc(); delete ptr; ptr = new Child(); ptr->someFunc()...

How to create generic method

Hi All, I want to create a method which will take some base type as a parameter and compares and check if it is derived type and return the derived type based on that. For Example: Class A : IBase Class B : IBase My method: Public IBase GetData(IBase type) { If type is A then { //do smthing return A } If type is B { ...

Determining a Type of an Object

Hello all, Again, I really hope this isn't a matter of opinion; I'm trying to know which is the best way to determine the type of an object that belongs to a certain hierarchy in C#. I have two ways to design my application: 1 - Use a property on the base class: public abstract class Parent { public abstract TypeOfObject TypeOfOb...

invocation of polymorphic like event

Considering the code below: public class TableMain { public virtual event Action UpdateFilter; .... } public class TableSub : TableMain { public override event Action UpdateFilter; public void UpdateQuery(){ ..... if(UpdateFilter!=null){ UpdateFilter(); // Invocation of polymorphic field-like event??? } } ...

Best way to implement ad-hoc polymorphism in Haskell?

I have a polymorphic function like: convert :: (Show a) => a -> String convert = " [label=" ++ (show a) ++ "]" But sometimes I want to pass it a Data.Map and do some more fancy key value conversion. I know I can't pattern match here because Data.Map is an abstract data type (according to this similar SO question), but I have been uns...

Can I override an overload of an operator and return a different type?

class A{ public: virtual char &operator[](int); protected: .. }; class B:A{ public: A* &operator[](int); protected: } Can I change the return type when I overload an overload of an operator? thanks! //EDIT Okay, so now that we established that this wont work how can I build a work around? Lets say I have classe...

Cannot use polymorphism because of generic base class

I create base generic class with no fields with just one method public class Base<T> where T:class { public static T Create() { // create T somehow } } public class Derived1 : Base<Derived1> { } public class Derived2 : Base<Derived2> { } public class Program { bool SomeFunction() { // Here I need reference to...

nhibernate polymorphism mapping

I have two entities that I need to treat polymorhically, each of which has a similar "business id" type of property. As you might expect, there is semantic meaning to each id in the domain, and there is an object type to represent it. In one entity, a Project, the domain language of this property is a ProjectCode. In the other entity, an...

How can I represent polymorphism in JMX?

I have some types like this: public interface Numbering { List<NumberingComponent> getComponents(); } public interface NumberingComponent { Object getValue(); } public interface StringNumberingComponent extends NumberingComponent { String getValue(); } public interface IntegerNumberingComponent extends NumberingComponent ...

Overriding a method contract in an extended interface that uses generics (Java)?

I am attempting to override a method declaration within an interface that extends another interface. Both of these interfaces use generics. According to the Java tutorials, this should be possible, but the example does not use generics. When I try to implement it, the compiler shows the following error (I've replaced names because some o...

What is the difference between forall a. [a] and [forall a. a]?

Title and tags should explain the question adequately. ...

Polymorphism in Python

class File(object): def __init__(self, filename): if os.path.isfile(filename): self.filename = filename self.file = open(filename, 'rb') self.__read() else: raise Exception('...') def __read(self): raise NotImplementedError('Abstract method') class FileA(Fi...

Question about polymorphism and overloading

I'm trying to understand the concepts of polymorphism and overloading. I have the following code as a sort of experiment. I cannot figure out, however, why this program does not run (it fails because of mobj.foo(str). mobj is defined using polymorphism, and from what I can gather, should be of type MyDerivedClass. If that were true thoug...

rspec validates_presence_of polymorphic attributes ruby on rails

I am writing an rspec for an address model that has a polymorphic attribute called :addressable. I am using factory girl for testing. This model has no controller because I do not wish to create a standalone address but that doesnt stop the rspec from creating a standalone address. My model, factory and rspec are class Address < Act...

Force base method call

Is there a construct in Java or C# that forces inheriting classes to call the base implementation? You can call super() or base() but is it possible to have it throw a compile-time error if it isn't called? That would be very convenient.. --edit-- I am mainly curious about overriding methods. ...

Java generics (template) specialization possible (overriding template types with specific types)

I'm wondering what are the options to specialize generic types in Java, i.e. in a templated class to have specific overrides for certain types. In my case I was a generic class (of type T) to return null usually, but return "" (the empty string), when T is the String type, or 0 (zero) when its the Integer type, etc. Merely providing a ...