Polymorphism and inheritance?

Polymorphism allows type B to be treated as type A A be = new B(); int x = b.Foo(); // x will be 1 if virtual, 5 if not.

Polymorphism allows type B to be treated as type A. A be = new B(); int x = b.Foo(); // x will be 1 if virtual, 5 if not.

Assuming this is C#: You didn't override Foo. You hid the Foo of the baseclass. This means that if you call foo an a variable with the static type B you will get B.Foo() and calling on the static type A (even if the type is B at runtime) will give you A.Foo().

Your code should give a compiler warning, since you're hiding the base method without using the new keyword. B be = new B(); int x = b.Foo();//calls B. Foo A a = b;//Runtime type B, compiletime type A a.Foo(); // calls A.

Foo If you had overridden Foo then you'd get B.Foo() in both cases.

Your declaring type is B thus you will call the implementation of B.. if your declaring type is A and you are instantiating new instance of B then you get 5 as the declaring type unless you use virtual keyword to support polymorphysm ..

Marking a method as virtual allows it to be polymorphic. That means that it can be overridden in derived classes. A polymorphic method comes into play when you call it from a base class reference.

The method that's going to get called at runtime depends on the runtime class of the object pointed by the base class reference. If its runtime type is the base class itself, the method called will be the one on the base class; if the runtime type is of a derived type that has overridden that method, the method called will be that overridden method. If the method is not virtual, it will be bound to call the method that's declared in the type of the compile time reference to the object, regardless of its runtime type.

The point of making the method virtual is when you call the method via a base class pointer or reference: A p = new B(); int x = p.Foo(); In this example, the static type of p is A, but the dynamic type is B. This means that, due to polymorphism, B. Foo is called although the static type is A.

Do you mean something like ((A)b).Foo(); – Srcee Oct 23 at 15:09 The question is about Java. P->Foo() doesn't exits in Java. – Luchian Grigore Oct 23 at 15:11 Oops, sorry - I was thinking C++!

– Dabbler Oct 23 at 15:12 Also A* p = new B; isn't legal in Java... A p = new B();, with parenthesis, is different. – Luchian Grigore Oct 23 at 15:13 1 +1: even though your example was in C++, it showed the right concepts. I've updated it to match the question's language.

– Jordão Oct 23 at 16:53.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions