OOP Interview Questions

What is Encapsulation in OOPS?

  • Encapsulation is one of the fundamental principles of object-oriented programming.
  • Encapsulation is a process of hiding all the internal details of an object from the outside world
  • Encapsulation is the ability to hide its data and methods from outside the world and only expose data and methods that are required
  • Encapsulation is a protective barrier that prevents the code and data being randomly accessed by other code or by outside the class
  • Encapsulation gives us maintainability, flexibility and extensibility to our code.
  • Encapsulation makes implementation inaccessible to other parts of the program and protect from whatever actions might be taken outside the function or class.
  • Encapsulation provides a way to protect data from accidental corruption
  • Encapsulation hides information within an object
  • Encapsulation is the technique or process of making the fields in a class private and providing access to the fields using public methods
  • Encapsulation gives you the ability to validate the values before the object user change or obtain the value
  • Encapsulation allows us to create a “black box” and protects an objects internal state from corruption by its clients

What is Inheritance in OOPS?

  • Inheritance, together with encapsulation and polymorphism, is one of the three primary characteristics (concept) of object-oriented programming
  • Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes
  • The Class whose methods and variables are defined is called super class or base class
  • The Class that inherits methods and variables are defined is called sub class or derived class
  • Sometimes base class known as generalized class and derived class known as specialized class
  • Keyword to declare inheritance is “:” (colon) in visual c#

Benefits of using Inheritance

  • Once a behavior (method) or property is defined in a super class(base class),that behavior or property is automatically inherited by all subclasses (derived class).
  • Code reusability increased through inheritance
  • Inheritance provide a clear model structure which is easy to understand without much complexity
  • Using inheritance, classes become grouped together in a hierarchical tree structure
  • Code are easy to manage and divided into parent and child classes

What is Polymorphism in OOPS?

  • Polymorphism is one of the primary characteristics (concept) of object-oriented programming
  • Poly means many and morph means form. Thus, polymorphism refers to being able to use many forms of a type without regard to the details
  • Polymorphism is the characteristic of being able to assign a different meaning specifically, to allow an entity such as a variable, a function, or an object to have more than one form
  • Polymorphism is the ability to process objects differently depending on their data types
  • Polymorphism is the ability to redefine methods for derived classes.

Types of Polymorphism

  • Compile time Polymorphism
    •  Compile time Polymorphism also known as method overloading
  • Run time Polymorphism
    • Run time Polymorphism also known as method overriding

What is Access Modifier in OOPS?

Access modifiers determine the extent to which a variable or method can be accessed from another class or object

The following five accessibility levels can be specified using the access modifiers
• Private
• Protected
• Internal
• Protected internal
• Public

What is Abstract Class?

An abstract class is a class that cannot be instantiated and is always used as a base class.

The following are the characteristics of an abstract class:

  • You cannot instantiate an abstract class directly. This implies that you cannot create an object of the abstract class; it must be inherited.
  • You can have abstract as well as non-abstract members in an abstract class.
  • You must declare at least one abstract method in the abstract class.
  • An abstract class is always public.
  • An abstract class is declared using the abstract keyword.

The basic purpose of an abstract class is to provide a common definition of the base class that multiple derived classes can share. Static, Value Types & interface doesn’t support abstract modifiers. Static members cannot be abstract. Classes with abstract member must also be abstract.

 

What is Instantiation?

When you make a new instance the process is called instantiation and is typically done using the new keyword. A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class, if that class is not abstract By default, C++ matches a function call with the correct function definition at compile time. This is called static binding. You can specify that the compiler match a function call with the correct function definition at run time; this is called dynamic binding. You declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific function.

Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object. Static classes can be used when there is no data or behavior in the class that depends on object identity. When applied to a class, the sealed modifier prevents other classes from inheriting from it. In the following example, class B inherits from class A, but no class can inherit from class B.

class A {}
sealed class B : A {}

You can also use the sealed modifier on a method or property that overrides a virtual method or property in a base class. This enables you to allow classes to derive from your class and prevent them from overriding specific virtual methods or properties.

Delegate:Any method that matches the delegate’s signature, which consists of the return type and parameters, can be assigned to the delegate. This makes is possible to programmatically change method calls, and also plug new code into existing classes. As long as you know the delegate’s signature, you can assign your own delegated method.
Composition is “Has a” relation (objects contained in it destroys when the main object is destroyed)
Aggregation is also “Has a” relation but objects contained is it .. has their own identity

Usually, when using composition, the object instantiates the objects it has. Look at the Chair class above. You can see that Back , Seat , and Leg are instantiated in the Chair class. When using aggregation, the object does not instantiate the objects it has. Look at the code below. The DinnerChair class has a Person , but it does not instantiate Person .

Overriding is in Inheritance while Overloading is same function name with different signature (in the same scope or in derived class).
The signature of a method is the combination of the method’s name along with the number and types of the parameters (and their order).

Is-a relationship is called inheritance i.e. Pepperoni is a pizza
Has-a relationship defined already where One objects contains other objects.

This is object-oriented programming and UML terminology, not Java-specific. There are actually three cases you should be aware of:
1. A House is a Building (inheritance);
2. A House has a Room (composition);
3. A House has an occupant (aggregation).

The difference between (2) and (3) is subtle yet important to differentiate. Together they are forms of association. What’s the difference? Composition implies the child object cannot live out of the context of the parent (destroy the house and rooms disappear) whereas aggregation implies the child can exist on its own (destroy the house and the occupant goes elsewhere).

Can you specify the accessibility modifier for methods inside the interface?
All the methods inside an interface are always public, by default. You cannot specify any other access modifier for them.

Is it possible for a class to inherit the constructor of its base class?
No, a class cannot inherit the constructor of its base class.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s