Is there a difference in how polymorphism is applied in Java and C# ?

Asked

Viewed 279 times

4

I’m studying the concepts of POO and generated me a doubt.

In Java I believe I can access all methods and attributes declared as public in a subclass even when using a superclass type variable to reference this subclass.

However, in C# I cannot access the subclass methods when assigning your reference to a super class?

Is there such a difference between languages? If so, why, if the concept of POO theoretically is the same for any programming language?

Example:

ContaPoupanca poupanca = new ContaPoupanca();
poupanca.CalculaInvestimento();

Conta conta = new ContaPoupanca();
conta.CalculaInvestimento();  // Não consigo acessar esse método através da variável conta.
  • 4

    If the method is unique to the subtype, will not work even in java.

  • This has nothing to do with the fact that every private Java method is equivalent to the virtual C method#?

  • @Jeffersonquesado ?!?!?!!?! :)

  • @bigown, I may be getting confused... but come on; in the section 10.5.3 the documentation speaks of The implementation of a non-virtual method is invariant (like, don’t look vtable), whereas In Contrast, the implementation of a virtual method can be superseded by derived classes. (like, look vtable); in Java, every private method suffers this superseded mentioned, even the bytecode to call the nonprivate instance method has as mnemonic invokeVirtual, saying that will always check the vtable.

  • 2

    This is not the case because if it is private, there is no reason to look elsewhere. But I think I now understand what you mean. In Java every public method is virtual by default, and in C# is not virtual by default. There it is. But in essence it does not change the behavior, only the default is another.

2 answers

7

The answer from LINQ already said that it does not give, but it has solution. I’ll guess more or less how would be the class that needs:

using static System.Console;
using System;

public class Program {
    public static void Main() {
        ContaPoupanca poupanca = new ContaPoupanca();
        poupanca.CalculaInvestimento();
        Conta conta = new ContaPoupanca();
        conta.CalculaInvestimento();
    }
}

public class Conta {
    public virtual void CalculaInvestimento() { throw new NotImplementedException(); }
}
public class ContaPoupanca : Conta {
    public override void CalculaInvestimento() { WriteLine("ok"); }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

If that’s what you need, it’s another story.

  • This would be the equivalent of the Abstract method in java or nothing to do?

  • No, if the method were abstract the class should also be, then it could not be instantiated. In fact I’ll even improve the code, so my internet that is horrible leave.

5


In java I believe I can access all methods and attributes declared as public in a subclass even when using a superclass type variable to reference this subclass.

No, you can’t.

It makes no sense to access members of a subtype in a higher type. In either language this will work.

This actually has nothing to do with polymorphism. It would be interesting to read the publication below

  • I swore that I had accessed the subclass method using a super class type. I will take the test again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.