• Post Reply Bookmark Topic Watch Topic
  • New Topic

What are Inheritance and Interfaces?  RSS feed

 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly what are they and what do they do?

Just need to have some one break it down to apple and oranges if you can?
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inheritance is a feature of Object oriented programming. All OOP languages(For example, C++, Java, C# etc.) support inheritance.

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.

-
 
R Stewart
Greenhorn
Posts: 9
1
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The short and sweet answer:
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"
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
R Stewart
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A taxi is a specialised form of car.
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.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and welcome to the Ranch
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@stewart ie: A note on posting. Since this is a forum and not a mailing list it is preferable that you do not quote the entire post before yours.  A short snippet for context is okay.
 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In object oriented programming, inheritance means: specialization.

this clarifies inheritance to me much  more better than I used to think .
Thanks a lot.
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for the feed back. I have to play around with it a little more an dig in deeper and read a few articals but i am learning that's the main thing right? And with help from folks like you how can i go wrong.
 
R Stewart
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always found the best way to learn is to play about with it, and follow forums such as these.
I also found the mala gupta certification books to be a great source of knowledge.
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nver heard of the books but i will look into them.Thanks
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are asking beginning‑type questions, then a certification book might not be the best thing to read. Have you come across Sierra and Bates' Head First Java? Since it is now an old book, you can get lots of second‑hand copies.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!