Results 1 to 9 of 9
- 01-06-2012, 06:58 PM #1
Member
- Join Date
- Apr 2011
- Location
- UK
- Posts
- 10
- Rep Power
- 0
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:
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?Java Code:public class SharedParameters { private Age age; private Weight weight; private Gender gender; ...etc... } public class Dog { private SharedParameters ageWeightGender; ...etc... }
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
- 01-06-2012, 09:40 PM #2
Re: What about variables? implements Interface > extends
What? How does one inherit any other way?I know inheritance via 'extending' is not promoted, in fact it has almost become a crime.
TrueWithout extending from Animal these variables will have to be defined repeatedly in every class.
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.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?
- 01-06-2012, 11:01 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-06-2012, 11:06 PM #4
Re: What about variables? implements Interface > extends
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.Extending from a class is considered a bad thing because it is one of the highest forms of coupling between two entities.
- 01-07-2012, 07:47 PM #5
Member
- Join Date
- Apr 2011
- Location
- UK
- Posts
- 10
- Rep Power
- 0
- 01-07-2012, 08:38 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: What about variables? implements Interface > extends
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-08-2012, 02:37 AM #7
Re: What about variables? implements Interface > extends
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! :DJust wait until you have to program in a moderately sized (large?) team of programmers; you'll love interfaces and composition over directly extending implementations.
- 01-08-2012, 08:28 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: What about variables? implements Interface > extends
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-08-2012, 08:57 AM #9
Similar Threads
-
Creating a class that extends Activity and implements OnClickListener
By sehudson in forum AndroidReplies: 4Last Post: 01-17-2012, 12:39 AM -
Extends a class that implements an interface
By monocole in forum New To JavaReplies: 2Last Post: 12-27-2011, 03:55 AM -
Advanced extends and implements
By V1C3M4N in forum Advanced JavaReplies: 15Last Post: 11-17-2010, 01:28 PM -
Interface and implements
By Bored2 in forum New To JavaReplies: 27Last Post: 11-10-2010, 01:08 PM -
Implements MyClass extends JFrame
By coco in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 03:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks