OOPS Interview Questions and Answers | Object Oriented Programming Interview Questions C#

Questpond
16 Jan 202258:12

Summary

TLDRThis video script offers an in-depth exploration of Object-Oriented Programming (OOP) concepts, focusing on interview questions and answers using C#. It emphasizes the importance of understanding OOP for IT professionals, covering key topics like classes, objects, abstraction, encapsulation, inheritance, polymorphism, and interfaces. The script provides practical examples and advice for interviews, highlighting the need for preparedness, clear and concise answers, and the use of real-world scenarios over clichéd examples.

Takeaways

  • 🎯 Object-oriented programming (OOP) concepts are crucial for IT interviews, regardless of the programming language you know.
  • 📝 Importance of OOP lies in its pillars: Abstraction, Encapsulation, Inheritance, and Polymorphism, which are essential for developers to understand.
  • 💡 Abstraction in OOP means representing real-world objects in code, which helps in organizing and managing code more effectively.
  • 🔍 During interviews, it's advised to provide direct answers and support theoretical explanations with practical examples from your experience.
  • 🚫 Avoid using cliché examples like cars or dogs; instead, use domain-specific examples relevant to the job or industry you're targeting.
  • 📑 A class is a blueprint or template for creating objects, and an object is an instance of a class with specific values assigned to its properties.
  • 🔑 Abstraction is about showing only the necessary details to the outside world, while Encapsulation is about hiding the complexity and internal workings of a class.
  • 🧬 Inheritance in OOP allows a class (child) to inherit properties and methods from another class (parent), promoting code reusability and establishing a hierarchy.
  • 🔄 Polymorphism enables an object to take on many forms or act differently under different conditions, which requires inheritance to be implemented.
  • 🛠️ Method overloading and method overriding are distinct; overloading involves multiple methods with the same name but different signatures within the same class, while overriding involves a child class providing a new implementation for a virtual method of its parent class.
  • 🌟 Operator overloading allows customizing the behavior of operators for class instances, enhancing the flexibility and expressiveness of the code.

Q & A

  • Why is understanding Object Oriented Programming (OOP) concepts crucial for IT programming interviews?

    -Understanding OOP concepts is crucial because OOP principles such as abstraction, encapsulation, inheritance, and polymorphism are widely used in software development. Interviewers expect candidates to demonstrate a clear grasp of these concepts, as they are foundational to designing and implementing efficient, maintainable, and scalable software systems.

  • What is the significance of thinking in terms of real-world objects in OOP?

    -Thinking in terms of real-world objects allows developers to model software solutions based on tangible entities and their interactions, leading to more organized, manageable, and understandable code. This approach enhances the maintainability and scalability of software systems by mirroring real-world scenarios within the codebase.

  • What are the four pillars of OOP and how do they contribute to robust software design?

    -The four pillars of OOP are Abstraction, Polymorphism, Inheritance, and Encapsulation. Abstraction allows hiding of unnecessary details and focusing on essential features. Polymorphism enables objects to behave differently under varying conditions. Inheritance promotes code reusability and establishes a hierarchy of classes. Encapsulation ensures that the internal state of an object is protected from external interference. Together, these principles lead to the creation of solid, flexible, and easily maintainable software.

  • How does the use of practical examples during an interview demonstrate a candidate's understanding and experience with OOP?

    -Using practical examples in an interview shows that a candidate can apply theoretical OOP concepts to real-world scenarios. It indicates hands-on experience with enterprise applications and the ability to communicate complex ideas effectively, which are critical skills for software development roles.

  • What is the difference between Abstraction and Encapsulation in OOP?

    -Abstraction is the process of simplifying complex reality by selecting only the essential features that are relevant to the problem at hand. Encapsulation is the mechanism of hiding the internal state and functionality of an object from the outside world. While both focus on managing complexity, abstraction is a design concept that decides what gets exposed, and encapsulation is an implementation concept that enforces this design by using access modifiers like private and public.

  • Explain the concept of Inheritance in OOP and its benefits.

    -Inheritance in OOP is a mechanism that allows a class (child) to inherit properties and methods from another class (parent), promoting code reusability and establishing a hierarchical relationship between classes. This leads to reduced redundancy, easier maintenance, and a more organized codebase as common functionality can be defined once in the parent class and extended or modified by child classes as needed.

  • What is the purpose of the 'virtual' keyword in C# and how does it relate to method overriding?

    -The 'virtual' keyword in C# is used to indicate that a method or property in a base class can be overridden by a derived class. This provides a mechanism for runtime polymorphism, allowing derived classes to provide their own implementation of a member that is already defined in the base class, thus enabling flexible and dynamic behavior changes based on the object's actual type at runtime.

  • How does method overloading differ from method overriding in OOP?

    -Method overloading refers to the ability of a class to have multiple methods with the same name but different signatures. It is a form of compile-time polymorphism where the appropriate method to call is determined based on the number and types of arguments passed. Method overriding, on the other hand, is a form of runtime polymorphism where a derived class provides its own implementation for a method that is already defined in its base class, allowing for different behavior depending on the actual object type.

  • What is polymorphism in OOP and why is it necessary to have inheritance for implementing polymorphism?

    -Polymorphism in OOP is the ability of an object to take on many forms, or in this context, to behave differently under different conditions. It is necessary to have inheritance for implementing polymorphism because it establishes a parent-child relationship that allows a child class object to be treated as an object of its parent class. This enables the runtime determination of which method implementation to invoke based on the actual object type, leading to flexible and reusable code.

  • What are the two types of polymorphism in C# and how are they implemented?

    -The two types of polymorphism in C# are static (or compile-time) polymorphism and dynamic (or runtime) polymorphism. Static polymorphism is implemented through method overloading, where multiple methods with the same name but different signatures exist within the same class. Dynamic polymorphism is implemented through method overriding, where a derived class provides a new implementation for a virtual method inherited from its base class.

  • Explain the concept of operator overloading in OOP and how it is used in C#.

    -Operator overloading in OOP is the ability to redefine the behavior of existing operators such as addition, subtraction, or concatenation, for custom classes. In C#, this is achieved using the 'operator' keyword followed by the operator symbol and a method definition that specifies the custom logic for the operator. This allows developers to define how objects of their class interact with operators, enabling custom behaviors without changing the fundamental syntax of the language.

  • What is an abstract class in OOP and why can't instances of it be created?

    -An abstract class in OOP is a partially completed class that serves as a base for other classes. It often contains both implemented and abstract methods. Instances of an abstract class cannot be created because it is meant to be inherited and extended by other classes. By preventing the instantiation of abstract classes, it ensures that the class is only used as a base and that its abstract methods are implemented by derived classes, providing a clear structure and preventing misuse.

