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
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?
Polymorphism, in C#, is the ability of objects of different types to provide a unique interface for different implementations of methods. It is usually used in the context of late binding, where the behavior of an object to respond to a call to its method members is determined based on object type at run time.
Inheritance is a feature of object-oriented programming languages that allows you to define a base class that provides specific functionality (data and behavior) and to define derived classes that either inherit or override that functionality.
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."
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.
This snippet is about Inheritance
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With