- When do you say a programming language is Object oriented?
- 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
- Example for Inheritance
- Animal is a base class
- Tiger, Cow are derived classes
- Shout() method on cow says baaa…..
- Shout() method on Tiger says grrrrr…..
- Both are animals but have different characteristics.
- Example for polymorphism
- Same as above
- What is method overriding?
- Overriding means giving a defining the base class method again in the derived class. The base class method must be declared as virtual
- In the derived class override (C#) key word should be used to override the method.
- What is method overloading?
- 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
- Two methods having different return types will not be considered as overloaded
- What is the difference between class and an object?
- Object is an instance of a class
- Class represents a classification, or class is an abstract representation of an object or group or a thing
- What is the difference between Abstract class and Interface?
- Abstract class should have at least one abstract method.
- Abstract class can contain member variables
- Abstract class can define methods
- Interface cannot have member variables and all the methods should be abstract
- An abstract method is nothing but a method declaration without method definition
- What is the difference between method declaration and definition?
- Declaration means just having the method signature. Like
i. {Return Type} {MethodName} ( {parameters}
- Definition means defining the method.
- What is initialization?
- Means giving an initial value at the time of declaring the variable
- What is the difference between class and a struct?
- Class is a reference type and will be stored on heap.
- Struct is a value type and will be stored on stack
- Class can inherit other classes and implement interface
- Struct cannot inherit but can implement interfaces
- What is the significance of this keyword?
- Each object has a reference “this” which points to itself.
- Can be used to refer to the current object.
- Also be used by one constructor to explicitly invoke another constructor of the same class.
- In which sequence constructors will be called in case of derived classes?
- First base classes constructors will be called then derived class constructors will be called
- In which sequence destructors will be called in case of derived classes?
- First derived classes destructors will be called then base class destructors will be called
- What are Static Binding and Dynamic Binding?
- Static binding means giving a type to a variable at compile time. Runtime binding mans giving a type to the variable at runtime.
- Ex: int x – static biding, object x; x = “some string” --runtime binding
No comments:
Post a Comment