Outlines

00:00

📘 Introduction to OOP Interview Questions

This paragraph introduces the topic of the video, which is about covering 25 important Object Oriented Programming (OOP) interview questions using C# language. It emphasizes the importance of understanding OOP concepts for clearing IT programming interviews, regardless of the programming language one specializes in. The speaker highlights the significance of concepts like abstraction, encapsulation, inheritance, polymorphism, and others in every project. The introduction also mentions a free C# interview questions e-book for viewers who share the video on social media platforms and directs them to the website questpond.com for more interview-related resources.

05:06

🎯 Importance of Being Practical in Technical Interviews

This paragraph discusses the importance of being practical and providing real-world examples during technical interviews. It advises against using clichéd examples like cars, dogs, or trees, and instead suggests using examples relevant to the domain one has worked in, such as inventory or hospital management systems. The speaker explains that providing domain-specific examples can demonstrate to the interviewer that the candidate has practical experience with enterprise applications. The paragraph also introduces the concept of the '3 P's' (Prepared, to the Point, and Practical) as key to successful interview responses.

10:15

📌 Defining Classes and Objects

This paragraph explains the fundamental concepts of classes and objects in object-oriented programming. A class is described as a blueprint or a type, with properties and methods, while an object is an instance of a class. The speaker uses the example of an Employee class with properties like name and address and a method called validate to illustrate the concept. The paragraph also differentiates between abstraction and encapsulation, with abstraction being about showing only what is necessary and encapsulation about hiding complexity. It further clarifies that encapsulation is used to implement the design decisions made during the abstraction phase.

15:34

🧬 Inheritance and Its Mechanism

The paragraph delves into the concept of inheritance in OOP, defining it as a parent-child relationship between classes where the child inherits properties and methods from the parent. It uses the example of an Employee class and a Manager class to explain how inheritance allows the Manager class to have all the properties of the Employee class, with the ability to add its own unique methods. The paragraph also touches on the use of the virtual keyword and method overriding, explaining how a child class can override a virtual method from the parent class to provide its own implementation. Additionally, it addresses method overloading, contrasting it with method overriding and explaining how it involves having the same method name with different signatures.

20:36

🌟 Understanding Polymorphism and Its Types

This paragraph focuses on the concept of polymorphism, which is the ability of an object to act differently under different conditions. It clarifies that polymorphism cannot be implemented without inheritance. The two types of polymorphism in C# are discussed: static (compile-time) and dynamic (runtime) polymorphism. Static polymorphism is achieved through method overloading, while dynamic polymorphism is achieved through method overriding. The paragraph also introduces operator overloading as a form of polymorphism that allows for custom operator functionality. The explanation includes examples and contrasts between method overloading and overriding, highlighting the importance of polymorphism in OOP interviews.

25:40

📖 Abstract Classes and Interfaces in OOP

