tags:

views:

1155

answers:

10

Hi Guys,
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?

+7  A: 

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.

Mehrdad Afshari
yes you are correct, polymorphism is more closely tied to behavior.
Doug T.
I do not agree (yet). There is nothing being inherited.
Joe Philllips
@d03boy: Derived clearly inherits Base. Even though it doesn't inherit any implementation, it does inherit the fact of "being a Base".
Mehrdad Afshari
Fair enough. I'm still not convinced that your definition of abstraction is accurate though.
Joe Philllips
@d03boy: It's not incorrect, but it's very rough (as I mentioned). Abstraction in general means removing crap (unrelated stuff) when thinking about an idea. It'll result in a more general and simplified model of thought.
Mehrdad Afshari
In most examples of inheritance you would see Base implementing a method and then Derived executing it.
Joe Philllips
@d30boy: There's no point in arguing about this. This is not important at all. Object oriented programming is a unified concept. It's not really acceptable to separate its components without any intersection. Anyway, while I believe I'm pretty correct in this choice, I might be wrong. So, I'll stick with my choice until someone can convince me otherwise.
Mehrdad Afshari
@d30boy: By the way, for the record, I agree that abstraction is valid too, but I don't really count abstraction as a separate concept. It's all over there in OOP. Whatever you do somehow relates to abstraction.
Mehrdad Afshari
Line #2 would likely have been the "chosen" line to point out if inheritance was the answer.
Joe Philllips
And why do they say Base = new Derived() instead of Derived = new Derived()
Joe Philllips
It's to show that Derived 'is of' Base. If Derived did not inherit Base, there would be a compiler error there.
Justin Drury
*EDIT Above* 'is of' should be 'is a'
Justin Drury
You do not need Base b = new Derived() to show inheritance. You could just as easily use Derived b = new Derived() and it would still represent inheritance. (Sorry for my typos before, missing b)
Joe Philllips
How would that show inheritance? Your just creating a new object of the same type. Base b = new Derived() means your created a new Derived object as its base type, Base. It's the same as say... Stream s = new MemoryStream().
Justin Drury
@d03boy: It's ambiguous in this snippet but Base b = new Derived(); is used when the object that's using b intends to treat it like a Base. You can't instantiate an object of type base because it's abstract.
SnOrfus
Guys! forget about it! Brainbench has thrown a STUPID test on the net (that's probably why it's free) and we're ... http://xkcd.com/386/
Mehrdad Afshari
Also, the def for aggregation is a bit misleading in "is composed out of another entity." Composition != Aggregation.
SnOrfus
SnOrfus: Agreed on that point. I was thinking about the same thing when I wrote that, but I didn't want to list everything here and thought it's a better simplification. I put "rough" on the title for these reasons ;)
Mehrdad Afshari
Line 2 is about inheritance. Line 8 is polymorphism. Inheritance is more related to classes while Polymorphism is to objects. That would be the main difference.
OscarRyz
+3  A: 
  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

bb
excuse me while I borrow your definition :P
Joe Philllips
nevermind. we are on the one side:))
bb
Abstraction does not mean using a "more abstract" class. Abstraction means that you hide implementation details behind a model. Client code interacts with your API at the level of the API, not at the level of the implementation.
Wedge
@bb: Abstraction applied to OO means, you don't have to specify all the details of a real object to model it on OO system. For instance, to model a Car, depending on your domain, using: name, year, and color could be enough. You don't need to add: Tire [] tires = Tire[4]; nor a Wheel, you just ABSTRACT the concept and use the relevant parts only.
OscarRyz
abstraction is very fuzzy term. But one of meanings is more high level of inheritance, such as in question.
bb
@Wedge, that is exactly what is happening here. The abstraction here is Base, whatever happens "behind that" (ie: the implementation) doesn't matter.
Joe Philllips
+4  A: 

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.

Simon Gibbs
Polymorphism would require that different prototypes of the same method/function exist.
Joe Philllips
Inheritance is about code reuse, just not for the derived to reuse the base, but rather for the derived to be used with code that already works with base.
David Rodríguez - dribeas
I'd always put the emphasis on being able to handle instances with same code. I accept I may be wrong but I think its worth seeing how many people feel the same way. Maybe I'll mark this a wiki post so I don't get rep.
Simon Gibbs
@dribeas, not sure what you. afaict, implementation inheritance certainly involves re-using the base class, but I agree interface inheritance encourages re-use of code outside the class. +1 for pointing out the re-use value outside the class.
Simon Gibbs
+28  A: 

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."

