1

I am implementing library system as Exercise, in that I have created one abstract class :

 public abstract class RentalItem
{
    .....  
    public RentalItem() { }
    public RentalItem(string itemID, string title, int qty,float price, PeriodType period) {
        ItemID = itemID;
        Title = title;
        No_Of_Copies = qty;
        Period = period;
    }
    public abstract void addItem(string itemId, string title, int no_of_copies,float price,PeriodType p);
   .....
}

Afterwards, I have created MovieItem class, which inherits RentalItem class : now this class has extra fields. as below :

public Movie(string itemId, string title, int no_of_copies, float price, PeriodType p, MovieType type, string actor, string director)
        : base(itemId, title, no_of_copies, price, p)
    {

        this.Type = type;
        this.Actors = actor;
        this.Director = director;
    }


public override void addItem(string itemId, string title, int no_of_copies,float price,PeriodType p){};

But actully I want to implement addItem method such away, it takes base parameters + additional parameters as below :

public void addItem(string itemId, string title, int no_of_copies, float price, PeriodType p, MovieType type, string actor, string director)

So how can I use abstract method? And if I am implementing my own addItem(...) method then what is use of abstract class ?

divyanshm
  • 6,600
  • 7
  • 43
  • 72
Hakoo Desai
  • 341
  • 3
  • 4
  • 21

3 Answers3

0

When using an abstract method you have to override the method, which means you have to adhere to the original signature of the abstract method. You can not add or remove parameters from the original signature. If you need to change the signature, then it's not appropriate to use an abstract method

TGH
  • 38,769
  • 12
  • 102
  • 135
  • yeah, I understood that one, so there is no meaning of defining Abstract method in Abstract class in my case. right? – Hakoo Desai May 14 '13 at 05:57
0

You are trying to abstract 2 things that is different. Is adding a RentalItem is same with adding a MovieItem? If yes then you can abstract the method. Otherwise, better not abstract it.

EDIT:

Maybe I am not clear with my statement. Now look at example of this code:

RentalItem rent = new RentalItem();
rent.AddItem(..params);

Is the usage of AddItem method in RentalItem class. Now for the MovieItem class:

MovieItem mv = new MovieItem();
mv.AddItem(..params);

Now looks fine. However, because MovieItem is inherited by RentalItem, we can implement it like this:

RentalItem item = new MovieItem();
item.AddItem(..params);

This comes the problem. We can see that the MovieItem.AddItem is different with RentalItem.AddItem, because it cannot be invoked by RentalItem, and both method accept different parameter. Now what we can do?

First, if does not needed or if not logically same, let the MovieItem as a new class instead inherited from RentalItem.

Second, if you need inheritance, let the RentalItem and MovieItem become only the entity with just properties and not method. And for handling the AddItem, you create another class like RentalItemCollection and MovieItemCollection which in logic does not has relation each other.

Fendy
  • 4,565
  • 1
  • 20
  • 25
  • Actually, MovieItem is RentalItem. So, I am trying to give common behavior to MovieItem like Add,Edit, Delete and Update methods, which child class has to implement. But as I mentioned, MovieItem adds, RentalItem+It's own parameters in one line and writes into file. – Hakoo Desai May 14 '13 at 06:07
0

Method signatures have to remain the same in order for abstract classes to work. If your addItem method from Movie class has more parameters, you can deal with that by changing its signature to accept List or Dictionary or some other collection, or you can encapsulate parameters in a new class named RentalItemProperties.

Maisie John
  • 1,016
  • 9
  • 9