The paragraph discusses abstract classes and interfaces, which are crucial topics in OOP interviews. An abstract class is described as a half-defined or partially defined class that cannot be instantiated and may contain both defined and abstract methods. The speaker uses the example of a Customer class and its subclasses, GoldCustomer and SilverCustomer, to explain how abstract classes work and the necessity of implementing abstract methods in child classes. The paragraph also addresses common questions about abstract classes, such as whether abstract methods are virtual by default and why a simple base class cannot replace an abstract class. The explanation emphasizes the purity of abstract classes in defining a clear parent-child relationship and the necessity of implementing abstract methods in derived classes.

Mindmap

Keywords

💡Object Oriented Programming (OOP)

OOP is a programming paradigm based on the concept of 'objects', which can contain data and methods to operate on that data. It is centered around the idea of organizing software design around real-world entities and the relationships between them. In the video, OOP is the main theme, with a focus on explaining various concepts such as abstraction, encapsulation, inheritance, and polymorphism that are fundamental to this paradigm.

💡Abstraction

Abstraction in OOP refers to the process of simplifying complex systems by hiding unnecessary details and exposing only the essential features. It allows developers to manage complexity by focusing on what is relevant to the problem at hand. In the video, the speaker emphasizes the importance of abstraction in designing classes, where only necessary properties and methods are exposed to the outside world, as seen in the example of a hospital management system where the internal workings of a 'Patient' class are hidden from the user.

💡Encapsulation

Encapsulation is the OOP concept that involves bundling data with the methods that operate on that data, and restricting direct access to the data. This helps to hide the internal state of an object and protect it from being corrupted or misused. In the video, the speaker explains that encapsulation is about hiding complexity and ensuring that only the necessary aspects of an object are visible to the outside, such as making the 'validate' method public while keeping internal validation functions private.

💡Inheritance

Inheritance in OOP is a mechanism where a new class is derived from an existing class, inheriting its properties and methods, and can also add new behavior. It promotes code reusability and establishes a hierarchy of classes. The video provides an example of a 'Manager' class inheriting from an 'Employee' class, where the 'Manager' gains all the properties of an 'Employee' and can have additional methods specific to management tasks.

💡Polymorphism

Polymorphism allows objects to be treated as instances of their parent class rather than their actual class, enabling a single interface to be used for generalizing functions. It can be static (compile-time) or dynamic (runtime), and it allows methods to act differently depending on the object they are applied to. The video explains polymorphism with examples of an 'Employee' object behaving as a 'Manager' or 'Supervisor', showcasing the flexibility in handling different types through a common interface.

💡Overriding

Overriding in OOP is the process by which a subclass provides a specific implementation of a method that is already defined by its superclass. This allows the subclass to change or extend the behavior of the inherited method. In the video, the concept of overriding is discussed in the context of the 'validate' method, where a 'Manager' class can override the 'validate' method of the 'Employee' class to implement its own validation logic.

💡Overloading

Method overloading is when multiple methods have the same name but different signatures (i.e., they accept a different number or type of parameters). This allows a class to provide several methods with the same name, each performing a similar task but with different input requirements. The video illustrates method overloading with a 'validate' method that is implemented three times, each with a different parameter list, allowing for various validation scenarios.

💡Abstract Class

An abstract class in OOP is a class that cannot be instantiated and serves as a base for other classes. It often contains abstract methods, which are placeholders for functionality that must be provided by derived classes. The video explains that abstract classes are useful for defining common behavior and state that can be shared among a group of related classes, such as 'GoldCustomer' and 'SilverCustomer' sharing properties from a 'Customer' class but implementing 'CalculateDiscount' differently.

💡Interface

An interface in OOP is a contract that defines a set of methods that a class can implement. It does not provide any implementation details, allowing multiple classes to implement the same interface in different ways. Interfaces promote modular and flexible design by separating the definition of methods from their implementation. The video will likely discuss interfaces as a way to achieve polymorphism and code reusability without the need for a class hierarchy, unlike abstract classes.

💡Operator Overloading

Operator overloading is a feature in OOP that allows operators like '+', '-', '*' to be redefined for custom data types. This enables developers to create custom behavior for basic operations when applied to objects of their classes. In the video, the concept is exemplified by overloading the '+' operator to define a custom logic for adding two objects of a 'SomeClass' type, resulting in a new object with combined properties.

💡Technical Interview

A technical interview is a process where candidates are assessed on their problem-solving skills, coding abilities, and understanding of programming concepts. In the context of the video, the speaker is preparing the audience for technical interviews by covering essential OOP concepts and providing strategies for answering questions effectively. The video emphasizes the importance of being prepared, answering to the point, and using practical examples to demonstrate understanding and experience.

Highlights

The importance of understanding OOP concepts in IT programming interviews, as it is crucial to clear the interview and demonstrate knowledge of abstraction, encapsulation, inheritance, polymorphism, and other OOP principles.

The offer of a free C# interview questions e-book for those who share the video on social media platforms, available at questpond.com.