Brian ONeil
It's not polymorphism because the highlighted code does not use any polymorphic features. Just because you derive from Base does not make your class polymorphic.
John Dibling
you are incorrect... when you assign a type to a reference of a base type that is a Polymorphic behavior of the Type. It is not the inheritance that is Polymorphic, it is the assignment that is the Polymorphic behavior.
Brian ONeil
@brian How do you achieve polymorphism w/o virtual methods, of which none are shown?
Duck
By that logic all classes that derive from base classes are polymorphic. Doing "base* p = derived;" does not a polymorphic class make. That's just a feature of inheritance, which is not the only requirement to be polymorphic.
John Dibling
virtual methods are what most people think of when you talk about Polymorphism, but they are just an example of Polymorphic behavior. I am not saying that because one class derives from the other that is a polymorphic behavior... I am saying that the assignment of a variable that is of the base class type to an instance of the derived type is a Polymorphic behavior. If you look at the definition (on link included) I think that you will agree.
Brian ONeil
The problem with the presented example is that the classes do nothing. If you saw code like this would you say it is polymorphic? class Base { public: string doThing() {return "hi"; }; class Der : public Base { public: string doThing() { return "hello"; } }; class Base * p = new Der;
John Dibling
Also, please present an example of classes that have member functions and are polymorphic but have no *virtual* functions in C++.
John Dibling
" am saying that the assignment of an instance to a variable of a type that it derives from is an example of Polymorphic behavior." This is not true, and the following wiki quote does not follow logically from your claim.
John Dibling
This whole discussion comes down to one side thinking it is polymorphism if it looks like a duck. The other side thinks that not only does it need to look like a duck, it needs to behave like duck. Now I know something about being a duck. A wooden decoy, no matter how well painted, is no duck. Since the object in the test question, in the languages presented, cannot behave like a duck without virtual methods it ain't no duck. So says Duck. Finito and a goodnight to all!
Duck
Really... still? The quotes could go on and on... every actual definition from just about anywhere says that polymorphism fits the assignment of one type to another as to treat it as the other type. Virtual methods are tools of polymorphism not the definition of it! Seriously, read a book, an article, hell one of the many links I have posted.
Brian ONeil
Your own quotes provide support for the other position. Your updated quotes specifically state the data types as being "handled" in the first quote and "be used like" in the second. In the test question presented and in the languages referenced, that criterion is not met. There is the assignment of compatible pointer types. Not any more polymorphic in and of itself than automatic promotion of an int to a long.
Duck
Assignment is usage, and your example of int to long is invalid. Derived is a Base there is no conversion going on here like there is in the promotion of int to long. We read it differently.
Brian ONeil
It wasn't an example. It was a very late night attempt at a mildly humorous analogy. Look, you see 200 angels on the head of this pin, others see 100. In either case it is inappropriate for a standardized test that some human resource manager is going to base decisions upon. Life moves on.
Duck
Why you decide that this is polymorphic behavior? Where you see any behavior - for get behavior we should call method.
bb
+14  A: 

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.

Duck
Absolutely right --- developing good test items that produce predicable results is very difficult, and one that few test companies get right. (IMO)
Ralph Shillington
100% agreed. The test is a crap.
Mehrdad Afshari
Yes. None of those choices are correct.
John Dibling
+7  A: 

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.

JohnMcG
+2  A: 

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.

Robert Lewis
funny, you didn't read the first paragraph in the second article you linked... It clearly says "...polymorphism is the ability of one type, A, to appear as and be used like another type, B...". That definitely fits.
Evan Teran
@Evan I see what you mean, but I'm suggesting that in the context of mainstream object-oriented languages, the term "polymorphism" is mostly used when discussing the ability of subclasses to override functions defined in a base class, which the code sample doesn't show.
Robert Lewis
A: 

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.

John Dibling
+3  A: 

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

Evan Teran
+2  A: 
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

ja
it should be one of them.
Mykola Golubyev