Oop - going deep into inheritance is the best option? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Oop - going deep into inheritance is the best option?

I am writing a program that has a many different users: visitor, member, writer, Editor, Manager, administrator. There are many behavior that they have in common, like writing: all of them can, start from Writer. so I arrange them in father-son relationship. start with complete abstract User class, then class Member and Visitor inherit from that, then writer from member, Editor from writer, manager from Editor and finally Administrator from Manager. is this deep inheritance right? (god damn 512 char limit )

8th May 2017, 7:41 AM
Hiwa
Hiwa - avatar
4 Answers
+ 5
Hmm, if your compiler shows a warning about that, then it isn't safe, I wouldn't go too far if I were you thou
8th May 2017, 9:26 AM
Complex
Complex - avatar
+ 4
well I think it's not just about compiling, what about the right designing and code reusabilty and not repeating codes?
8th May 2017, 9:30 AM
Hiwa
Hiwa - avatar
+ 4
good advise krish, but the whole point that I want to use inheritance is I don't want to repeat the code, with interface I should write the whole block again. but for example writer, editor, manager all write in a same way, so why should I write that again? I heard about strategy pattern that could solve problems like that, is that right?
10th May 2017, 10:19 AM
Hiwa
Hiwa - avatar
+ 3
You should create and Interface when doing this project. try overriding methods when dealing wtih inheritance. an Abstract class will be of help, but since you dealing with so many people, try coding an Interface (IPerson) and then use it as your base.
10th May 2017, 9:55 AM
Krishneel Nair
Krishneel Nair - avatar