The explanation of why Object Oriented Programming (OOP) is needed, emphasizing its ability to model real-world objects and improve code organization.

The four pillars of OOP: Abstraction, Polymorphism, Inheritance, and Encapsulation, with the acronym APIE introduced for easy recall.

The distinction between Abstraction and Encapsulation, clarifying that Abstraction is a design phase concept while Encapsulation is implemented during the coding phase.

The definition of a Class and an Object, with a class being a blueprint and an object being an instance of that class.

The explanation of Inheritance in OOP, detailing the parent-child relationship and how child classes inherit properties and methods from parent classes.

The concept of Virtual methods and how they allow for logic in the parent class to be overridden by child classes.

The difference between Method Overloading and Overriding, with Overloading involving the same method name with different signatures within the same class, and Overriding involving a parent and child class relationship.

The introduction to Polymorphism, the ability of an object to act differently under different conditions, and the necessity of inheritance for implementing polymorphism.

The two types of polymorphism in C#: Static (compile-time) and Dynamic (runtime), with static polymorphism implemented by method overloading and dynamic polymorphism by method overriding.

The explanation of Operator Overloading in polymorphism, allowing for custom logic to be applied to operators like addition and concatenation.

The transition to discussing Abstract Classes and Interfaces, which are crucial topics in OOP interviews.

The definition of an Abstract Class as a half-defined or partially defined parent class that cannot be instantiated and requires child classes to implement certain methods.

The clarification that abstract methods in an abstract class are by default virtual, allowing for them to be overridden in child classes.

The discussion on the necessity of implementing abstract methods in child classes and the impossibility of creating instances of abstract classes.

The rationale behind using abstract classes over simple base classes, emphasizing the purity of a partially defined class in abstract classes compared to the hacky approach in simple classes.

Transcripts

play00:00

To day we are covering 25 plus important  Object Oriented Programming Interview questions  

play00:08

with answers using C# language. Even though this  video made using c# language it is very much valid  

play00:15

for people who are in JAVA, C++ becuase OOP  programming concepts do not change as such.

play00:23

Before I start answering questions I want  to make a very very important statement -  

play00:29

when you talk about IT programming  interviews if you are not able to answer  

play00:34

oop interview questions it is very  difficult you will clear the interview.  

play00:40

In every project OOP programming concepts  are used. No senior or team leader would  

play00:46

like to see a developer does not understand  abstraction, encapsulation, inheritance,  

play00:51

polymorphism, overloading , dynamic polymorphism,  static polymorphism, abstract classes, interfaces.

play00:58

If you are not able to answer simple OOP questions  

play01:02

it is very difficult you will make through  the interview. Let us get started in this  

play01:08

1 hour of video I am going to cover oop  interview questions around classes, objects,  

play01:16

abstraction, encapsulation, inheritance,  polymorphism, overriding, overloading,  

play01:21

static and dynamic polymorphism, abstract  classes, interfaces and so many other things.

play01:31

Before starting with the first question I want to  make a small announcement, give you a small gift  

play01:39

which is C# Interview questions  e-book. If you share this video  

play01:48

anywhere on your linkedin, facebook, twitter you  will get this free C# interview 200 pages e-book.  

play02:19

Visit questpond.com we have lot of  videos out there which are specifically  

play02:24

meant for how to crack interview, get your  next jobs, prepare for technical things.  

play02:29

Lot of videos out there C# Interview  Questions, asp.net interview questions,  

play02:33

sql server interview questions, angular  interview questions. Interview questions  

play02:40

with demonstrations with proper explanation  which will help you to get the enxt job.

play02:48

Get started with 25 important oop interview  questions and start with the first question why  

play02:57

do we need object oreinted programming, what is  the benefit of using object oriented programming.  

play03:05

When this question comes up many developers answer  like oop programming will make your code better,  

play03:13

readable, some developers talk about features  of object oriented programming like abstraction,  

play03:19

encapsulation, polymorphism and so on. Please do  not beat around the bush answer to the point. The  

play03:28

first P in the technical interview is answer to  the point. The person who is taking your interview  

play03:34

is a senior person he does not get extra pay for  it. He is already stressed with project deadlines  

play03:44

and on the top in the interview if he is seeing  beating around the bush he just gets irritated.

play03:54

Why do we need object oreinted programming ? object oreinted programming helps us to think  

play04:08

in terms of real world objects. Let  us try to understand this statement  

play04:14

thinking in terms of real world objects. You are  creating a software for a hospital, a hospital has  

play04:22

patients and now in the code you will create class  patient, patient has a name, address. Hospital has  

play04:39

doctors, will say class doctor. Doctor has a name  

play04:48

the patient is allocated to a  doctor or doctor treats a patient.  

play04:58

This is the doctor object aggregated inside it.  

play05:05

Your code is mimicking the real world object.  Patient has a name address, there is a doctor who  

play05:15

