Abstract class and Abstract Method in Java:
It is worth looking at another example, as abstraction is an important principle in object oriented design. This time we consider how to construct an abstract class hierarchy by factoring out common behaviour in a number of subclasses.
The example concerns an information system for a warehouse, perhaps for an e-commerce company. The system needs to keep track of a wide variety of items that customers may wish to order. We can define a number of classes to model the various sorts of items, such as Book, MusicCD, VideoDVD, ComputerGame, HouseholdItem, GardenItem and so on. We assume that a detailed object-oriented analysis and initial design has been carried out to identify the required classes and the details of each class - its data items and behaviour. As a result, we find that there are certain common features between many of the classes. For example, each item has a name, an item code and a unit price. Each class will need instance variables, itemName, itemCode and unitPrice, and associated methods for accessing or modifying this data, such as getUnitPrice and setUnitPrice. Instead of repeatedly implementing these separately in each class, we can factor out the common behaviour by defining a more general class Item, from which all these classes inherit. The Item class will define instance variables itemName, itemCode and unitPrice. The associated methods for accessing or modifying this data may be either concrete or abstract, depending on the circumstances; for example, getName is likely to be the same for all subclasses of Item, so it can be implemented as a concrete method. If returning the unit price depends on factors that differ between types of item, then getUnitPrice could be left as an abstract method to be overridden by each specialized subclass. In this case, Item must be declared as an abstract class.
We can apply this factoring at a number of levels, if appropriate. So, for example, we may be able to identify commonality between MusicCD, VideoDVD and ComputerGame. In this case we can factor out the common aspects and define a superclass, say, MultimediaItem. This could be either concrete or abstract, as before, depending on how similar the common behaviour is.
![2296_abstract class.png](https://www.expertsmind.com/CMSImages/2296_abstract%20class.png)
Figure shows a possible hierarchy for this.
Here are some code fragments to illustrate how some of the various classes could be declared:
public abstract class Item
{
private String itemName; private String itemCode; protected int unitPrice;
public String getItemName ()
{
...
}
public abstract int getUnitPrice ();
...
} // end class Item
public abstract class MultimediaItem extends Item
{
...
}
public class MusicCD extends MultimediaItem
{
...
public int getUnitPrice ()
{
// concrete method, so code goes here
}
}
public class VideoDVD extends MultimediaItem
{
...
public int getUnitPrice ()
{
// concrete method, so code goes here
}
}
public class PrintedItem extends Item
{
...
public int getUnitPrice ()
{
// concrete method, so code goes here
}
}
public class Book extends PrintedItem
{
...
/* need not implement getUnitPrice()
because inherits it from PrintedItem
but could still override it if need be */
}
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Abstract class and Abstract Method in Java homework and assignments? Live Abstract class and Abstract Method in Java experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Abstract class and Abstract Method in Java homework help, java assignment help and Abstract class and Abstract Method in Java projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours