My problem is a little strange. I have a clas let say classA. In the classA i have a method with tha name MethodA. I have anaother class called classB that derived from classA. I want to override methodA of classA, but not with the name methodA, but with a different name, let say methodB. is there any way to do this ?
Thanks in advance.

Recommended Answers

All 8 Replies

I want to override methodA of classA, but not with the name methodA, but with a different name,

So why do you need to override it :)?? method with different implementation with different name!!!

Let me explain. I have class colUsers:Arraylist. in the array list is a method called Add. For safety type i want to override it. But for logical reason i dont want to call the method Add (is in english), but in another language for example in italin language : Aggungi. Was i clear ?!!

It's not override at all! it's wrapping classes with different names...

public class Parent
    {
        public void Add(object o) { //some code }

    }
    public class Child : Parent
    {
        public void Aggungi(object o)
        {
            base.Add(o);
        }
    }

*seems[text]

doesn't work!
[/text]

Thank you for the reply. I am sorry for a so stupid question, but i haved work before only in vb6, and know only a little theory about classes and behaviore.

there something wrong in what you have written. If I do how you say :
I will create an object of childClass, and when i call the method Add it will be called the method Add of the parent class. Instead i want , that the method add , not to be accessible from an object of childClass, or when it is called, the Aggungi method to be called aoutomatically. However , i think that my problem will be solved using generics or not ??

Ok so you want to hide the parent method Add and instead offer a method called Aggungi which does the same thing?

You can't really do that with object orientated programming languages. If a parent has a public method a child will have it too, it can provide an alternative implementation but it must be named the same.

You have 2 choices as I can see it:
1) create a common parent with protected method AddLanguageIndependent then create child classes that have public methods of appropriate names:

class Example
    {
        protected void AddLanguageIndependent()
        {

        }
    }

    class EnglishExample : Example
    {
        public void Add()
        {
            base.AddLanguageIndependent();
        }
    }
    class ForeignExample : Example
    {
        public void Aggungi()
        {
            base.AddLanguageIndependent();
        }
    }

2) Other option would be to not inherit at all and instead just make it a variable in the class:

class Example
    {
        public void Add()
        {

        }
    }

    class ForeignExample
    {
        Example e;
        public void Add()
        {
            e.Add();
        }
    }

edi843, You have lack in your logic design! how you need to achieve wrapping to class without accessing its methods?!

To explain a little more, i will have colUsers:ArrayList. ArrayList have a method Add, i have e method Aggungi that make the same thing like Add with some little differences. In my code i will use always Agungi that take like argument clsUser. In this manner i will be always type safe. But if someone else in my team uses the method Add of the ArrayList on an object of type colUsers, then i will no longer be type safe, because the parameter that take Add of ArrayList is Object.
My solution was that instead of ArrayList i derived from the analouge generic class of ArrayList, that is List<>, and i specified colUser : ....List<clsUser>. In this manner if someone use the add method, there will be e error in compilation phase, if the object that i pass to the method Add is not of clsUser type.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.