Fundamental of Object-Oriented Programming


1. Object – entity in the real world distinctly identified i.e. a student, a loan, a circle etc.

2. Object has unique identity, state (set of data fields), and behaviours (set of methods).
3. Classes – constructs that define objects of the same type.
4. A class is a blueprint that defines what an object’s data and methods will be. An object is an instance of a class. You can create many instances of a class (instantiation).
5. A class provide special method known as constructors, which are invoked to construct objects from the class.
6. Constructor can perform any action, but constructors are designed to perform initializing actions (i.e. initializing the data fields of objects)


class Circle {
     /** The radius of this circle */
     double radius = 1.0;                 //Data field 
     /** Construct a circle object */                 //1st Constructor (no-arg)
     Circle() {                                      //i.e. new Circle() creates an object of the
       }                                                    //Circle class using this constructor
      /** Construct a circle object */       //2nd Constructor
      Circle(double newRadius) {                  //new Circle(5.0) creates an
  radius = newRadius;                                  //object using this constructor
}
/** Return the area of this circle */
double getArea() {       //Method
  return radius * radius * 3.14159;
}
 }
7. Constructor with no parameters is referred to as a no-arg constructor.
8. Constructors must have the same name as the class itself.
9. Constructors do not have a return type—not even void.
10. Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.
11. A class may be declared without constructors. In this case, a no-arg constructor with an empty body is implicitly declared in the class. This constructor, called a default constructor, is provided automatically only if no constructors are explicitly declared in the class.
12. (Common mistakes) If void keyword is located in front of a constructor, 
      i.e. public void Circle(){
         }
      In this case, Circle() is method, not a constructor.

No comments:

Post a Comment