Thursday, June 3, 2010

Object oriented concepts

  1. When do you say a programming language is Object oriented?
    1. A language is called object oriented language if it satisfies the following object oriented concepts (OOPS concepts)

i. Abstraction: binding data and methods (behavior) together. Example: Class binds data and methods

ii. Encapsulation: Hiding the data. Access specifiers like public, protected, private, internal are access specifiers

iii. Inheritance: A concept used to create a new class from an existing class there by get the behavior and properties of the base/parent class

iv. Polymorphism: Behaving differently at run time based on the object the behavior is invoked. There are 3 ways we can have polymorphic behavior:

1. Method overriding

2. Method Overloading

3. Operator overloading

  1. Example for Inheritance
    1. Animal is a base class
    2. Tiger, Cow are derived classes
    3. Shout() method on cow says baaa…..
    4. Shout() method on Tiger says grrrrr…..
    5. Both are animals but have different characteristics.
  2. Example for polymorphism
    1. Same as above
  3. What is method overriding?
    1. Overriding means giving a defining the base class method again in the derived class. The base class method must be declared as virtual
    2. In the derived class override (C#) key word should be used to override the method.
  4. What is method overloading?
    1. A method is said to be overloaded when the method signature is different with same name. A signature is different if one of the following is true

i. Number of parameters are different

ii. Sequence of parameters different

iii. Data type of any one parameter is different

    1. Two methods having different return types will not be considered as overloaded
  1. What is the difference between class and an object?
    1. Object is an instance of a class
    2. Class represents a classification, or class is an abstract representation of an object or group or a thing
  2. What is the difference between Abstract class and Interface?
    1. Abstract class should have at least one abstract method.
    2. Abstract class can contain member variables
    3. Abstract class can define methods
    4. Interface cannot have member variables and all the methods should be abstract
    5. An abstract method is nothing but a method declaration without method definition
  3. What is the difference between method declaration and definition?
    1. Declaration means just having the method signature. Like

i. {Return Type} {MethodName} ( {parameters} )


    1. Definition means defining the method.
  1. What is initialization?
    1. Means giving an initial value at the time of declaring the variable
  2. What is the difference between class and a struct?
    1. Class is a reference type and will be stored on heap.
    2. Struct is a value type and will be stored on stack
    3. Class can inherit other classes and implement interface
    4. Struct cannot inherit but can implement interfaces

  1. What is the significance of this keyword?
    1. Each object has a reference “this” which points to itself.
    2. Can be used to refer to the current object.
    3. Also be used by one constructor to explicitly invoke another constructor of the same class.
  2. In which sequence constructors will be called in case of derived classes?
    1. First base classes constructors will be called then derived class constructors will be called
  3. In which sequence destructors will be called in case of derived classes?
    1. First derived classes destructors will be called then base class destructors will be called
  4. What are Static Binding and Dynamic Binding?
    1. Static binding means giving a type to a variable at compile time. Runtime binding mans giving a type to the variable at runtime.
    2. Ex: int x – static biding, object x; x = “some string” --runtime binding

No comments:

Post a Comment