Alex Rivera | Logout

Which OO concept is "Base b = new Derived()" an example of?

Asked 2009-04-22T19:47:03.187
10

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?

Edit
Report

2 Answers

3
  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

answered 2009-04-22T19:53:34.217
2

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.

answered 2009-04-22T21:55:26.967

Your Answer