OOP

Aug 2, 2018


OOP

Core OOPS concepts are:

  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance
  • Association
  • Aggregation
  • Composition

Abstraction

  • hiding the internal details and describing things in simple terms.
Ways to achieve Abstraction

There are two ways to achieve abstraction in java

  • Abstract class (0 to 100%)
  • Interface (100%)

Real life Example of Abstract Class

abstract class Shape{  
abstract void draw();  
}  

  
class Rectangle extends Shape{  
void draw(){System.out.println("drawing rectangle");}  
}  

class Circle1 extends Shape{  
void draw(){System.out.println("drawing circle");}  
}  

  
class TestAbstraction1{  
public static void main(String args[]){  
Shape s=new Circle1();  
s.draw();  
s=new Rectangle();  
s.draw();  
}  
}  

Real Life Example of Inheritance

interface Animal {
   public void eat();
   public void travel();
}
public class Man implements Animal {

   public void eat() {
      System.out.println("Man eats");
   }

   public void travel() {
      System.out.println("Man travels");
   } 

   public int noOfLegs() {
      return 0;
   }

   public static void main(String args[]) {
      Man m = new Man();
      m.eat();
      m.travel();
   }
} 
public class Dog implements Animal {

   public void eat() {
      System.out.println("Dog eats");
   }

   public void travel() {
      System.out.println("Dog travels");
   } 

   public int noOfLegs() {
      return 0;
   }

   public static void main(String args[]) {
      Dog m = new Dog();
      m.eat();
      m.travel();
   }
} 

Association, Aggregation, and Composition in OOP

Association

is a relationship where all objects have their own lifecycle and there is no owner.

Let’s take an example of Teacher and Student. Multiple students can associate with single teacher and single student can associate with multiple teachers, but there is no ownership between the objects and both have their own lifecycle. Both can be created and deleted independently.

Aggregation

is a specialised form of Association where all objects have their own lifecycle, but there is ownership and child objects can not belong to another parent object.

Let’s take an example of Department and teacher. A single teacher can not belong to multiple departments, but if we delete the department, the teacher object will not be destroyed. We can think about it as a “has-a” relationship.

Composition

is again specialised form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object does not have its lifecycle and if parent object is deleted, all child objects will also be deleted.

Let’s take again an example of relationship between House and Rooms. House can contain multiple rooms - there is no independent life of room and any room can not belong to two different houses. If we delete the house - room will automatically be deleted.

Let’s take another example relationship between Questions and Options. Single questions can have multiple options and option can not belong to multiple questions. If we delete the questions, options will automatically be deleted.


OOP | Java Tutorial