OOP Job Interview Questions And Answers.
Question - 1:
Can we call a base class method without creating instance?
Ans:
Yep. But ..
* Its possible If its a static method.
* Its possible by inheriting from that class also.
* Its possible from derived classes using base keyword.
Question - 2:
In which cases you use override and new base?
Ans:
Use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and
modify it with the new modifier.
Question - 3:
What is a private constructor? Where will you use it?
Ans:
When you declare a Constructor with Private access modifier then it is called Private Constructor. We can use the private constructor in singleton pattern.
If you declare a Constructor as private then it doesn't allow to create object for its derived class, i.e you loose inherent facility for that class.
Example:
Class A
{
// some code
Private Void A()
{
//Private Constructor
}
}
Class B:A
{
//code
}
B obj = new B();// will give Compilation Error
Because Class A constructor declared as private hence its accessibility limit is to that class only, Class B can't access. When we create an object for Class B that
constructor will call constructor A but class B have no rights to access the Class A constructor hence we will get compilation error.
Question - 4:
Can we declare private class in a Namespace?
Ans:
No. If you try to create a private class in a Namespace, Compiler will throw a compile time error "Namespace elements cannot be explicitly declared as private,
protected, or protected internal".
Reason: The message says it all. Classes can only be declared as private, protected or protected internal when declared as nested classes, other than that, it doesn't
make sense to declare a class with a visibility that makes it unusable, even in the same module. Top level classes cannot be private, they are "internal" by default, and
you can just make them public to make them visible from outside your DLL.