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:00

The real answer is: a poorly formed and therefore meaningless question.

This is suppose to be a standardized multiple choice question yet you have people with many years of experience not coming to a consensus. The only conclusion that should be reached is that as a measurement of knowledge it is useless.

查看更多
Anthone
3楼-- · 2019-04-05 17:04

This snippet is about Inheritance

A rough summary of concepts

Abstraction is about the whole idea of modeling a real-world concept in terms of objects rather than thinking about function calls or other stuff. It's basically thinking about objects as separate entities.

Encapsulation is the act of hiding implementation of an object from the outside world behind well-defined interfaces.

Inheritance is the relationship between derived classes and base classes and categorization of concepts. It defines "is-a" relationship between too entities, adding the ability of using derived classes where a base is expected.

Polymorphism means two objects are similar in interface but behave in different ways (think about virtual methods).

Aggregation defines a "has-a" relationship between two concepts. Means an object is composed out of another entity.

查看更多
三岁会撩人
4楼-- · 2019-04-05 17:04

It isn't directly any of the choices given.

It isn't directly about polymorphism because the code isn't calling a virtual method through a base pointer or deleting a pointer to the base class.

It isn't directly about inheritance because the highlighted code doesn't do any inheritance.Line 2 is about inheritance.

It certainly isn't about abstraction, encapsulation or aggregation because, well, it's not anywhere near those things.

The concept is most directly illustrates is an automatic cast.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-04-05 17:05
  1. Polymorphism

    • no, because we don't make any calls of virtual methods - and don't use polymorphic behaviour on any other manner
  2. Aggregation

    • no, because because Base* b is not member
  3. Encapsulation

    • don't see what we have encapsulated.. except implementation of constructor
  4. Abstraction

    • I think - yes - we will use more abstract class then created
  5. Inheritance

    • relationship between base and derived is inheritance - but you asked about assignment line

EDIT

Abstraction is simplifying complex reality by modelling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem.

definition from wikipedia - http://en.wikipedia.org/wiki/Object_oriented

查看更多
The star\"
6楼-- · 2019-04-05 17:06

I think the diversity of the answers demonstrates that this is a poorly constructed question.

If you put a gun to my head, I would probably choose inheritance, since this models that since Derived inherits Base, then Derived can be used where a Base is required (such as being pointed to by a Base*) but I could imagine a defense of any of the answers.

If I were making a hiring decision, I'd be more interested in hearing how a candidate defended her chosen answer than in which one she chose. But a company using a test like this probably isn't, or doesn't have anyone on staff capable of assessing skills at that level.

查看更多
Explosion°爆炸
7楼-- · 2019-04-05 17:06

The answer is polymorphism.

See http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming.

Many of the answers here discard this answer because there is no virtual function call. But that is just one example of polymorphism (and the most common). But that's not the only one.

The definition of polymorphism in simplest terms is summed up by the wikipedia article nicely:

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.

Certainly line 8 is an example of an object appearing to be of type Base but really is of type Derived

查看更多
登录 后发表回答