is allocated to a patient. By having such approach  you can manage your code better, your code gets  

play05:21

organized properly. Object Oriented Programming  helps us to think in terms of real world objects.  

play05:29

As we complete the first question's answer lets  see one more important P about technical interview  

play05:37

that is be Practical. Technical interview should  be substantiate with the practical examples. We  

play05:46

just saw object oriented programming helps us to  think in ters of real world objects and later on  

play05:53

gave an example of a hospital, patient, doctor I  talked about aggregation. Just try to support your  

play06:03

theoritical answers with some practical examples  as well. This send message to the interviewer  

play06:10

that you have worked with an enterprise  application and you are person for this job.

play06:16

One serious request when it comes to object  oriented programming don't give examples  

play06:21

about car, dog, cat, tree because it can send a  wrong message to the interviewer, it can make the  

play06:32

interview very weird. Always try to give  examples like inventory, payroll, billing,  

play06:37

accounting, hospital management system or  something of domain you have worked with.

play06:48

Questions no. 2 - what are  the important pillars in OOP ? 

play06:54

There are four important pillars in oop, the  first one is Abstraction. Abstraction means  

play07:00

show only what is necessary. Second one  Polymorphism - object can act differently under  

play07:06

different conditions so user object can become an  emplyee or can become an admin or worker. Object  

play07:16

can take different shapes in different conditions  or it act differently under different condition.  

play07:22

Third one inheritance - you can  have a parent child relationship  

play07:26

where you have something common in  the parent and the child can inherit  

play07:30

and add something more to it.  Last one is Encapsulation means  

play07:34

Hide complexity. Hide anything which is complex  which should not be shown out side the object.

play07:42

Abstraction, Polymorphism, Inheritance and  Encapsulation. In the coming up lectures I  

play07:49

am going to talk very detail about  abstraction, inheritance and so on.

play07:53

This question is very focused - what are the  important pillars so I answered these 4 points.  

play08:02

Incase you do not remember these  4 things remember the word APIE -  

play08:11

A for Abtraction, P for Polymorphism, I for  Inheritance and E for Encapsulation. Do not  

play08:18

be shy saying like I am preparing for interviews,  remeber the third P - always be prepared,  

play08:30

it is okay to use acronyms. You are working in a  company, doing some practical work, sending emails  

play08:37

the whole day you are not revising and learning.  Doing a job and cracking a interview are two  

play08:46

different things. A prepared developer has  more chance of clearing an interview than an  

play08:54

unprepared superhero. You can be a superhero  in a company, would have delivered big projects  

play09:00

but if you do not answer simple questions like  what are the important characteristics of OOP  

play09:05

it is difficult to clear the interview. For  this question remember this acronym APIE.

play09:17

Before moving to the next questions let us  iterate those 3 P's. If you remeber these 3  

play09:23

P's while giving the answers half of your problem  is solved. First p be Prepared, b to the Point  

play09:32

don't waste intevriewer's time and the last P be  Practical, talk about some practicale examples.

play09:39

Cracking a technical interview is 70%  

play09:44

you should have a knowledge of the topic  but 30% is about your technical vocabulary.

play09:54

The next is what exactly is the class and  object? A class is a blue print a class is type.  

play10:04

Class has Employee, it has name, address and  it can have some functions, methods. Now to  

play10:15

use this class we need to create an instance  where will say Employee e1 = new Employee();

play10:23

Here e1 is an object it is an instance or can  create one more instance e2 = new Employee();

play10:33

We can say e1 name is Shiv and e2 name  is Raju. We can say a class is a blue  

play10:53

print and to use the class we need to create  the instance of the class which is an object.

play11:01

Differentiate between  Abstraction and Encapsulation.The  

play11:07

answer to this abstraction means show only what is  necessary and Encapsulation means hide complexity.  

play11:17

The answer is so synonymically same, it  is like saying we are happy when we drink  

play11:32

beer or drinking bear make us happy. Again  the interviewer will counter question saying  

play11:40

what's the difference between  abstraction and encapsulation?  

play11:45

Abstraction says show only necessary for example  this is an emplyee class and in the emplyee class  

play11:52

the name has to be shown outside so we made it  public. Address has to be shown outside we made  

play11:57

it public and then this is the validate function  which has ton be shown outside we made it public.

play12:05

When you say validate you would like  to validate the name and the address.  

play12:13

You create two more function or rather i will  say developer will create two more functions  

play12:18

like checkname which will actually check the  name CheckAddress which will check the address.  

play12:25

Inside the validate function you will have  checkname and you will have CheckAddress. For now  

play12:37

you only want to show validate outside and not  CheckName and CheckAddress, they are more internal  

play12:45

things. Developer has created it. When he started  coding developer has created both these functions  

play12:53

so that the validate function can be completed.  What happens because of making everything public  

play13:02

in abstraction or we decided that for the emplyee  class only validate should be seen outside  

