OOP

  Home  Computer Programming  OOP


“Object-oriented programming (OOP) is a programming paradigm, In this OOP Interview Questions and Answers you will learn that OOP uses "objects" – data structures consisting of datafields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as information hiding, data abstraction, encapsulation, modularity, polymorphism, and inheritance. Learn OOP by OOP Interview Questions and Answers”



37 OOP Questions And Answers

1⟩ When to use Interface over abstract class?

Abstract Classes: Classes which cannot be instantiated. This means one cannot make a object of this class or in other way cannot create object by saying ClassAbs abs = new ClassAbs(); where ClassAbs is abstract class.

Abstract classes contains have one or more abstarct methods, ie method body only no implementation.

Interfaces: These are same as abstract classes only difference is we can only define method definition and no implementation.

When to use wot depends on various reasons. One being design choice.

One reason for using abstarct classes is we can code common

functionality and force our developer to use it. I can have a complete

class but I can still mark the class as abstract.

Developing by interface helps in object based communication.

 232 views

2⟩ What is Abstract Class?

Abstract class is a class that can not be instantiated, it exists extensively for inheritance and it must be inherited. There are scenarios in which it is useful to define classes that is not intended to instantiate; because such classes normally are used as base-classes in inheritance hierarchies, we call such classes abstract classes.

Abstract classes cannot be used to instantiate objects; because abstract classes are incomplete, it may contain only definition of the properties or methods and derived classes that inherit this implements it's properties or methods.

Static, Value Types & interface doesn't support abstract modifiers. Static members cannot be abstract. Classes with abstract member must also be abstract.

 227 views

3⟩ What is Sealed modifiers?

Sealed types cannot be inherited & are concrete.

Sealed modifiers can also be applied to instance methods, properties, events & indexes. It can't be applied to static members.

Sealed members are allowed in sealed and non-sealed classes.

 226 views

5⟩ What is Polymorphisms?

Polymorphism means one interface and many forms. Polymorphism is a characteristics of being able to assign a different meaning or usage to something in different contexts specifically to allow an entity such as a variable, a function or an object to have more than one form.

There are two types of Polymorphism.

Compile time: function or operator overloading

Runtime: Inheritence & virtual functions

 224 views

7⟩ What are Constructors?

Constructors are used for initializing the members of a class whenever an object is created with the default values for initialization.

If no constructor defined then the CLR will provide an implicit constructor which is called as Default Constructor.

A class can have any number of constructors provided they vary with the number of arguments that are passed, which is they should have different signatures.

Constructors do not return a value

Constructors can be overloaded

 240 views

8⟩ What are the various types of Constructor

Public : Accessible to All

Private: Those classes in which only static members are there and you don't want there objects to be created in any class.

Static: Used for initializing only the static members of the class. These will be invoked for the very first time the class is being loaded on the memory. They cannot accept any arguments. Static Constructors cannot have any access modifiers.

Intern: implementations of the abstract class to the assembly defining the class. A class containing an internal constructor cannot be instantiated outside of the assembly (Namespace).

and External

 218 views

9⟩ When to Use Abstract Classes and When Interfaces.

If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.

If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.

If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.

If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.

 223 views

10⟩ What is Public access modifier in C#?

The public keyword is an access modifier for types and type members ie. we can declare a class or its member (functions or methods) as Public. There are no restrictions on accessing public members.

 222 views

11⟩ What is pure virtual function in OOP?

When you define only function prototype in a base class without and do the complete implementation in derived class. This base class is called abstract class and client won’t able to instantiate an object using this base class.

A pure virtual function is a function that must be overridden in a derived class and need not be defined. A virtual function is declared to be "pure" using the curious "=0"

syntax:

class Base {

public:

void f1(); // not virtual

virtual void f2(); // virtual, not pure

virtual void f3() = 0; // pure virtual

};

 246 views

13⟩ What is Internal access modifier in C#?

The internal keyword is an access modifier for types and type members ie. we can declare a class as internal or its member as internal. Internal members are accessible only within files in the same assembly (.dll). In other words, access is limited exclusively to classes defined within the current project assembly.

 204 views

14⟩ What is Protected access modifier in C#?

The protected keyword is a member access modifier. It can only be used in a declaring a function or method not in the class ie. a class can't be declared as protected class.

A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declare this member. In other words access is limited to within the class definition and any class that inherits from the class

A protected member of a base class is accessible in a derived class only if the access takes place through the derived class type.

 230 views

15⟩ What is Private access modifier in C#?

The private keyword is a member access modifier ie. we can't explicitly declare a class as Private, however if do not specify any access modifier to the class, its scope will be assumed as Private. Private access is the least permissive access level of all access modifiers.

Private members are accessible only within the body of the class or the struct in which they are declared. This is the default access modifier for the class declaration.

 215 views

17⟩ Default Access modifiers in C#?

An enum has default modifier as public

A class has default modifiers as private . It can declare members (methods etc) with following access modifiers:

public

protected

internal

private

protected internal

An interface has default modifier as public

A struct has default modifier as private and it can declare its members (methods etc) with following access modifiers:

public

internal

private

 249 views

18⟩ What is Protected Internal access modifier in C#?

Protected Internal is a access modifiers for the members (methods or functions) ie. you can't declare a class as protected internal explicitly. The members access is limited to the current assembly or types derived from the containing class.

Protected Internal means the method is accessible by anything that can access the protected method UNION with anything that can access the internal method.

 218 views

20⟩ What is Polymorphism?

In OPP’S, polymorphism(Greek meaning “having multiple forms”) is the ablity of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a a function, or an object to have more than one forms.

In C# :

Parent classes may define and implement “virtual” methods(Which is done using the “virtual” keyword), and derived classes can override them(using the “override” keyword), which means they provide their own definition and implementation.At run-time, when user’s code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. Thus in your source code when a method of the base class is called it executes the overriden method.

 205 views