Results 1 to 7 of 7
  1. #1
    JayJayJay is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default Best Practice inheriting attributes

    Hello,

    I'm searching for the Best Practice to hardcode an attribute of a Subclass, which is declared in the Superclass.

    For example:
    A square is a rectangle, but only one of the sides needs to be initialzed.
    Or: Harry Potter is a Wizard, but his Magic Power is always 50.

    I thought of 3 Methods to inherit attributes an initializing them inside of the Subclass (hard-coded):

    1. Method:
    Java Code:
    abstract class Superclass {
      protected int attribute;
    }
    
    class Subclass extends Superclass {
      Subclass() {
        super();
        this.attribute = 10;
      }
    }
    Problem:
    If somebody creates an instance of Subclass, he isn't force to initialize the attribute.

    2. Method
    Java Code:
    abstract class Superclass {
      abstract int getAttribute();
    }
    
    class Subclass extends Superclass {
      int getAttribute() {
        return 10;
      }
    }
    Problem:
    Technically, it's an attribute, this is a method.

    3. Method

    Java Code:
    abstract class Superclass {
      protected int attribute;
      public Superclass(int attribute) {
        this.attribute = attribute;
      }
    }
    
    class Subclass extends Superclass {
      Subclass() {
        super(10);
      }
    }
    Problem:
    When there are many attributes it gets very unclear and hard to read/understand.


    I hope you understood what my Problem is.
    Now I want to know which one of these is the best Practice?

    - JayJayJay1
    Last edited by JayJayJay; 08-16-2013 at 08:30 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Best Practice inheriting attributes

    Quote Originally Posted by JayJayJay View Post
    A sqare is a rectangle
    Mathematically speaking you are right, but OO speaking that is an extremely dangerouse assumption if you have setWidth( ... ) and setHeight( ... ) methods in your Rectangle class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    JayJayJay is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default Re: Best Practice inheriting attributes

    That's not really my problem. I'm searching for a way to say:

    Tiger inherits from Cat. Cat has an attribute "danger-level", which all it's subclasses have to initialize.
    Tiger shall always have the "danger-level" 100.

    Sorry, if my previous post wasn't clear enough.

    JayJayJay

  4. #4
    tech.sol18 is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default Re: Best Practice inheriting attributes

    Hi,
    I think, first method is the best one. In second method your using a method to return a constant value which is a poor coding standard and in third method, as you mentioned, more number of attributes will hard to read/understand.

    Regards,

    Akram

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Best Practice inheriting attributes

    I don't get the "problem" of case 1. Of course the person creating the object doesn't need to initialize the property, the constructor does that for you. That's not a problem, that's working as designed.

    Without a specific context, I prefer the third one myself.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Best Practice inheriting attributes

    Square/Rectangle example:
    Java Code:
    class Rectangle {
        private int width;
        private int length;
    
        Rectangle(int w, int l) {
            width = w;
            length = l;
        }
    }
    
    class Square extends Rectangle {
        Square(int side) {
            super(side, side);
        }
    }
    Cat/Tiger example:
    Java Code:
    class Cat {
        private int dangerLevel;
    
        Cat(int dl) {
            dangerLevel = dl;
        }
    }
    
    class Tiger {
        Tiger() {
            super(100);
        }
    }
    I don't know what your problem is.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Best Practice inheriting attributes

    Quote Originally Posted by Junky View Post
    Square/Rectangle example:
    Quote Originally Posted by JosAH View Post
    .. if you have setWidth( ... ) and setHeight( ... ) methods in your Rectangle class.
    Nuff said.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. class attributes vs instance attributes in web apps
    By rdangelo in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-26-2012, 01:39 PM
  2. Having trouble with inheriting constructor from superclass
    By coasterguy10 in forum New To Java
    Replies: 3
    Last Post: 10-20-2011, 06:07 AM
  3. Replies: 4
    Last Post: 10-12-2011, 12:48 AM
  4. Inheriting two kinds of Lists into one Class
    By rsiddharth in forum New To Java
    Replies: 22
    Last Post: 12-26-2010, 04:39 PM
  5. How is this dog inheriting a bark?
    By fresh83 in forum New To Java
    Replies: 3
    Last Post: 07-05-2010, 02:33 AM

Posting Permissions

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