2

I am really confused by this task. Knowing that instances of an abstract class can be managed by using their subclasses consider this: I was given an UML and it says I have an abstract class Room that has a constructor with arguments and a concrete method. Room also includes private fields length and width. Class Flatextends the abstract class, but Flat has no constructor. Another class called Building is used to manage all objects of Room via an array Room allRooms [].

public abstract class Room {
   private float length;
   private float width;

   public Room(float length, float width) {
     this.length = length;
     this.width = width;
   }

   public calcSurface () {
     return this.length * this.width;
   }
}

public class Flat extends Room {
   ... 
   /* no constructor is intended here, not even methods are getting
      overriden in here */
   ...
}

So after writing this I think, except extend Roomthere is no real connection between these two classes...

public class Building {
   public Room allRooms[]; 
   ... 

public boolean addRoom (Room room) {
   ... // for loop 
   Room allRooms[i] = room; //so here are my passed object. but 
   since i can't instantiate an abstract class I usually would have 
   used the (sub)classes ==> = {new Flat(...)};
   ...
}

Because of the fact Roomcontains private fields and is an abstract class, I can't really figure out how to use them without any real connection between my classes.

// method to calculate the surface of all rooms and flats
public float getSumOfRommsurface() {

}

public float getSurfaceOfFlats() {

}

So: How can I access Flat through Building and how can I instantiate Flatif there is no constructor, except the one inside the abstract class.

However, maybe there is a special concept of inheritance and association I have never heard before, if so, I would be really glad if someone could explain it to me in plain English.

Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55
MEL-T
  • 33
  • 5
  • 1
    If Flat has no constructor, then it can't possibly compile (unless Room also has a no-arg constructor. addRoom() takes a room as argument. So you don't need to create a new Room inside that method: the caller has already created a room and passed it as argument to the method. The method doesn't need to care about the concrete class of the room it receives: it's a room, and it can thus be stored in an array of Rooms. And Room has a calculateSurface() method, so you can call this method to compute the sum of all the surfaces of the rooms. – JB Nizet Jul 14 '19 at 12:46
  • You could always use [Reflection](https://www.baeldung.com/java-reflection) to loop through all of the Rooms in the Building and have them calculate the surface area. – CallSign-Filter Jul 16 '19 at 18:59

1 Answers1

0

Can you please share UML diagram or description against which you have written the code.

In my knowledge, this is not possible in java how you trying to achieve this due to the following reason: 1) If you'd created a constructor for a parent class then sub-class must have a constructor like this:

public class Flat extends Room {
    public Flat(float length, float width) {
        super(length, width);
        //some work
    }
}

or you can do something like this:

Can we instantiate an abstract class directly?

Zain Ul Abideen
  • 1,617
  • 1
  • 11
  • 25