Wednesday, May 7, 2014

Introduction to OOP concepts

Q. What is a class?

"A dog is an animal."

"Animals make different sounds."

"A dog barks."

"My dog's name is Abby."

Consider this in programming terms. 

"Everything is an object"

"Each object has a category (or class in programming terms)"

"A dog is an (object of class) Animal."

"The Animal class has many subcategories (of which Dog is one)."

"The Animal class itself can be said to be a subclass of Object."
This is called singly rooted heirarchy (will be covered later)

"Each object of class Animal makes sounds differently. For example, dogs bark. Cats say meow"
This is called polymorphism

"Each class (i.e. category) of objects has some particular behavior(s) and properties associated with it."

Some classic examples of OOP concepts: 

  1. Consider a car. How the engine works (say) is not known to the driver. The mechanics behind the workings of the car are hidden from the driver. If the car was to break down, only the mechanic at the workshop knew what went wrong and only he could fix it. NOT the driver. This is called abstraction and encapsulation.
  2. A smartphone has a camera. This is called HAS-A relationship. It is commonly called composition.
  3. A dog is an animal. This is called IS-A relationship. It is called inheritance in object oriented programming.
  4.  A shape can be of any type - a triangle, square, hexagon etc. However the method of calculation of area is different for each shape. Thus the area of a shape is said to be calculated dynamically i.e. polymorphically. 

No comments :

Post a Comment