play13:09

but we are also seeing CheckName and CheckAddress,  we make these as private. Now I only see what is  

play13:25

necessary i.e the name , address, validate but I  don't see CheckName and CheckAddress. Abstraction  

play13:40

is a design thought process or it happens during  the design phase. When you get the requirement in  

play13:47

your technical document. In your class diagram  you you decide what are the public methods,  

play13:56

public properties and public clases  which have to be exposed outside.

play14:02

During implementation or during the coding phase  the developer needs more functions to complete  

play14:09

the abstraction or the thought process. At the  time the developer creates more functions and  

play14:15

he needs to use the access modifiers like  private, public and protected to implement  

play14:22

Encapsulation. Abstraction  happens during the design phase.  

play14:37

In the design phase you decide what has to  be shown public and during execution phase  

play14:54

developer uses encapsulation means he uses  private, public and protected access modifiers  

play15:07

to implement the thought process of  abstraction. One more thing which you  

play15:13

can really put ahead before the interviewer  is Encapsulation implements abstraction.

play15:33

Encapsulation and Abstraction  complement each other.When  

play16:10

you give the answer just try to emphasize  that Encapsulation implements Abstraction  

play16:18

and abstraction happens during design phase and  encapsulation happens during the coding phase.

play16:27

Explain Inheritance - Inheritance is one of  properties of object oriented programming  

play16:32

wherein you can define a parent and a  child relationship between two classes.  

play16:39

This is an emplyee class out here you can  create a new class called as manager. This  

play16:46

manager will have all the properties and the  methods of the parent class. To do iheritance  

play16:52

will say Manager inherits from Employee so now  you get all the properties of the parent class  

play17:02

methods and everything but you can define your  own methods in the child class, I can say here  

play17:09

manager does management. If I create the employee  class I will get x properties but if i create  

play17:28

the object of the manager class i will get all  the properties and the methods of the parent class  

play17:34

but as well as the manager class has its own  exta methods like management. Inheritance  

play17:42

defines a prant and a child relationship between  two classes. In the interview lot of times this  

play17:50

question can also tweet and interviwer can ask  what is a relationship is a relationship again  

play17:57

a parent child relationship ? Is a relationship  means manager is a child of employee. Lot of  

play18:05

times this inheritance question is also  tweet by asking what is a relationship?

play18:13

What is the use of virtual keyword  or what exactly is virtual methods?

play18:20

This question can also come in a tweet manner  where interviewer can ask explain the concept  

play18:26

of overriding, virtual keyword, virtual  methods the answer lies in the same concept.  

play18:34

Virtual keyword helps us to define some logic  in thr parent class which can be overridden in  

play18:41

the child class. We have this class employee we  can say this method as virtual. As soon as you  

play18:53

give this word virtual here go to your child  class. For example this manager is inheriting  

play18:59

from the employee so the manager becomes a  child class. We can say override validate.  

play19:09

The employee class will have certain  functionality but we can override here with our  

play19:17

own logic. Virtual keyword helps us to provide  some implementation in the parent class which  

play19:23

can be overriden in the child class. This  concept is also termed as overriding.

play19:30

One of the connected questions which comes with  Overriding is what exactly is Method overloading ?  

play19:38

This question can come in a tweet fashion what is  the difference between overriding and overloading?

play19:45

Method overloading means same method  names with different signature.  

play19:51

We have a validate method out here and will  have one more method called as validate  

play19:57

and it has an input. This is  validate without any inputs,  

play20:02

this is validate with the input. I can have one  more validate wgeb I say some another input.

play20:11

The validate method in this manager  class has been overloaded three times.  

play20:18

One is it has validate with without any signature,  

play20:21

another one is it has validate which has  a boolean input and there is an another  

play20:25

validate which it has two more inputs. Method  overloading means in the same class you can have  

play20:35

same method names but different signatures. You  can say here validate without the input, validate  

play20:42

with the boolean input and validate with two more  inputs. This is termed as method overloading.

play20:50

Quickly summarize the difference between an  Overloading and Overridding. This question  

play20:58

is like comparing Applie with Mangoes but  let us try to put some sentence around it.  

play21:04

When you talk about method overloading then in  the same class you have the same method names  

play21:10

with different signatures. Method overriding  comes in a parent child relationship so there  

play21:26

should be a parent child relationship or  else method overriding does not exists.

play21:31

We have an employee class it has a  validate method which is marked as virtual  

play21:36

then this virtual method is later overridden by  using the override keyword. In method overriding  

play21:44

we are using this override keyword and there is a  

play21:48

parent child relationship where the parent  class has a virtual method. Method overriding  

play22:04

makes sense when we talk about parent child  relationship, when you talk about a parent method  

play22:14

overloading in the same class will have  the method names with different signature.

play22:20

Congratulations we have completed 9 questions.  Now the coming four questions from question 10  

play22:29

