Two Laptop Bag*
The moose likes Beginning Java and the fly likes Abstract Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract Classes" Watch "Abstract Classes" New topic
Author

Abstract Classes

Kimber Frye
Greenhorn

Joined: Feb 25, 2005
Posts: 4
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

Joined: Feb 25, 2005
Posts: 26
Yes

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

Regards

Saravanan
Kimber Frye
Greenhorn

Joined: Feb 25, 2005
Posts: 4
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

Joined: Jan 08, 2005
Posts: 375
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

Joined: Feb 25, 2005
Posts: 4
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Kimber Frye
Greenhorn

Joined: Feb 25, 2005
Posts: 4
Thanks!
Kenneth Brooks
Greenhorn

Joined: Feb 21, 2005
Posts: 9
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
 
wood burning stoves
 
subject: Abstract Classes
 
Similar Threads
Abstract class question
abstract
abstract method
Inheritance with abstract classes
Mutually dependent generic abstract classes