-1

I have an abstract class Rental...I have two more subclasses,SuiteRental and SimpleRoomRental that inherits Rental class. I have an ArraList in a third class Hotel (not connected with the others) that contains both objects SimpleRoomRental and SuiteRental.

So,I want to examine each object (propably with instance_of) but i can't create a Rental object (cause its abstract class).What type should I declare my 'k' object?!?

any ideas?

public void calculateTotalRentalFees (){

      double price=0; 

      for (int i=0; i<rentals.size(); i++){

          if (k instanceof SimpleRoomRental){
            SimpleRoomRental y= new SimpleRoomRental();
            price=price + y.calculateCost();

          }else if(k instanceof SuiteRental){
              SuiteRental z=new SuiteRental();
              price=price + z.calculateCost();

          }

      }
  }

Also I have an interface that contains calculateCost() method.

public interface Cost {

public interface Costs {
     double calculateCost();             
}    

}

thank you for your time

Manolis
  • 9
  • 1
  • Just because you can't use `new` to create an instance of Rental doesn't mean that you can't have a variable of type Rental. Note that the instanceof checks are useless. Just add a calculateCost() abstract method in Rental. You're completely missing the point of inheritance and polymorphism. – JB Nizet Nov 21 '16 at 15:01
  • Declare it as whichever class the `ArrayList` is declared as. – RamenChef Nov 21 '16 at 15:01
  • Well what's the type of `rentals`? Note that you almost certainly want to cast `k` to `SimpleRoomRental` or `SuiteRental` rather than creating new instances... and I suspect you'd be better off with an enhanced `for` loop. But do you even need to cast them? Doesn't `Rental` have a `calculateCost()` method? It sounds like it should have, and if `rentals` is an `ArrayList`, that's all you need. – Jon Skeet Nov 21 '16 at 15:02
  • Fundamentally though, we need a [mcve] to provide a lot more information... – Jon Skeet Nov 21 '16 at 15:02
  • @JonSkeet the type of 'rentals' is 'ArrayList'.Rental doents have a 'calculateCost()' cause I have to use somehow the inteface! – Manolis Nov 21 '16 at 15:14
  • Well what sort of rental doesn't have a cost? Maybe `Rental` should implement `Cost`. You still haven't provided all the relevant information *in the question*. – Jon Skeet Nov 21 '16 at 15:35
  • The programming technique is called "Polymorphism". There is a lot of material on the web for the interested. It is definitely not possible to instantiate an abstract class, but you can instantiate a concrete class as any parent class. It is for example possible to write `CharSequence cs = new String("hi")`. I am not entirely sure what the problem is though. – patrik Nov 21 '16 at 15:39

1 Answers1

0

By using interfaces you are also able to handle them both, that's the benefit of using polymorphism, they can even have different implementations for that method!

RoomRental rental = new SimpleRoomRental(); 
// You can use too: new SuiteRental();
price = price + rental.calculateCost();