What about variables? implements Interface > extends
Hi All,
I know inheritance via 'extending' is not promoted, in fact it has almost become a crime. However I find the the best thing about it is that I can specify variables in one class instead of many.
For example let's say I have the following classes:
abstract Animal - variables: Age, Gender, Weight
Dog extends Animal
Cat extends Animal
Parrot extends Animal
Without extending from Animal these variables will have to be defined repeatedly in every class. During my time at university I've found 'extends' to be very useful. I use it sparingly and only for the reduction of repetition. However, inheriting such variables from an Interface is pretty useless considering any variable will be declared as 'public static final'.
I know one possibility is:
Code:
public class SharedParameters {
private Age age;
private Weight weight;
private Gender gender;
...etc...
}
public class Dog {
private SharedParameters ageWeightGender;
...etc...
}
With that said, I can't see why that is any less messy than 'extending' from an abstract. Or is it better simply because it is more easily extendable in terms of polymorphism?
I realise I've almost answered my own question but I was wondering whether I should completely avoid 'extends', perhaps even forget it exists. Or maybe it is okay to use it sparingly?
Regards,
BFC
Re: What about variables? implements Interface > extends
Quote:
I know inheritance via 'extending' is not promoted, in fact it has almost become a crime.
What? How does one inherit any other way?
Quote:
Without extending from Animal these variables will have to be defined repeatedly in every class.
True
Quote:
With that said, I can't see why that is any less messy than 'extending' from an abstract. Or is it better simply because it is more easily extendable in terms of polymorphism?
I realise I've almost answered my own question but I was wondering whether I should completely avoid 'extends', perhaps even forget it exists. Or maybe it is okay to use it sparingly?
Really have no idea what you're talking about. Who told you extending a super class was bad? How do you do OOP without doing that? Take a look at the Java API, it is FULL of direct parent child inheritance.
Re: What about variables? implements Interface > extends
Extending from a class is considered a bad thing because it is one of the highest forms of coupling between two entities.
kind regards,
Jos
Re: What about variables? implements Interface > extends
Quote:
Extending from a class is considered a bad thing because it is one of the highest forms of coupling between two entities.
Meh, I hear you there, but I don't believe everything should be decoupled simply for the sake of loose coupling. There is a time and place for everything, and hierarchal relationships via inheritance have very useful purposes, especially in things such as data structures.
Re: What about variables? implements Interface > extends
Quote:
Originally Posted by
quad64bit
Meh, I hear you there, but I don't believe everything should be decoupled simply for the sake of loose coupling. There is a time and place for everything, and hierarchal relationships via inheritance have very useful purposes, especially in things such as data structures.
I agree with you but everywhere I look 'extends' is badmouthed. I'm glad to know I'm not the only one that finds this somewhat overexaggerated though.
Re: What about variables? implements Interface > extends
Quote:
Originally Posted by
Bigfatcat
I agree with you but everywhere I look 'extends' is badmouthed. I'm glad to know I'm not the only one that finds this somewhat overexaggerated though.
Just wait until you have to program in a moderately sized (large?) team of programmers; you'll love interfaces and composition over directly extending implementations.
kind regards,
Jos
Re: What about variables? implements Interface > extends
Quote:
Just wait until you have to program in a moderately sized (large?) team of programmers; you'll love interfaces and composition over directly extending implementations.
Hey, I wasn't badmouthing interfaces in the slightest! I'm just saying that 'extending is bad' is a blanket statement which doesn't hold in many many cases. I work on a massive commercial CMS product, and believe me, I'd be lost without interfaces and composition! :D
Re: What about variables? implements Interface > extends
Quote:
Originally Posted by
quad64bit
Hey, I wasn't badmouthing interfaces in the slightest! I'm just saying that 'extending is bad' is a blanket statement which doesn't hold in many many cases. I work on a massive commercial CMS product, and believe me, I'd be lost without interfaces and composition! :D
I didn't take it as badmouthing but there (always?) is a piece of truth in blanket statements; same as "always use getters and setters", or "code to the interface, not to the implementation"; they all have a piece of truth in them that shouldn't be ignored.
kind regards,
Jos
Re: What about variables? implements Interface > extends