|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Object oriented design |
| 4 | +--- |
| 5 | + |
| 6 | +### Encapsulation |
| 7 | + |
| 8 | +Suppose you want the variable `small` to be always $$1/3^{rd}$$ of `big`. |
| 9 | + |
| 10 | +But in the below code as the variable `small` is `public`, any code can access it and assign any arbitrary value |
| 11 | +thus violating the requirement of `small` being $$1/3^{rd}$$ of `big`. This happens __due to lack of encapsulation__. |
| 12 | + |
| 13 | +{% highlight java linenos %} |
| 14 | +class Foo { |
| 15 | + public int big = 9; |
| 16 | + public int small = 3; |
| 17 | + public void setBig(int num) { |
| 18 | + big = num; |
| 19 | + small = num/3; |
| 20 | + } |
| 21 | + // other code here |
| 22 | +} |
| 23 | +{% endhighlight %} |
| 24 | + |
| 25 | +The problem can be solved by changing the access modifier of `small` to `private` so that each and every code |
| 26 | +will go through `setBig()` method to set the value of `big` and `small`. This is nothing but a small __practical |
| 27 | +example of encapsulation__. Here we encapsulated the variable `small` by making it private and providing _getters_ |
| 28 | +and _setters_ for code to access it. _(We should follow this practice always for all variables)_ |
| 29 | + |
| 30 | +### Inheritance |
| 31 | + |
| 32 | +A class inheriting public/protected properties of another class by using the keyword `extends` is called _inheritance |
| 33 | +in java_. |
| 34 | + |
| 35 | +The two most common __reasons to use inheritance__: |
| 36 | + |
| 37 | +* To promote code reuse. |
| 38 | +* To use polymorphism. |
| 39 | + |
| 40 | +### IS-A relationship |
| 41 | + |
| 42 | +In OO, the concept of _IS-A_ is based on __class inheritance or interface implementation__. _IS-A_ is a way of saying, |
| 43 | +"this thing is a type of that thing." |
| 44 | + |
| 45 | +For example in the below figure: |
| 46 | + |
| 47 | +{% img /img/posts/IS-A.png 150x250 %} |
| 48 | + |
| 49 | +_"Car extends Vehicle" means "Car IS-A Vehicle."<br/> |
| 50 | +"Subaru extends Car" means "Subaru IS-A Car."_ |
| 51 | + |
| 52 | +### HAS-A relationship |
| 53 | + |
| 54 | +_HAS-A_ relationships are _based on usage, rather than inheritance_. In other words, class A _HAS-A_ B if code in class A has |
| 55 | +a reference to an instance of class B. For example, you can say the following, A Horse _IS-A_ Animal. A Horse _HAS-A_ Halter. |
| 56 | +The code might look like this: |
| 57 | + |
| 58 | +{% highlight java linenos %} |
| 59 | + |
| 60 | +public class Animal { } |
| 61 | + |
| 62 | +public class Horse extends Animal { |
| 63 | + private Halter myHalter; |
| 64 | +} |
| 65 | + |
| 66 | +{% endhighlight %} |
| 67 | + |
| 68 | +### Polymorphism |
| 69 | + |
| 70 | +Any Java object that can pass more than one _IS-A_ test can be considered __polymorphic__. Other than objects of type `Object`, |
| 71 | +all Java objects are polymorphic in that they pass the _IS-A_ test for their own type and for class `Object`. |
| 72 | + |
| 73 | +The only way to access an object is through a reference variable: |
| 74 | + |
| 75 | +* A reference variable can be of only one type, and once declared, that type can never be changed (although the object it |
| 76 | +references can change provided its not declared `final`). |
| 77 | +* A reference variable's type determines the methods that can be invoked on the object the variable is referencing. |
| 78 | +* A reference variable can refer to any object of the same type as the declared reference, or __to any subtype of the |
| 79 | +declared type__. |
| 80 | +* A reference variable can be declared as a class type or an interface type. If the variable is declared as an interface |
| 81 | +type, it can reference any object of any class that implements the interface. |
| 82 | + |
| 83 | +More on polymorphism in [Overriding](/2015/05/29/overriding.html) & Overloading in Java. |
0 commit comments