Results 1 to 9 of 9
  1. #1
    Bigfatcat is offline Member
    Join Date
    Apr 2011
    Location
    UK
    Posts
    10
    Rep Power
    0

    Default 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:
    Java 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

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: What about variables? implements Interface > extends

    I know inheritance via 'extending' is not promoted, in fact it has almost become a crime.
    What? How does one inherit any other way?

    Without extending from Animal these variables will have to be defined repeatedly in every class.
    True

    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.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default 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.
    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.

  5. #5
    Bigfatcat is offline Member
    Join Date
    Apr 2011
    Location
    UK
    Posts
    10
    Rep Power
    0

    Default Re: What about variables? implements Interface > extends

    Quote Originally Posted by quad64bit View Post
    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.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default Re: What about variables? implements Interface > extends

    Quote Originally Posted by Bigfatcat View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: What about variables? implements Interface > extends

    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

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default Re: What about variables? implements Interface > extends

    Quote Originally Posted by quad64bit View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: What about variables? implements Interface > extends

    Yeah, I hear that! :)

Similar Threads

  1. Replies: 4
    Last Post: 01-17-2012, 01:39 AM
  2. Extends a class that implements an interface
    By monocole in forum New To Java
    Replies: 2
    Last Post: 12-27-2011, 04:55 AM
  3. Advanced extends and implements
    By V1C3M4N in forum Advanced Java
    Replies: 15
    Last Post: 11-17-2010, 02:28 PM
  4. Interface and implements
    By Bored2 in forum New To Java
    Replies: 27
    Last Post: 11-10-2010, 02:08 PM
  5. Implements MyClass extends JFrame
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 04:43 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •