OOPS Interview Questions and Answers | Object Oriented Programming Interview Questions C#
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
📘 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.
🎯 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.
📌 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.
🧬 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.
🌟 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.
📖 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)
💡Abstraction
💡Encapsulation
💡Inheritance
💡Polymorphism
💡Overriding
💡Overloading
💡Abstract Class
💡Interface
💡Operator Overloading
💡Technical Interview
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
To day we are covering 25 plus important Object Oriented Programming Interview questions
with answers using C# language. Even though this video made using c# language it is very much valid
for people who are in JAVA, C++ becuase OOP programming concepts do not change as such.
Before I start answering questions I want to make a very very important statement -
when you talk about IT programming interviews if you are not able to answer
oop interview questions it is very difficult you will clear the interview.
In every project OOP programming concepts are used. No senior or team leader would
like to see a developer does not understand abstraction, encapsulation, inheritance,
polymorphism, overloading , dynamic polymorphism, static polymorphism, abstract classes, interfaces.
If you are not able to answer simple OOP questions
it is very difficult you will make through the interview. Let us get started in this
1 hour of video I am going to cover oop interview questions around classes, objects,
abstraction, encapsulation, inheritance, polymorphism, overriding, overloading,
static and dynamic polymorphism, abstract classes, interfaces and so many other things.
Before starting with the first question I want to make a small announcement, give you a small gift
which is C# Interview questions e-book. If you share this video
anywhere on your linkedin, facebook, twitter you will get this free C# interview 200 pages e-book.
Visit questpond.com we have lot of videos out there which are specifically
meant for how to crack interview, get your next jobs, prepare for technical things.
Lot of videos out there C# Interview Questions, asp.net interview questions,
sql server interview questions, angular interview questions. Interview questions
with demonstrations with proper explanation which will help you to get the enxt job.
Get started with 25 important oop interview questions and start with the first question why
do we need object oreinted programming, what is the benefit of using object oriented programming.
When this question comes up many developers answer like oop programming will make your code better,
readable, some developers talk about features of object oriented programming like abstraction,
encapsulation, polymorphism and so on. Please do not beat around the bush answer to the point. The
first P in the technical interview is answer to the point. The person who is taking your interview
is a senior person he does not get extra pay for it. He is already stressed with project deadlines
and on the top in the interview if he is seeing beating around the bush he just gets irritated.
Why do we need object oreinted programming ? object oreinted programming helps us to think
in terms of real world objects. Let us try to understand this statement
thinking in terms of real world objects. You are creating a software for a hospital, a hospital has
patients and now in the code you will create class patient, patient has a name, address. Hospital has
doctors, will say class doctor. Doctor has a name
the patient is allocated to a doctor or doctor treats a patient.
This is the doctor object aggregated inside it.
Your code is mimicking the real world object. Patient has a name address, there is a doctor who
is allocated to a patient. By having such approach you can manage your code better, your code gets
organized properly. Object Oriented Programming helps us to think in terms of real world objects.
As we complete the first question's answer lets see one more important P about technical interview
that is be Practical. Technical interview should be substantiate with the practical examples. We
just saw object oriented programming helps us to think in ters of real world objects and later on
gave an example of a hospital, patient, doctor I talked about aggregation. Just try to support your
theoritical answers with some practical examples as well. This send message to the interviewer
that you have worked with an enterprise application and you are person for this job.
One serious request when it comes to object oriented programming don't give examples
about car, dog, cat, tree because it can send a wrong message to the interviewer, it can make the
interview very weird. Always try to give examples like inventory, payroll, billing,
accounting, hospital management system or something of domain you have worked with.
Questions no. 2 - what are the important pillars in OOP ?
There are four important pillars in oop, the first one is Abstraction. Abstraction means
show only what is necessary. Second one Polymorphism - object can act differently under
different conditions so user object can become an emplyee or can become an admin or worker. Object
can take different shapes in different conditions or it act differently under different condition.
Third one inheritance - you can have a parent child relationship
where you have something common in the parent and the child can inherit
and add something more to it. Last one is Encapsulation means
Hide complexity. Hide anything which is complex which should not be shown out side the object.
Abstraction, Polymorphism, Inheritance and Encapsulation. In the coming up lectures I
am going to talk very detail about abstraction, inheritance and so on.
This question is very focused - what are the important pillars so I answered these 4 points.
Incase you do not remember these 4 things remember the word APIE -
A for Abtraction, P for Polymorphism, I for Inheritance and E for Encapsulation. Do not
be shy saying like I am preparing for interviews, remeber the third P - always be prepared,
it is okay to use acronyms. You are working in a company, doing some practical work, sending emails
the whole day you are not revising and learning. Doing a job and cracking a interview are two
different things. A prepared developer has more chance of clearing an interview than an
unprepared superhero. You can be a superhero in a company, would have delivered big projects
but if you do not answer simple questions like what are the important characteristics of OOP
it is difficult to clear the interview. For this question remember this acronym APIE.
Before moving to the next questions let us iterate those 3 P's. If you remeber these 3
P's while giving the answers half of your problem is solved. First p be Prepared, b to the Point
don't waste intevriewer's time and the last P be Practical, talk about some practicale examples.
Cracking a technical interview is 70%
you should have a knowledge of the topic but 30% is about your technical vocabulary.
The next is what exactly is the class and object? A class is a blue print a class is type.
Class has Employee, it has name, address and it can have some functions, methods. Now to
use this class we need to create an instance where will say Employee e1 = new Employee();
Here e1 is an object it is an instance or can create one more instance e2 = new Employee();
We can say e1 name is Shiv and e2 name is Raju. We can say a class is a blue
print and to use the class we need to create the instance of the class which is an object.
Differentiate between Abstraction and Encapsulation.The
answer to this abstraction means show only what is necessary and Encapsulation means hide complexity.
The answer is so synonymically same, it is like saying we are happy when we drink
beer or drinking bear make us happy. Again the interviewer will counter question saying
what's the difference between abstraction and encapsulation?
Abstraction says show only necessary for example this is an emplyee class and in the emplyee class
the name has to be shown outside so we made it public. Address has to be shown outside we made
it public and then this is the validate function which has ton be shown outside we made it public.
When you say validate you would like to validate the name and the address.
You create two more function or rather i will say developer will create two more functions
like checkname which will actually check the name CheckAddress which will check the address.
Inside the validate function you will have checkname and you will have CheckAddress. For now
you only want to show validate outside and not CheckName and CheckAddress, they are more internal
things. Developer has created it. When he started coding developer has created both these functions
so that the validate function can be completed. What happens because of making everything public
in abstraction or we decided that for the emplyee class only validate should be seen outside
but we are also seeing CheckName and CheckAddress, we make these as private. Now I only see what is
necessary i.e the name , address, validate but I don't see CheckName and CheckAddress. Abstraction
is a design thought process or it happens during the design phase. When you get the requirement in
your technical document. In your class diagram you you decide what are the public methods,
public properties and public clases which have to be exposed outside.
During implementation or during the coding phase the developer needs more functions to complete
the abstraction or the thought process. At the time the developer creates more functions and
he needs to use the access modifiers like private, public and protected to implement
Encapsulation. Abstraction happens during the design phase.
In the design phase you decide what has to be shown public and during execution phase
developer uses encapsulation means he uses private, public and protected access modifiers
to implement the thought process of abstraction. One more thing which you
can really put ahead before the interviewer is Encapsulation implements abstraction.
Encapsulation and Abstraction complement each other.When
you give the answer just try to emphasize that Encapsulation implements Abstraction
and abstraction happens during design phase and encapsulation happens during the coding phase.
Explain Inheritance - Inheritance is one of properties of object oriented programming
wherein you can define a parent and a child relationship between two classes.
This is an emplyee class out here you can create a new class called as manager. This
manager will have all the properties and the methods of the parent class. To do iheritance
will say Manager inherits from Employee so now you get all the properties of the parent class
methods and everything but you can define your own methods in the child class, I can say here
manager does management. If I create the employee class I will get x properties but if i create
the object of the manager class i will get all the properties and the methods of the parent class
but as well as the manager class has its own exta methods like management. Inheritance
defines a prant and a child relationship between two classes. In the interview lot of times this
question can also tweet and interviwer can ask what is a relationship is a relationship again
a parent child relationship ? Is a relationship means manager is a child of employee. Lot of
times this inheritance question is also tweet by asking what is a relationship?
What is the use of virtual keyword or what exactly is virtual methods?
This question can also come in a tweet manner where interviewer can ask explain the concept
of overriding, virtual keyword, virtual methods the answer lies in the same concept.
Virtual keyword helps us to define some logic in thr parent class which can be overridden in
the child class. We have this class employee we can say this method as virtual. As soon as you
give this word virtual here go to your child class. For example this manager is inheriting
from the employee so the manager becomes a child class. We can say override validate.
The employee class will have certain functionality but we can override here with our
own logic. Virtual keyword helps us to provide some implementation in the parent class which
can be overriden in the child class. This concept is also termed as overriding.
One of the connected questions which comes with Overriding is what exactly is Method overloading ?
This question can come in a tweet fashion what is the difference between overriding and overloading?
Method overloading means same method names with different signature.
We have a validate method out here and will have one more method called as validate
and it has an input. This is validate without any inputs,
this is validate with the input. I can have one more validate wgeb I say some another input.
The validate method in this manager class has been overloaded three times.
One is it has validate with without any signature,
another one is it has validate which has a boolean input and there is an another
validate which it has two more inputs. Method overloading means in the same class you can have
same method names but different signatures. You can say here validate without the input, validate
with the boolean input and validate with two more inputs. This is termed as method overloading.
Quickly summarize the difference between an Overloading and Overridding. This question
is like comparing Applie with Mangoes but let us try to put some sentence around it.
When you talk about method overloading then in the same class you have the same method names
with different signatures. Method overriding comes in a parent child relationship so there
should be a parent child relationship or else method overriding does not exists.
We have an employee class it has a validate method which is marked as virtual
then this virtual method is later overridden by using the override keyword. In method overriding
we are using this override keyword and there is a
parent child relationship where the parent class has a virtual method. Method overriding
makes sense when we talk about parent child relationship, when you talk about a parent method
overloading in the same class will have the method names with different signature.
Congratulations we have completed 9 questions. Now the coming four questions from question 10
till question 14 is completely on Polymorphism. No object oriented programming interview is complete
without a question on polymorphism. Will talk about what is Polymorphism, what is static
polymorphism, what is dynamic polymorphism, what is operator overloading and so on.
Before we move ahead with the questions on
polymorphism if you are really liking this video subscribe to this channel,
hit the bell button so whenever we upload an interview question video you get an indication.
Explain polymorphism? If you see the word polymorphism, poly means
many and morph means change as per situation. It is ability of an object to act differently
under different conditions. In this code on the screen, this Employee e in line no.9 is acting
like a manager and the same employee object e in line no.10 is acting like a supervisor.
In line no.9 it is manager then dynamically in line no. 10 it is becoming supervisor. This is
termed as polymorphism, the ability of an object to act differently under different condition.
Can we implement polymorphism without inheritance or without parent child relationship?
The answer is NO, this question specifically from the interviewer from the point do you
understand polymorphism in detail. In this case the Emplyee e become a manager in line no. 9
and in line no. 10 it is supervisor because both the manager class is inheriting from the employee
as well as the supervisor class is inheriting from the employee. If you remove this inheritance
out here c# will start giving exception. To implement polymorphism inheritance is must.
What are the two kinds of polymorphism in C# ?
The two kind of polymorphism in C# are Static and Dynamic polymorphism.
Many developer also term them as run time polymorphism and compile time polymorphism.
Run time polymorphism means dynamic polymorphism and compile time is static polymorphism.
Static polymorphism is implemented by method overloading while dynamic
polymorphism is implemented by overriding. We have this Manager class, validate method.
One validate method we have without a signature then we have validate method with a one signature
and then we have validate method wtih two signatures. One signature is staic polymorphism,
this will be checked during the compile time means when I hit the build at that time it will check.
Runtime polymorphism and dynamic polymorphism happen in a parent child relationship.
In parent child relationship we have to use virtual keywords
and then the manager goes and inherits and overrides this validate. I can say
dynamic polymorphism employee e can point towards manager and it will call e.validate
and the same employee can point towards supervisor and and he can call validate.
Method overriding is run time polymorphism and method overloading is static polymorphism.
Explain operator overloading?
Operator overloading is a concept of polymorphism where we can redefine operators like plus sign
and the minus sign and the multiplication sign with addition functionalities. It is
inbuilt inside .net by default. When you use the + sign in line no.9 Shiv + Koirala,
this will do unconcatination. This will concatinate both the strings. If we say 1+2
then this will do an arithmetic addition. By default you will see polymosphism in .NET.
The next cross question from the interviewer can be what if you want to implement your own
logic of polymorphism or do a custom operator overloading. Here I am treating an object
of some class called as o1 and creating another class object as o2. What if i want to add
these objects o1 & o2, for that us the operator overloading keyword here operator and the + sign.
This complete method we need to define as static public static SomeClass operator
and pass the first instance of SomeClass and second instance of SomeClass
and then you define the logic what you want to do. I am adding this 2 numbers of SomeValue.
To do a custom operator overloading then use operator keyword. If I run this application
you will see two objects will be added where o1 + o2 will give o3
and in o3 we have 10+20 which is 30. The value in the o3 object of someValue is 30.
Operator overloading means the ability to load an operation without functionalities
and to do custom operator overloading then use the operator keyword.
Congratulations we have completed 30 minutes of this video, we have completed 14 questions
till now. From Question 15 till Question 27 these questions are on abstract classes and interfaces.
No object oriented programming interview is complete without a question on abstract class
interface. It is one of the most important and loving topics when it comes to OOP interview
questions. Before starting with Abstract classes and interfaces , go to facebook.com/questpond and
say I am on question no. 14 or 15 it just tells us how many people are watching the
video till what limit and this encourageous us to record more videos on this channel.
Between Abstract classes and interfaces abstract class is easy one. Let us start with
some questions around abstract classes first then go towards interfaces and will compare
the differences between abstract class and interface. What is an abstract class?
Abstract class is a half defined parent class or a partially defined parent class.
Here I have a customer class then from this customer class we have two
other classes which are inheriting GoldCustomer and SilverCustomer.
Both the GoldCustomer and the SilverCustomer share lot of common properties for example they share
the customer name, product name, address, productAmount. There is a CalculateDiscount
and this CalculateDiscount is overridden by both of the classes with their own behaviour.
GoldCustomer is getting 10% of discount and SilverCustomer is getting 5% of discount.
In the parent class because we do not have implementation we have said here throw new
NotImplementedException, in other words if anybody tries to invoke this calculate discount
of the parent class it will say this will be done by the child classes, there is no
implementation out here. The problem with this approach is that somebody can
create an object of a Customer class like this and he will try to invoke CalculateDiscount.
We can have confusion because you have customer class so developers
will try to create the instance of the customer class, they will try to invoke
this CalculateDiscount and they will get this NotImplementedException which is really bad.
Over here there is no implmentation. In this customer class there is
some implementation and some of the implementation has to be defined by the child class. We need to
create a class which is half defined, partially defined so for that we need abstract class.
We can say abstract class by saying abstract. This method CalculateDiscount is without any logic so
it should be half in other words there should not be any logic out there we say this as an abstract.
This customer class now is half defined. The name, address, productName and Amount is fully defined
but the calculate discount will be overridden by the child classes and the logic will be defined.
The best part about the abstract class is that it is half defined you cannot create instance of it.
The confusion where you can invoke a half class then later on it give an error, it is stopping you
to not to create instance which is actually very good. What you have out infront of the interviewer
is Abstract class is a half defined parent class where there are some implementation
which is defined and some implementationis left to the Child classes to be defined.
One more tricky question which comes around abstract classes are abstract methods of an
abstract class virtual? The answer is yes. In the last OOP interview questions I talked about the
virtual keyword. Virtual keyword what it does is, it will define in a parent class method as
virtual like this, in the child class you can override it. In the child class you can say
override. The CalculateDiscount is seen because it is virtual, if I remove this word virtual
i will not see CalculateDiscount. The question here the interviewer asking is if I declare a
method as abstract in abstract class yes it is virtual becuase down below you can say
override and see CalculateDiscount. If you do not declare this as an abstract it will not be seen.
Abstract method in abstract class are by default
virtual. There can be some short questions around abstract classes which can be asked
and those answers are just Yes and No but be very careful when answering them. First short question
which comes around abstract classes is that can we create instance of an abstract class? absolutely
NO. You cannot create an instance of an abstract class, the compiler will throw up an exception.
When I say NewCustomer it says you cannot create an instance of an abstract class that is the first
short question. The second short question which is asked around abstract class is is
it compulsory to implement the abstract methods in the child class? We have CalculateDisocunt, is it
compulsory that we should implement those methods in the child classes? yes it is compulsory, these
methods are half defined so if we do not write a code in a child class it will give an exception
saying the GoldCustomer does not implement CalculateDiscount. If there is an Abstract Method
or Abstract Function that it has to be implemented compulsarily in the child class.
Once more crossed question which can be asked around the abstract class is
why can't a simple base or parent class replace an abstract class? A simple base class or simple
parameter class we cannot define it in half way, we cannot define it partially. In other words
for example If we make it a simple class then we need to write some kind of a hack logic,
will say here return Null or return 0 or will say throw NewNotImplementedException.
It is not a clean way of creating a half partial class or half base class,
its like a hack but when we actually create an abstract class this becomes a pure
partial class. Somethings are defined and somethings are not defined.
Browse More Related Video
5.0 / 5 (0 votes)