Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending an already written class using OOPS

I am trying to learn Design practices and OOPs. I am using parking lot problem as sample to start.

I have a GeneralParkingLot interface and a Vehicle interface. GeneralParkingLot has only one function returnParkingLotSize, Vehicle interface has multiple Vehicle attributes.

I have created a class DowntownParkingLot which extends GeneralParkingLot and has other attributes like listOfCars, availableSlots etc. and a Car Class which extends Vehicle class.

I have a HandlerClass which handles incoming command and inside that class I have decalared a DownTownParkingLot object and multiple functions to handle commands, so in each function I just pass the object of DowntownParkingLot and operate on it.

I have created different services like CreateParkingLotObject, ParkACar, FreeASlot etc. which are called by the command handler.

I have also created unit Tests to test my application.

My problem is if I want to extend my current parking lot to have additional functionality like lets say multiple floor attribute or If I want to now handle multiple parking lots instead of one, so what would be the best way to extend my GeneralParkingLot class or DowntownParkingLot class. I have also read about adapter and decorator patterns, but I think those are useful when I am already following a particular design pattern from start, In my case I didnt follow any particular pattern, so what would be the best way to extend my code. I am asking this because sometimes we encounter a class which is not made according to any design pattern and is being used in multiple places (like a lot of API's etc), so what is the best way to extend such a code. Is refactoring from start the only option? or creating new classes which inherit from old classes? What would be the best way? Also I would like to use the already created unit tests as much as possible and not rewrite same test cases again.

like image 246
Legendary_Hunter Avatar asked Jun 13 '18 14:06

Legendary_Hunter


People also ask

How do you extend one class to another?

The extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class.

Can I extend a class that extends another class?

You can extend a class to provide more specialized behavior. A class that extends another class inherits all the methods and properties of the extended class. In addition, the extending class can override the existing virtual methods by using the override keyword in the method definition.

When a new class is created from an existing class by extending it it is called?

The process of creating a new class i.e., derived class from an existing class is called as inheritance. There are five types of inheritance: single inheritance, multiple inheritance, multilevel inheritance, hybrid inheritance, hierarchical inheritance.

How do you create a class that extends another class in Java?

To create a sub class (child) from a Java super class (parent), the keyword extends is used. You then follow the "extends" keyword with the parent class you want to extend.


1 Answers

In your case, your top most class is an interface so you cannot add attributes directly.

One way to solve your problem is create a top most implementation class which implement the GeneralParkingLot interface:

public abstract CommonParkingLot implements GeneralParkingLot {
     // add your missing attributes here
     protected int floorNo;
}

(If you just want to add attributes without provide some default implementation for GeneralParkingLot then you can omit the implements part)

And then have your implementation class extends this CommonParkingLot:

public class DowntownParkingLot extends CommonParkingLot implement GeneralParkingLot {
     // Now all missing attributes are accessible here
}

For missing functionalities, the default function interface should come handy. (the keyword is language specified), you can add as more function as you need to the GeneralParkingLot interface with a default body for it.

One more point, your current implementation kinda restricted to a DowntownParkingLot because you don't add common functionality to the interface. I would suggest you to refactor the HandlerClass class a bit:

public interface GeneralParkingLot {
    int returnParkingLotSize();
    ParkingLotObject createParkingLotObject();
    boolean parkACar();
    int freeASlot();
    default public void parkMultipleCar() {
         throws new UnsupportedOperationException();
    }
}

Now in your HandlerClass should operate on the GeneralParkingLot instead of DowntownParkingLot.

By doing this, you can add more GeneralParkingLot without changing code.

like image 127
Mạnh Quyết Nguyễn Avatar answered Oct 29 '22 08:10

Mạnh Quyết Nguyễn