0% found this document useful (0 votes)
8 views14 pages

Things You Might Miss

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views14 pages

Things You Might Miss

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Things you might

miss
Interview is not a Question-
Answer thing, it’s a
Discussion
- Use your Interviewer’s brain as much as
possible
- Interviewer is looking for a coworker with
whom he can be free, discuss anything
Encapsulation
vs
Abstraction
- Encapsulation is about disallowing access or
knowledge to internal structures
- Abstraction is about separating interface from
implementation
class DataStore{
List<Int> numbersList;
public DataStore(){
numbersList = new ArrayList<Int>();
}
public List getNumbersList(){
return numbersList;
}
public void setNumbersList(List<Int> list){
numbersList = list;
}
}

This is breaking encapsulation due to usage of getters and setters


class DataStore{
private List<Int> numbersList;
public DataStore(){
numbersList = new ArrayList<Int>();
}
public int getNumber(int index){
return numbersList.get(index);
}
public boolean addNumber(int numberToAdd){
if(isNumbercanBeAdded()){
numbersList.add(int);
return true;
}
return false;
}

See how
- Encapsulation is done by disallowing access to underlying List
- Abstraction is done with get and add methods
Inheritance
vs
Composition
- Use it based on logical relationship between
classes : Inheritance when is-a and
Composition when has-a.
- Don’t simply use Inheritance for code-reuse or
polymorphism
Abstract Class
vs
Interface
- Whenever there is hierarchy use class
- For interfaces for with the behaviour that can
applied anywhere
- E.g. comparable, movable
Primitive Obsession
- "Just a field for storing some data!" the
programmer says
- One field adds up every time and creating bad
smells
- Try grouping them and there borns your
object
Objects
- OOP code is not only having classes
- An object should have behaviour
- Remember “Object = state + behaviour”
Design a Parking Lot
- Vague question

- Always start by clarifying your questions or


assumptions you are going to make
- One way to think about it is,

class ParkingLot class Car


- int slots - int number
- Map<ParkingTicket,Car> - int size
parkedCarMap

- ParkingTicket park(Car car) throws


NoParkingSlotAvailableException,
CarAlreadyParkedException class ParkingTicket
- boolean unpark(ParkingTicket - int ticketNumber
parkingTicket) throws - int carNumber
CarNotParkedException
- int generateTicketNumber()
- One could design hierarchy around Vehicles
and Parking Spots needed
class Vehicle class Bus extends Vehicle
- ArrayList<ParkingSpot> parkingSpots - Bus() {
- int spotsNeeded spotsNeeded = 10
- int licensePlate size = VehicleSize.large
- int size }
- boolean
- int getSpotsNeeded() canFitInSpot(ParkingSpot
- int getSize() spot)
- void parkInSpot()
- void clearSpots()
- abstract boolean
canFitInSpot(ParkingSpot spot)
- One could build their code around Parking
Spots and Parking Lot
class ParkingSpot class ParkingLot
- Vehicle vehicle - ParkingSpot [] spots
- int size - int availableSpots
- int location
- boolean parkVehicle(Vehicle vehicle)
- boolean isAvailable() - int findAvailableSpots(Vehicle vehicle)
- boolean canFitVehicle() - void freeUpSpots()
- boolean park()
- void unpark()
Links to look out for
(Look at slide/speaker notes section for references)

- http://martinfowler.com/tags/clean%20code.html
- https://www.cs.helsinki.fi/u/luontola/tdd-2009/ext/ObjectCalisthenics.pdf
- http://ptgmedia.pearsoncmg.com/images/9780132928472/samplepages/0132928477.pdf
-

You might also like