• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Abstract Classes

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic