Thursday, May 19, 2011

Some real world java examples

Just thought about some real world examples.

1. Polymorphism:

In simple terms polymorphism means one interface - multiple implementations. Users can choose the
implementation based on their own interests. For example Vehicle could be the interface and travel
is the method. There could be n number of implementations for Vehicle interface. i.e Van, Car, Bus etc.
Uses can choose their own Vehicle implementation based on their need i.e capacity, cost etc. Polymorphism can
be used for grouping.

2. Inheritance:

In object oriented programming, users can map real world entities into java objects. Through inheritance
parent object's properties are passed to children object properties. This reduces the amount of code
to be written and also increases the maintainability of the code. For example employee is the class that could
have details as name, id, start date of employment, address etc. Also Lead is also an employee but he has
more details as module he owns, review permission etc. Manager is also an employee who has additional responsibilites
like performance review etc., along with the default employee details.

3. Encapsulation:

Encapsulation means providing right access details for the right entities. For example in a hotel,customers are not allowed
inside cooking room. Servers are not allowed inside owner's cabin.

4. Queue:

Queue means first in first out. Railway booking counters.

4. Stack:

Stack means last in first out. Assume a movie theatre has 1000 seats which has only one gate in which people are allowed based on the seat number. Once the
show is over person who sat in 1000th seat, can come out as a first person.

5. Deque:

Deque means addition/removal is supported on both ends. Same theatre example could be applied here where it has two gates. One front gate and one back gate, visitors are allowed in both the gates.

No comments:

Post a Comment