tags:

views:

67

answers:

5

For example, if you have class A, class B inheriting A, and class C inheriting B, is there any programming language in which class C can override a method of class A, even if class B don't override it?

class A {
  method() {}
}

class B extends A{
}

class C extends B {
  //override method from A
  method(){}
}
+1  A: 

yes , It is very general case, Java does it.

org.life.java
+3  A: 

AFAIK you can do this in most (if not all) OO languages, e.g. in Java and C++ for sure.

Péter Török
A: 

This ruby code does exactly what you want:

class A
  def hello
    puts "hello from class A"
  end
end

class B < A
end

class C < B
  def hello
    puts "hello from C"
  end
end

B.new.hello
C.new.hello

Once executed you will have the following output:

hello from class A
hello from C
Flavio
A: 

C# for one

public class A
{
    virtual public int retval(int x)
    {
        return x; 
    }
}
public class B : A
{

}
public class C : B
{
    public override int retval(int x)
    {
        return 3; 
    }
}

class Program
{

    static void Main(string[] args)
    {
        A a  = new C();
        Console.WriteLine(a.retval(2).ToString());

    }
}
rerun
A: 

I think most common languages will allow that without difficulty if all modules are recompiled. There is a gotcha in some (including C# and vb.net) if an override is added to a mid-level class which didn't have one when a child method was compiled. In that scenario, if the child classes are not recompiled, calls to their parent methods may bypass the mid-level classes (since those mid-level classes didn't have overrides when the child passes were compiled).

supercat