Monday 24 April 2017

Oops Concepts and its use in Objective C

Object Oriented Programming Concepts:


1) Object

It's real time entity. Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical.

2) Class

Collection of objects is called class. It is a logical entity.


3) Abstraction
Do we thing car as a component which made of different part??
No, we think it as a well designed object which has its unique individual behaviour.

We don't know the internal mechanism of car how it works. From the outside, the car is a single object. Once inside, you see that the car consists of several subsystems: steering, brakes, sound system, seat belts, heating, cellular phone, and so on. In turn, each of these subsystems is made up of more specialized units. For instance, the sound system consists of a radio, a CD player, and/or a tape or MP3 player. The point is that you manage the complexity of the car through the use of hierarchical abstractions.

Hiding internal details and showing functionality is known as abstraction.

- Abstract class can't be instantiated.
- If you implement one or more methods as abstract you have to define that class as abstract class
- You cannot create abstract constructor and abstract static methods.
Any subclass of an abstract class must either implement all of the abstract methods in the superclass, or be declared abstract itself.
- You cannot create object but you can use to create object reference 

Example 1:



// A Simple demonstration of abstract.
abstract class A {
 abstract void callme();
 // concrete methods are still allowed in abstract classes
 void callmetoo() {
 System.out.println("This is a concrete method.");
 }
}
class B extends A {
 void callme() {
 System.out.println("B's implementation of callme.");
 }
}
class AbstractDemo {
 public static void main(String args[]) {
 B b = new B();
 b.callme();
 b.callmetoo();
 }
}
// Using abstract methods and classes.
abstract class Figure {
 double dim1;
 double dim2;
 Figure(double a, double b) {
 dim1 = a;
 dim2 = b;
 }
 // area is now an abstract method
 abstract double area();
}
class Rectangle extends Figure {
 Rectangle(double a, double b) {
 super(a, b);
 }
 // override area for rectangle
 double area() {
 System.out.println("Inside Area for Rectangle.");
 return dim1 * dim2;
 }
}
class Triangle extends Figure {
 Triangle(double a, double b) {
 super(a, b);
 }
 // override area for right triangle
 double area() {
 System.out.println("Inside Area for Triangle.");
 return dim1 * dim2 / 2;
 }
}
class AbstractAreas {
 public static void main(String args[]) {
 // Figure f = new Figure(10, 10); // illegal now
 Rectangle r = new Rectangle(9, 5);
 Triangle t = new Triangle(10, 8);
 Figure figref; // this is OK, no object is created
 figref = r;
 System.out.println("Area is " + figref.area());
 figref = t;
08-ch08.indd 183 14/02/14 4:49 PM
CompRef_2010 / Java The Complete Reference, Ninth Edition /Schildt / 007180 855-8
184 PART I The Java Language
 System.out.println("Area is " + figref.area());
 }
}
In second example we create reference.

4) Inheritance:

It is the property by which one object acquires the property of another object.
It used for runtime polymorphism.

There are types of Inheritance.

Single level inheritance

Class A{
}

Class B: public A{
}

Multi level inheritance

Class A{
}

Class B: public A{

}

Class C: public B{


}

Objective-C allows only multilevel inheritance, i.e., it can have only one base class but allows multilevel inheritance. All classes in Objective-C is derived from the superclass NSObject.

Multiple inheritance

Class A{
}

Class B{

}

Class C: public A, public b{


}

Objective C doesn't support Multiple inheritance

Hierarchical inheritance

Class A{
}

Class B: public A{

}

Class C: public A{


}

Hybrid inheritance

Class A{
}

Class B{

}

Class C: public A, public B{



}

Class D: public A{


}

5) Encapsulation:

wrapping of data and code together  in a single unit is called  Encapsulation.

The very good example of encapsulation is class.

    class Box
    {
       public:
          double getVolume(void)
          {
             return length * breadth * height;
          }
       private:
          double length;      // Length of a box
          double breadth;     // Breadth of a box
          double height;      // Height of a box
    };

6) Polymorphism:

There are two types of polymorphism.

Runtime(Dynamic) - Method overriding


Method overriding is a perfect example of  runtime polymorphism. In this kind of polymorphism, reference of class X  can hold object of class X or an object of any sub classes of class X. For e.g. if class Yextends class X then both of the following statements are valid:

Y obj = new Y();
//Parent class reference can be assigned to child object
X obj = new Y();

Compile time - Method overloading
Compile time polymorphism is nothing but the method overloading in java. In simple terms we can say that a class can have more than one methods with same name but with different number of arguments or different types of arguments or both. To  know more about it refer method overloading in java.