till question 14 is completely on Polymorphism. No  object oriented programming interview is complete  

play22:36

without a question on polymorphism. Will talk  about what is Polymorphism, what is static  

play22:41

polymorphism, what is dynamic polymorphism,  what is operator overloading and so on.

play22:46

Before we move ahead with the questions on  

play22:49

polymorphism if you are really liking  this video subscribe to this channel,  

play22:55

hit the bell button so whenever we upload an  interview question video you get an indication.

play23:02

Explain polymorphism? If you see the word polymorphism, poly means  

play23:08

many and morph means change as per situation.  It is ability of an object to act differently  

play23:19

under different conditions. In this code on the  screen, this Employee e in line no.9 is acting  

play23:29

like a manager and the same employee object  e in line no.10 is acting like a supervisor.  

play23:38

In line no.9 it is manager then dynamically in  line no. 10 it is becoming supervisor. This is  

play23:45

termed as polymorphism, the ability of an object  to act differently under different condition.

play23:53

Can we implement polymorphism without  inheritance or without parent child relationship?  

play23:59

The answer is NO, this question specifically  from the interviewer from the point do you  

play24:06

understand polymorphism in detail. In this case  the Emplyee e become a manager in line no. 9  

play24:16

and in line no. 10 it is supervisor because both  the manager class is inheriting from the employee  

play24:25

as well as the supervisor class is inheriting  from the employee. If you remove this inheritance  

play24:31

out here c# will start giving exception. To  implement polymorphism inheritance is must.

play24:42

What are the two kinds of polymorphism in C# ?

play24:45

The two kind of polymorphism in C#  are Static and Dynamic polymorphism.  

play24:51

Many developer also term them as run time  polymorphism and compile time polymorphism.  

play24:57

Run time polymorphism means dynamic polymorphism  and compile time is static polymorphism.

play25:09

Static polymorphism is implemented  by method overloading while dynamic  

play25:14

polymorphism is implemented by overriding.  We have this Manager class, validate method.  

play25:27

One validate method we have without a signature  then we have validate method with a one signature  

play25:34

and then we have validate method wtih two  signatures. One signature is staic polymorphism,  

play25:40

this will be checked during the compile time means  when I hit the build at that time it will check.

play25:47

Runtime polymorphism and dynamic polymorphism  happen in a parent child relationship.  

play25:52

In parent child relationship  we have to use virtual keywords  

play25:59

and then the manager goes and inherits  and overrides this validate. I can say  

play26:08

dynamic polymorphism employee e can point  towards manager and it will call e.validate  

play26:15

and the same employee can point towards  supervisor and and he can call validate.  

play26:24

Method overriding is run time polymorphism  and method overloading is static polymorphism.

play26:31

Explain operator overloading?

play26:34

Operator overloading is a concept of polymorphism  where we can redefine operators like plus sign  

play26:41

and the minus sign and the multiplication  sign with addition functionalities. It is  

play26:48

inbuilt inside .net by default. When you  use the + sign in line no.9 Shiv + Koirala,  

play26:59

this will do unconcatination. This will  concatinate both the strings. If we say 1+2  

play27:10

then this will do an arithmetic addition. By  default you will see polymosphism in .NET.

play27:18

The next cross question from the interviewer  can be what if you want to implement your own  

play27:23

logic of polymorphism or do a custom operator  overloading. Here I am treating an object  

play27:32

of some class called as o1 and creating another  class object as o2. What if i want to add  

play27:42

these objects o1 & o2, for that us the operator  overloading keyword here operator and the + sign.  

play27:54

This complete method we need to define as  static public static SomeClass operator  

play28:02

and pass the first instance of SomeClass  and second instance of SomeClass  

play28:08

and then you define the logic what you want to  do. I am adding this 2 numbers of SomeValue.

play28:16

To do a custom operator overloading then use  operator keyword. If I run this application  

play28:25

you will see two objects will be  added where o1 + o2 will give o3  

play28:35

and in o3 we have 10+20 which is 30. The  value in the o3 object of someValue is 30.  

play28:46

Operator overloading means the ability to  load an operation without functionalities  

play28:53

and to do custom operator overloading  then use the operator keyword.

play28:59

Congratulations we have completed 30 minutes  of this video, we have completed 14 questions  

play29:08

till now. From Question 15 till Question 27 these  questions are on abstract classes and interfaces.  

play29:18

No object oriented programming interview is  complete without a question on abstract class  

play29:24

interface. It is one of the most important and  loving topics when it comes to OOP interview  

play29:33

questions. Before starting with Abstract classes  and interfaces , go to facebook.com/questpond and  

play29:43

say I am on question no. 14 or 15 it just  tells us how many people are watching the  

play29:52

video till what limit and this encourageous  us to record more videos on this channel.

play30:15

Between Abstract classes and interfaces  abstract class is easy one. Let us start with  

play30:22

