Quantcast
Channel: Programming Baba » OOP
Viewing all articles
Browse latest Browse all 5

what is diamond problem?

0
0

 

Explanation with examples:-

                         In OOP(Object-oriented programming language)there is a concept of multiple inheritance.
Suppose we have two child classes child1 and child2 that inherit from Class Parent and there is another class baby that inherits from both classes child1 and child2.

So there is a show() function in Parent class , when class baby calls its method/function that is not overridden from class Parent (whereas class child1 and child2 both contains same function) then there is an ambiguity in baby class that from which class does it inherit … !child1 or child2 ?? There is an ambiguity b/w both classes. The compiler generates an error because it doesn’t understand to which function call.

This problem is named as diamond because it looks like diamond shape, figure shown below:-

d2p

NOTE:- C# doesn’t support to multiple inheritance and there isn’t any use of multiple inheritance in practices.

  • Example of this problem is shown below:-

d1p

  • Thus the solution of this problem is as follows:-ambiguityremoveExample 2:
  • Parent class contains only one data member ‘n’ with a protected access specifier.
  • child1  and child2  both classes inherits same data.
  • When a baby child needed to override and want to show the result then the compiler generates an error .
  • Because contains two copies of same data and it doesn’t understand which one will be passed to baby class.abn1

 

  • Solution of this ambiguity is to write virtual keyword .
  • That will help to generate only one copy of data and the compiler will not confused b/w both classes.

 

abac

 

The post what is diamond problem? appeared first on Programming Baba.


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images