Recent Posts

Tuesday, 19 July 2016

OOPs Concepts


1. Data Hiding
* The data should not go out directly i.e outside person is not allowed to access the data this is nothing but “Data Hiding”.

* The main advantage of data hiding is we can achieve security.

* By using private modifier we can achieve this.
E.g
class Datademo {
   private double amount;
   ………
}
* It is highly recommended to declare data members with private modifier.

2. Abstraction
* Hiding implementation details is nothing but abstraction. The main advantages of abstraction are we can achieve security as we are not highlighting internal implementation.

* Enhancement will become easy. With out effecting outside person we can change our internal implementation.

* It improves maintainability.

* By using interfaces and abstract classes we can achieve abstraction.

Note
* If we don’t know about implementation just we have to represent the specification then we should go for interface

* If we don’t know about complete implementation just we have partial implementation then we should go for abstract.

* If we know complete implementation and if we r ready to provide service then we should go for concrete class

3. Encapsulation
     If a class follows data hiding and abstraction such type of class is said to be ‘Encapsulated’ class.
Encapsulation = Data Hiding + Abstraction
E.g
class Account {
   private int balance;
   public void setBalance(int balance) {
      //validating the user & his permissions.
      this.balance = balance;
   }
   public int getBalance() {
      //validating the user and his permissions.
      return balance;
   }
}
     The data members we have to declared as private. So that outside person is not allowed to access to directly we have to provide Access to our data by defining setter and getter methods. i.e hiding data behind methods is the central concept of encapsulation.
     The main advantages of encapsulation are security, enhancement, maintainability

4. Tightly Encapsulated Class
     A class is said to be tightly encapsulated iff all the data members declared as private.
E.g
class student {
   private String name;
   public void setName(String name) {
      this.name = name;
   }
   public String getName() {
      return name;
   }
}
5. IS-A Relationship
* IS-A relationship also known as ‘inheritance’.

* By using extends keyword we can implement inheritance.

* The main advantage is reusability.
Note
     Parent class reference can be used to hold child class object but by using that reference we are not allowed to call child class specific methods.
E.g
class P {
   m1() {
   }
   m2() {
   }
}

class C extends P {
   m3() {
   }
}

class Test {
   public static void main(String arg[]) {
      P p1 = new P();
      p1.m1();
      p1.m2();
      p1.m3(); //C.E: Child class methods are not available to the parent class.

   C c1 = new C();
   c1.m1();
   c1.m2();
   c2.m3(); // Fine
   
   P p2 = new C();
   p2.m1();
   p2.m2();
   p2.m3(); //C.E: By using parent class reference we can’t call child class specific method
      
   C c4 = new P(); //C.E: Child class reference can’t be used to hold parent class object.
   }
}
Note
     Java won't provide support for multiple inheritance but through interfaces it is possible
class A extends B,C {
} // C.E

interface A extends B,C {
} // Fine
* Cycle inheritance is not allowed in java
E.g
class A extends B {
}
class B extends A {
}

class A extends A {
}
     Above both gives compile time error saying cyclic inheritance involving
6. HAS – A Relationship
* Also known as Composition or Aggregation .

* There is no specific keyword, but most of the cases we can implemented by using new keyword.

* Main advantage is reusability.
E.g
class engine {
   m1(){}
   m2(){}
    .
    .
    .
}
class car {
   engine e = new engine();
  .
  .
}
Class car has engine
     The main disadvantage of HAS – A relationship is it increases dependency b/w components and creates maintainability problems.

Composition Vs Aggregation
* In the case of composition whenever container object is destroyed all contained objects will be destroyed automatically. i.e., without existing container object there is no chance of existing contained object i.e., container and contained objects having strong association.
E.g
    University is composed of several departments. Whenever you are closing university automatically all departments will be closed. The relationship between university object and department object is strong association which is nothing but composition.

Aggregation
     Whenever container object destroyed, there is no guaranty of destruction of contained objects i.e., without existing container object there may be a chance of existing contained object. i.e., container objects just maintains references of contained objects. This relationship is called weak association which is nothing but Aggregation.
E.g
     Several professors will work in the department. Whenever we are closing the department still there may be a chance of existing professors. The relationship between department and professor is called weak association which is nothing but Aggregation.

7. Method Signature
     In java method signature consists of method name and arguments list( including order also).
public void m1(int i, float f) { 
}
Here m1(int i, float f) is method signature
     Compiler uses method signature to resolve method calls.
     Two methods with the same signature are not allowed in any java class, violation leads to compile time error.
class Test {
   public void m1(int i) {
   }
   public void m2() {
   }
   public int m1(int i) {
   }
   public void m1(int i, float f) {
   }
}
C.E: m1(int) is already defined in Test
     In java return type is not part of method signature.

Next Tutorial  OOPS Concepts Part 2

Previous Tutorial  Assertions Tutorial

No comments:

Post a Comment