• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract Classes

 
Kimber Frye
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a question about abstract classes and inheritance.

I have a class named Item as follows:

class Item {

int a, b;

}

Now, I want to specify more specific items so I make Item abstract, create new classes that extend Item as follows:

abstract class Item {

}

class xItem extends Item {

}

class yItem extends Item {

}

class zItem extends Item {

)


The xItem and yItem have an attribute a and the zItem has the attribute b. So the question is do I put the a attribute in the Item class even though the zItem doesn't need to inherit the attribute?

thanks
 
Saravanan Jothimani
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes

you can put a attribute in Item class and if you want you can use in zitem.

Regards

Saravanan
 
Kimber Frye
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abstract class Item {
int a
}

class xItem extends Item {

}

class yItem extends Item {

}

class zItem extends Item {
int b
)


Is it good design to put a in the parent class even though all the subclasses don't use it but they all inherit it?

thanks
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If its a common property to all child classes, put it in a parent class, otherwise declare it in the child class which will use it. It keeps the code clean and avoids unneccessary inheritance of parent properties which the child class will never need. You need to make a design decision and should carefully think what will all the child Items which extend the parent Item class will need, and what is specific to each type of item.
 
Kimber Frye
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, if only 2 of the item subclasses use the attribute a, it would still be okay to put it in the parent item class. This attribute would be unused in the third class.

Thanks
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kimber Frye:
So, if only 2 of the item subclasses use the attribute a, it would still be okay to put it in the parent item class. This attribute would be unused in the third class.

Thanks


Usually when something lie this happens, the code is telling you that your inheritance relationships aren't quite right. You might consider introducing an "AItem" which extends Item, and is in turn extended by xitem and yitem. AItem would have an "a" attribute. zitem would extend Item directly, and zitem would have the "b" attribute. Now no classes have "extra" attributes.

An alternative is to make Item an interface rather than an abstract class, and have xitem, yitem, and zitem all implement that interface, and include only the attributes they need. You could still use the AItem class in this case, just as an implementation convenience.
 
Kimber Frye
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
Kenneth Brooks
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would agree with Ernest. Do not put something in the parent class that the child classes do not need or use.

That would be telling me that those child classes aren't really related to the parent class.

Sounds like the family tree got all forked up.

-k
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic