Posts

Showing posts from August, 2017

Design a parking lot using object-oriented principles?

Image
To design the parking lot system , we will make our own assumption to start with . So that we could keep our design simple and easy to grasp what is happening . As we would  be done with the development as per our first assumption. We will gradually increase the dimension of our assumption and enhance the system accordingly. Our assumption are as below: 1) Parking lot is only ground floor , so there is no multiple level i.e there is only one level. 2) Parking lot has three different kind of parking lot : small parking lot, medium parking lot and large parking lot. 3) There are three different kind of vehicle which needs to be parked. 4) Motorcycle needs to be parked in small parking lot , car needs to be parked in medium parking lot and bus needs to be parked in large parking lot. As we can see that motorcycle and bus all comes in the category of vehicle. So we will be using the inheritance property of OOPS and will create vehicle abstract class and will keep the

Covariant Return Type in Java

Before JDK 5.0, it was not possible to override a method by changing the return type. When we override a parent class method, the name, argument types and return type of the overriding method in child class has to be exactly same as that of parent class method. Overriding method was said to be invariant with respect to return type. Java 5.0 onwards it is possible to have different return type for a overriding method in child class, but child’s return type should be sub-type of parent’s return type. Overriding method becomes variant with respect to return type. Co-variant return type is based on Liskov substitution principle . Below is the simple example to understand the co-variant return type with method overriding. Output: Note : If we swap return types of Base and Derived, then above program would not work See the below example : Output:  Advantages: 1) It helps to avoid confusing type casts present in the class hierarchy and thus making the code rea