Thursday, March 12, 2015

OOP principles in Java


Encapsulation is the mechanism that binds together code and data and keeps both safe from the outside interference and misuse.
The technique is, making the fields in a class as private. if we declare a variable as private than anyone can't accessed the fields outside the class.
The advantage of encapsulation is, the users can't break the code. so It gives maintainability and flexibility of code.


In OOP concepts, we define everything as objects(eg: dog,cat,human) . Each objects have behaviours and attributes. There is some relationships among objects with their behaviours and attributes.
Inheritance is a process which one object acquires the properties of another object. sub classes acquire some attributes of  the super class. For a example animal is a super class of mammals and mammal is a super class of dog.

In inheritance, we use the keywords extends and implements.

extends : when we inherit a class with a class this keyword can be used.
For example:
public class mammal{}
public class dog extends mammal{}
In this case, all the properties of super class will be able to inherit with the super class except the private properties of the super class.

implements : This keyword is used when a class inherit with a interface.
For a example:
public interface mammal{}
public class dog implements mammal{}