Which OO concept is “Base b = new Derived()” an ex

2019-04-05 16:21发布

I was passing a test and met a question in which we didn't find an agreement with my colleagues.

С++

  1 class Base {};
  2 class Derived : public Base {};
  3 class Foo
  4 {
  5 public:
  6     Foo()
  7     {
 -8-         Base* b = new Derived(); // Concept name is?
  9     }
 10 };

C#

  1 abstract class Base{}
  2 public class Derived : Base{}
  3
  4 public class Foo
  5 {
  6    public Foo
  7    {
 -8-        Base b = new Derived(); // Concept name is?
  9    }
 10 }

The question is: line number 8 above is an example of the following oo concept

  1. Polymorphism
  2. Aggregation
  3. Encapsulation
  4. Abstraction
  5. Inheritance

Please put a link with the answer to the source of knowledge.

P.S. The test is 'OO Concept' on breinbench. It is free.

Update:

From one of the answer which defends polymorphism

"In simple terms, polymorphism is the ability of one type, A, to appear as and be used like another type, B. In strongly typed languages, this usually means that type A somehow derives from type B, or type A implements an interface that represents type B."

From wikipedia which defends inheritance

Inheritance is also sometimes called generalization, because the is-a relationships represent a hierarchy between classes of objects.

and

Inheritance therefore has another view, a dual, called polymorphism, which describes many pieces of code being controlled by shared control code.

so Base = new Derived() shows 'is a'(inheritance). And consequence of this is polymorphism.

So I doubt what is right?

标签: c# c++ oop
10条回答
狗以群分
2楼-- · 2019-04-05 17:07

This question is easy... It is Polymorphism.

The Polymorphic behavior is accomplished because of inheritance. You can treat the instance of Derived as Base because Derived inherits from Base. This is the definition of Polymorphism when applied to types in an OO language...

http://en.wikipedia.org/wiki/Polymorphism_(computer_science)

[Update, Update]

I hope this is definitive enough... these are all different ways of saying the same thing.

Polymorphism (C# Programming Guide)

"Through inheritance, a class can be used as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces. This is called polymorphism."

[Update]

Given the comments I will try to be more specific... I am not saying that because Derived inherits from Base that the line is an example of Polymorphic behavior, I am saying that the assignment of an instance to a variable of a type that it derives from is an example of Polymorphic behavior. To quote the first sentence of the link I attached...

"In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface"

Meaning that I can treat an instance of Derived as an instance of Base is exhibiting a Polymorphic behavior. This doesn't depend on the existence of virtual methods on the class to be true.

and another quote from a different source...

"In simple terms, polymorphism is the ability of one type, A, to appear as and be used like another type, B. In strongly typed languages, this usually means that type A somehow derives from type B, or type A implements an interface that represents type B."

查看更多
可以哭但决不认输i
3楼-- · 2019-04-05 17:08

I'd have said Polymorphism, because the types are assignable to each other and can be treated as if they are the same. You are using the parent interface to handle the instance of the subtype.

Inheritance is more to do with inheriting members and/or member implementations from parent to child (aka base to derived, super to sub, abstract to concrete) class.

I see resolution of virtual functions to a concrete implementation as a feature of polymorphism so would not be put off by the absence of virtual function resolution in the example.

查看更多
smile是对你的礼貌
4楼-- · 2019-04-05 17:13
6. None of the above.  

It's an example of covariance.
http://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)
See also, Liskov subtitutability principle:
http://en.wikipedia.org/wiki/Liskov_substitution_principle

查看更多
来,给爷笑一个
5楼-- · 2019-04-05 17:20

I think the simple code here is best described as an example of subtyping (http://en.wikipedia.org/wiki/Subtype). In object-oriented programming terminology, polymorphism refers to the ability of subclasses to change or specialize the behaviour defined in the base class (http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming). This example doesn't really show that.

Of the choices listed on brainbench, I guess inheritance makes the most sense simply because its the most vague.

查看更多
登录 后发表回答