OOP Programming


1. What is OOP?

Object Oriented Programming

2. Write basic concepts of OOP?

(I personally think this is a bad question to ask, but people do ask this kind of questions.)

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

3. What is a class?

A class is a definition of some thing, an instance (or object) is the actual thing of that definition. e.g. TV is a class, and my Samsung TV is my instance.

4. What is an object?

It is the actual instance of a class. e.g. Car is class, my Benz is an instance.

5. What is Encapsulation?

The Class can encapsulate the details of the logic.

6. What is Polymorphism?

class Employee { 
     public int getDiscount() { 
          return 5;
     }
}


class InternalEmployee : Employee { 
     public int getDiscount() { 
          return 10;
     }
 }

 class Contractor : Employee { 
      public int getDiscount() { 
          return 20;
     }
 }

Employee employee = new Employee();

employee.getDiscount(); // returns 5

Employee employee1 = new InternalEmployee();

employee1.getDiscount(); // returns 10

Employee employee2 = new ContractEmployee();

employee2.getDiscount(); // returns 20

As you see, although the variables have been declared with the same type Employee, and depends its actual initialisation, the same method will be executed differently.

So Polymorphism: Objects may behave differently depending on the “type” while keeping the same interface.

7. What is Inheritance?

One class can extend the features of the other Class, and the first class will be the sub class, usually it implies a more specific type of entity. e.g. Dog is subclass of Pet. Dog is more specific whereas Pet can contain some common features of Pet. This way when I create a class called Cat, I do not need to repeat everything Pet has already got.

9. Define a constructor?

Constructor is a method used to initialise of an object, and it gets invoked at the time of object creation.

The creation of object involves allocating the right size of the memory.

10. Define Destructor?

Destructor is a method which is automatically called when the object is destroyed. You can clean up things there if needed.

12. What is a virtual method?

Virtual method is a member method of class and its functionality can be overridden in its derived or subclass class.

13. What is overloading?

Same method name, but different number or type of parameters.

e.g.

void add(int a, int b);

void add(double a, double b);

void add(int a, int b, int c);

14. What is an abstract class?

  • An abstract class is a class which cannot be instantiated.
  • It will be inherited by specific type or class. (A type is a class, a class is a type)
  • An abstract class can contain only Abstract method.

15. What is a ternary operator?

condition ? expr1 : expr2 

If condition is true, expr1 is executed otherwise expr2 will be executed.

 

16. What is method overriding?

Method overriding is a feature that allows sub class to provide implementation of a method that is already defined in the main class. This will overrides the implementation in the super class by providing the same method name, same parameter and same return type.

17. What is an interface?

  • An interface is a contract.
  • An interface is a collection of abstract method. If the class implements an inheritance, and then thereby inherits all the abstract methods of an interface.

18.  What is exception handling?

Exception is an event that occurs during the execution of a program. Exceptions can be of any type – Run time exception, Error exceptions. Those exceptions are handled properly through exception handling mechanism like try, catch and throw keywords.

19. Difference between class and an object?

An object is an instance of a class. Objects hold any information , but classes don’t have any information. Definition of properties and functions can be done at class and can be used by the object. Class can have sub-classes, and an object doesn’t have sub-objects.

20. What is early and late binding?

Early binding refers to assignment of values to variables during design time whereas late binding refers to assignment of values to variables during run time.

Social Media


Categories



All Posts



Contact Info