Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

issue when implementing abstract method in Java

Tags:

java

oop

I want to model the following situation in OOP:

enter image description here

I want that the Freight class to be an abstract class, because I would like that my program charges some extra fees according to the Degree of Hazardousness that one piece of cargo has.

Actually the problem that I got is that I want that the Freight class to be an array of objects. I mean that it can store Piece of luggages and Pieces of Cargo. My question is where can I put a method call addItem? should I put it into the Piece of Luggage and Piece of Cargo classes? or should I put a general abstract method called addItem into the Freight class? something like this (I am using Java for this purpose):

abstract class Freight{
//other things here
protected Freight[] fr=Freight[10];
protected int numItems;

abstract addItem();
}

class PieceOfLuggage extends Freight{
//other things
       public PieceOfLuggage(int iden,double weight,int id){
           super(iden,weight,id)
       }
       public addItem(){
           fr[numItems]=this;
           numItems++;
       }
}

class PieceOfCargo extends Freight{
      private degreeHazard;
      public PieceOfCargo(int iden,double weight,int id,int degHazard){
           super(iden,weight,id);
           degreeHazard=degHazard;
      }
      public addItem(){
          fr[numItems]=this;
          numItems++;
      }
}

so that in my main program I can do something like:

Luggage l1=new Luggage(100,50,1234);   //ident, weight, id
Cargo c1=new Cargo(300,123.56,1111,1); //ident, weight, id, degree of hazard
l1.addItem();
c1.addItem();

any suggestion where I can put that addItem method?, so that the class Freight contains an array of objects of type luggage and cargo?

Thanks

like image 763
Little Avatar asked May 28 '13 12:05

Little


People also ask

What is the implementation of an abstract method in Java?

An abstract method do not have a body (implementation), they just have a method signature (declaration). The class which extends the abstract class implements the abstract methods. If a non-abstract (concrete) class extends an abstract class, then the class must implement all the abstract methods of that abstract class.

Should a method always be declared in an abstract class?

A method must always be declared in an abstract class, or in other words you can say that if a class has an abstract method, it should be declared abstract as well. In the last tutorial we discussed Abstract class, if you have not yet checked it out read it here: Abstract class in Java, before reading this guide.

How do you declare an abstract class in Java?

A class is declared abstract using the abstract keyword. It can have zero or more abstract and non-abstract methods. We need to extend the abstract class and implement its methods. It cannot be instantiated. Note: An abstract class may or may not contain abstract methods.

What is the difference between abstract class and implementation class?

The abstract class is not real implementation class. It may contain abstract methods and doesnot need to implement the methods from the interface. It is concern of the REAL implementing class to define the abstract/interface methods.


2 Answers

I think that there is a design flaw here if I understand this right. You should use a container object like Freight which contains a Collection of items. But if you stick with this design what you need here is a Composite I think.

Excerpt from wikipedia:

interface Freight{

    void addItem();
}

/** "Composite" */
class CompositePieceOfCargo implements Freight {

    private List<Freight> childFreights = new ArrayList<Freight>();

    public void addItem(Freight freight) {
        childFreights.add(freight);
    }

}

/** "Leaf" */
class PieceOfCargo implements Freight {
    private degreeHazard;

    // your methods here
}

You can use a "Leaf" object in case you are dealing with a concrete Freight and if it is just a "container" you can use the "Composite".

This pseudo code points out the design flaw: In a Leaf object you can't provide a sensible implementation for addItem.

like image 145
Adam Arold Avatar answered Sep 29 '22 04:09

Adam Arold


If you want Freight to be able to hold those other types you have three options:

Make each class extend Freight:

class Luggage extends Freight

OR

give Freight an array of each:

class Freight
{  
     Luggage[] luggage = new Luggage[10];  
     Cargo[] cargo = new Cargo[10];  

     addItem(Luggage luggage){...}  
     addItem(Cargo cargo){...}
}  

OR

make Luggage and Cargo extend a base class and put that inside Freight:

   class DeliveryItem  
   {  
        addItem(DeliveryItem item){...}  
   }  

class Luggage extends DeliveryItem  
{  
    //override addItem if need be
}    

class Freight
{  
     List<DeliveryItem> items = new ArrayList<DeliveryItem>();  

     List<DeliveryItem> getItems()  
     {  
        return this.items;  
      }

     void addItem(DeliverItem item)  
     {
         this.items.add(item);
     }  
}  
like image 38
Woot4Moo Avatar answered Sep 29 '22 05:09

Woot4Moo