What are Inheritance and Interfaces?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Just need to have some one break it down to apple and oranges if you can?
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
Inheritance is inheriting all public and protected member methods and fields of that class to avoid code duplication. For example, if you have 'car class' that holds common member methods and fields for a car, and then if you want to create a class specific to one type of car you can just inherit the 'car class' to avoid re-writing all those common features all car have.
In C++ one class can inherit multiple classes that leads to the diamond problem. To avoid this newer OOP languages only allows you to inherit only one class. But to accommodate the features of multiple inheritance, they have introduced new feature called Interface. Interface is like a blue print of a class that(generally) does not have implementation.
-
-
2 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
Inheritance is something you are (you inherit your appearance from your parents), an interface is something you promise to do (you carry out the duties as a shop worker when you need to)
A longer example:
Think of an interface as a contract that an object promises to adhere to.
For example: you might have a 'Manger' object with behaviours like "writeRota()", "leadMeeting()".
you might have another object called 'Recruiter' with behaviours like "headHunt()" and "readCV()"
Both of these objects from time to time hold job interviews, and for the purpose of the interview they are both seen as "inverviewers" even though in any other light they are completely different. (as an aside, this is known as polymorphism)
So the question is, How do you get these two completely different objects to both "act as an interviewer" when needed?
You create a contract of what an interviewer is expected to do (an interface) and both Manager and Recruiter objects agree to implement the behaviours set out in the interface when needed.
An interviewer has the behaviours of "asksQuestions()" and "makeHiringDecision()"
These are abstract behaviours meaning the interface simply tells Manager and Recruiter that they must ask questions and make a hiring decision, but doesn't detail how they should do it. That's up to the Manager or recruiter to decide individually.
Inheritance is the concept of children inheriting common traits from their parents.
Using the example we have above:
Both recruiters and managers are employees. Employee is their "parent".
Every employee regardless of role has the behaviours of "goToWork()", "collectPay()", "complainAboutJob()" etc. and attributes like "name", "address" "date of birth" etc.
So if the recruiter object inherits (or extends) Employee then it gets the traits above for free without having to implement them.
To remember it I always thought about it like this: "A manager IS AN employee"
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
stewart ie wrote:Inheritance is something you are (you inherit your appearance from your parents), ...
Inheritance is the concept of children inheriting common traits from their parents.
Using the example we have above:
Both recruiters and managers are employees. Employee is their "parent".
One thing to be careful with, with regard to the concept of inheritance, is not to confuse the meaning of inheritance in biology with object oriented programming.
Too often people are talking about "parent classes" and "child classes", and comparing inheritance in programming to biological inheritance (inheriting traits from your parents).
This can be confusing, because inheritance in programming really does not mean the same thing as inheritance in biology.
In object oriented programming, inheritance means: specialization.
stewart ie wrote:To remember it I always thought about it like this: "A manager IS AN employee"
That's a much better way of thinking about inheritance in programming. There is indeed an "is a" relationship between a subclass and it's superclass; an instance of a subclass is a (special kind of) an instance of its superclass. So, a manager is a special kind of employee. It inherits the traits of its superclass Employee, and adds / implements these traits in a way that is specific for managers.
Note that in biology, inheritance does not mean specialization. A Child is not a Parent; I am not a specialized version of my father.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Jesper de Jong wrote:
stewart ie wrote:Inheritance is something you are (you inherit your appearance from your parents), ...
Inheritance is the concept of children inheriting common traits from their parents.
Using the example we have above:
Both recruiters and managers are employees. Employee is their "parent".
One thing to be careful with, with regard to the concept of inheritance, is not to confuse the meaning of inheritance in biology with object oriented programming.
Too often people are talking about "parent classes" and "child classes", and comparing inheritance in programming to biological inheritance (inheriting traits from your parents).
This can be confusing, because inheritance in programming really does not mean the same thing as inheritance in biology.
In object oriented programming, inheritance means: specialization.
stewart ie wrote:To remember it I always thought about it like this: "A manager IS AN employee"
That's a much better way of thinking about inheritance in programming. There is indeed an "is a" relationship between a subclass and it's superclass; an instance of a subclass is a (special kind of) an instance of its superclass. So, a manager is a special kind of employee. It inherits the traits of its superclass Employee, and adds / implements these traits in a way that is specific for managers.
Note that in biology, inheritance does not mean specialization. A Child is not a Parent; I am not a specialized version of my father.
That's a much better way of putting it.
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
An alarm clock is a specialised form of clock.
An oil tanker is a specialised form of ship.
A destroyer is a specialised form of ship.
A dog is a specialised form of animal.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-

-
-
Number of slices to send:Optional 'thank-you' note:
-
-
All things are lawful, but not all things are profitable.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
In object oriented programming, inheritance means: specialization.
this clarifies inheritance to me much more better than I used to think .
Thanks a lot.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I also found the mala gupta certification books to be a great source of knowledge.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
| What do you have in that there bucket? It wouldn't be a tiny ad by any chance ... Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |










