Monday, March 4, 2013

Object oriented programming concepts (part 1)

1. Program to an interface, not an implementation
This literally means program to a supertype i.e. the declared type of the variables should be a supertype, usually an abstract class or interface, so that a particular variable holding interface object can have any type during runtime and our code should not be limited to any particular data type. Using this concept, we can exploit the polymorphism in OOPS.

2. Is Java, pass by value or pass by reference?
Java is pass by value but in true sense, it is passing value of references of objects.
More details: stackoverflow/is-java-pass-by-reference

3. Primitive data types are passed by value while non primitive types (like class, interface, string) are passed by reference.Java only supports pass by value. With objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same object .

4. Encapsulation and Abstraction
"encapsulation is concrete, abstraction is...abstract! ;-) encapsulation is an object you can use, abstraction is an ideal you can only discuss. encapsulation is why you wear underwear, abstraction is how you explain the difference between underwear and swimsuits". By a stackoverflow user comment

5. Abstract class vs Interface
No implementation in interface however abstract class have both implementation as well as declarations. Total Abstract class is almost similar to interface with an exception that abstract class can inherit only one super class however interface can inherit as much number of interface as required. Abstract class does not have instance.
a) Interface
Interface cannot be final.
Interface can have variables but they must be static, public, final which means interface can declare only constants.
An interface can extend only other interfaces.
Interface can't be instantiated.
Interface methods could be only public and abstract. No other modifier permitted.
All methods of interface are abstract methods.
Adding a new method necessitate to modify subclasses.
Cannot contain Constructor.
Interfaces are somewhat slow as they require method finding process before calling actual method
Used when something in design changes more frequently.

b) Abstract Class
Abstract class can provide complete default functionality or details to be overridden.
Contains both implementation methods as well as abstract methods.
In a class if there is even a one abstract method, whole class should be declared as abstract class.
Cannot mark a class as both abstract and final
It cannot be instantiated.
Abstract class could be public, private or protected.
Adding a new method does not necessitate to modify subclasses.
Can contain CONSTRUCTOR.
Used when various implementation are of the same kind and use common behavior.
A non abstract method can access an abstract method.

6.  COMPOSITION
"Imagine a software firm that is composed of different Business Units (or departments) like Storage BU, Networking BU. Automobile BU. The life time of these Business Units is governed by the lifetime of the organization. In other words, these Business Units cannot exist independently without the firm. The firm is COMPOSED OF business units."

7. ASSOCIATION
"The software firm may have external caterers serving food to the employees. These caterers are NOT PART OF the firm. However, they are ASSOCIATED with the firm. The caterers can exist even if our software firm is closed down. They may serve another firm! Thus the lifetime of caterers is not governed by the lifetime of the software firm."

8. AGGREGATION
"Consider a Car manufacturing unit. We can think of Car as a whole entity and Car Wheel as part of the Car. (at this point, it may look like composition..hold on) The wheel can be created weeks ahead of time, and it can sit in a warehouse before being placed on a car during assembly. In this example, the Wheel class's instance clearly lives independently of the Car class's instance. Thus, unlike composition, in aggregation, life cycles of the objects involved are not tightly coupled."
Credits: http://stackoverflow.com/questions/731802/what-is-the-difference-between-composition-and-association-relationship

9. Overloading
a) MUST change the argument list
b) CAN change the return type
c) CAN change the access modifier
d) CAN declare new or broader checked exceptions
e) A method can be overloaded in the same class or in a subclass

10 Overriding
a)The overriding method cannot have a more restrictive access modifier than the method being overridden (Ex:  Cannot override a method marked public and make it protected).
b) Cannot override a final method.
c) Cannot override a static method.
To prevent a method from getting over ridden use "final" keyword.

11 Constructor
a) Constructor cannot be inherited and they do not have any return type.
b) Cannot be final, abstract, static in comparison to methods
c) "this" keyword is used to access constructor in the same class. "Super" keyword is used to access constructor in super class.

No comments:

Post a Comment