some questions around abstract classes first  then go towards interfaces and will compare  

play30:27

the differences between abstract class  and interface. What is an abstract class?  

play30:33

Abstract class is a half defined parent  class or a partially defined parent class.

play30:47

Here I have a customer class then  from this customer class we have two  

play30:52

other classes which are inheriting  GoldCustomer and SilverCustomer.  

play30:59

Both the GoldCustomer and the SilverCustomer share  lot of common properties for example they share  

play31:05

the customer name, product name, address,  productAmount. There is a CalculateDiscount  

play31:13

and this CalculateDiscount is overridden by  both of the classes with their own behaviour.

play31:22

GoldCustomer is getting 10% of discount and  SilverCustomer is getting 5% of discount.  

play31:32

In the parent class because we do not have  implementation we have said here throw new  

play31:37

NotImplementedException, in other words if  anybody tries to invoke this calculate discount  

play31:46

of the parent class it will say this will  be done by the child classes, there is no  

play31:51

implementation out here. The problem  with this approach is that somebody can  

play31:59

create an object of a Customer class like this  and he will try to invoke CalculateDiscount.  

play32:12

We can have confusion because you  have customer class so developers  

play32:17

will try to create the instance of the  customer class, they will try to invoke  

play32:21

this CalculateDiscount and they will get this  NotImplementedException which is really bad.

play32:30

Over here there is no implmentation.  In this customer class there is  

play32:37

some implementation and some of the implementation  has to be defined by the child class. We need to  

play32:48

create a class which is half defined, partially  defined so for that we need abstract class.  

play32:58

We can say abstract class by saying abstract. This  method CalculateDiscount is without any logic so  

play33:06

it should be half in other words there should not  be any logic out there we say this as an abstract.  

play33:16

This customer class now is half defined. The name,  address, productName and Amount is fully defined  

play33:24

but the calculate discount will be overridden by  the child classes and the logic will be defined.  

play33:31

The best part about the abstract class is that it  is half defined you cannot create instance of it.  

play33:39

The confusion where you can invoke a half class  then later on it give an error, it is stopping you  

play33:48

to not to create instance which is actually very  good. What you have out infront of the interviewer  

play33:58

is Abstract class is a half defined parent  class where there are some implementation  

play34:03

which is defined and some implementationis  left to the Child classes to be defined.

play34:09

One more tricky question which comes around  abstract classes are abstract methods of an  

play34:15

abstract class virtual? The answer is yes. In the  last OOP interview questions I talked about the  

play34:25

virtual keyword. Virtual keyword what it does  is, it will define in a parent class method as  

play34:35

virtual like this, in the child class you can  override it. In the child class you can say  

play34:44

override. The CalculateDiscount is seen because  it is virtual, if I remove this word virtual  

play34:52

i will not see CalculateDiscount. The question  here the interviewer asking is if I declare a  

play35:01

method as abstract in abstract class yes it  is virtual becuase down below you can say  

play35:14

override and see CalculateDiscount. If you do not  declare this as an abstract it will not be seen.

play35:20

Abstract method in abstract class are by default  

play35:24

virtual. There can be some short questions  around abstract classes which can be asked  

play35:32

and those answers are just Yes and No but be very  careful when answering them. First short question  

play35:38

which comes around abstract classes is that can we  create instance of an abstract class? absolutely  

play35:43

NO. You cannot create an instance of an abstract  class, the compiler will throw up an exception.  

play35:51

When I say NewCustomer it says you cannot create  an instance of an abstract class that is the first  

play35:58

short question. The second short question  which is asked around abstract class is is  

play36:04

it compulsory to implement the abstract methods in  the child class? We have CalculateDisocunt, is it  

play36:12

compulsory that we should implement those methods  in the child classes? yes it is compulsory, these  

play36:19

methods are half defined so if we do not write a  code in a child class it will give an exception  

play36:29

saying the GoldCustomer does not implement  CalculateDiscount. If there is an Abstract Method  

play36:36

or Abstract Function that it has to be  implemented compulsarily in the child class.

play36:44

Once more crossed question which can  be asked around the abstract class is  

play36:48

why can't a simple base or parent class replace  an abstract class? A simple base class or simple  

play36:59

parameter class we cannot define it in half way,  we cannot define it partially. In other words  

play37:06

for example If we make it a simple class then  we need to write some kind of a hack logic,  

play37:14

will say here return Null or return 0 or  will say throw NewNotImplementedException.  

play37:27

It is not a clean way of creating a  half partial class or half base class,  

play37:35

its like a hack but when we actually create  an abstract class this becomes a pure  

play37:44

partial class. Somethings are defined  and somethings are not defined.

Rate This

5.0 / 5 (0 votes)

Related Tags
OOP InterviewC# ProgrammingAbstractionInheritancePolymorphismEncapsulationInterview PrepTechnical VocabularySoftware DevelopmentCareer Advancement