C# Flashcards
WHAT ARE THE PRINCIPLES OF OOPS?
The principles of Object-Oriented Programming (OOP) are a set of guidelines that define the way in which objects interact with each other. These principles provide a foundation for the design and development of software systems that are modular, flexible, and maintainable. The four primary principles of OOP are:
Why OOPs?
drawbacks of procedure oriented and structure oriented programming language.
What is OOPs?
Pillars of OOPs.
1. Encapsulation:
Wrapping up of the data and data hiding. Eg: properties, class, methods, access specifiers/modifiers.
The concept of encapsulation involves hiding the internal details of an object from the outside world and providing a well-defined interface for interacting with it. This protects the object from unauthorized access or modification and allows for better control over how the object is used.
- Inheritance: Inheritance is the mechanism by which one class can inherit the properties and behavior of another class. This allows for the reuse of code and the creation of new classes that extend or modify the behavior of existing ones.
Why inheritance? reusability - Polymorphism: Polymorphism refers to the ability of objects to take on multiple forms or behaviors based on the context in which they are used. This allows for more flexible and dynamic code that can adapt to changing requirements or environments.
- Abstraction: Abstraction involves focusing on the essential properties and behavior of an object and ignoring the non-essential details. This simplifies the complexity of a system and allows for better organization and understanding of the code.
-C. Venkata Teja
WHAT IS INHERITANCE? WHY INHERITANCE IS IMPORTANT?
It is one of the Pillar in oops. Deriving a child class from Parent class. This child class automatically Inherit the properties of Parent class.
- It is very helpful for the code Reusability.
Type of Inheritance:
1.Single inheritance
2.Multiple inheritance
3.Multilevel inheritance
4.Hybrid inheritance
~ Mahathi Somisetty
WHAT ARE THE DIFFERENT TYPES OF INHERITANCE?
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows classes to inherit properties and behavior from other classes. There are different types of inheritance in OOP, including:
Single inheritance: A class can inherit properties and behavior from only one parent class.
Multiple inheritance: A class can inherit properties and behavior from more than one parent class.
Multilevel inheritance: A class can inherit properties and behavior from a parent class, which in turn inherits from another parent class. For example, if class C inherits from class B, which inherits from class A, then C has access to properties and behavior defined in both B and A.
Hierarchical inheritance: A class can have more than one child class, each of which inherits properties and behavior from the same parent class. For example, if class A is the parent class and classes B and C are child classes, then both B and C inherit properties and behavior from A.
Hybrid inheritance: Hybrid inheritance is a combination of two or more types of inheritance, such as multiple inheritance and hierarchical inheritance. It is also sometimes called “virtual inheritance.”
By: Siva Billudu
HOW TO PREVENT A CLASS FROM BEING INHERITED in c#?
In C# you can use the sealed keyword in order to prevent a class from being inherited.
By sealing or NotInheritable a class in .Net environment, you ensure that no class can be derived from that class. In some programming scenarios (mostly used for security reasons ) demand that you should declare your class in such a way that it should not be derived.
Typically, it occurs if the functionality that is implemented by the class should not be changed, or more appropriately, overridden. The sole purpose of this class to provide some implementation that can be used within that class only. Moreover a sealed class is the last class in the hierarchy.
- Abdul Aleem
Why we need Abstraction?
Abstraction is a crucial concept in computer science, software engineering, and many other fields. It is the process of reducing complexity by hiding unnecessary details while highlighting the essential features of a system. Here are some reasons why we need abstraction:
Simplifies Complexity: Abstraction helps us to simplify complex systems by breaking them down into smaller, more manageable parts. By abstracting away the details that are not necessary for understanding the system’s behavior, we can focus on the essential features and relationships that matter.
Encapsulates Complexity: Abstraction also allows us to encapsulate complexity by hiding implementation details behind a simple interface. This makes it easier to modify and maintain the system without affecting its external behavior.
Increases Reusability: Abstraction promotes reusability by making it easier to extract and reuse common functionality across different systems. By abstracting away implementation details, we can create more generic and flexible components that can be used in a wide range of contexts.
- Rizwan RA
Can you differentiate between Encapsulation and Abstraction?
Encapsulation:
Encapsulation is also a feature of OOPs. It is a Wrapping up of data and hides the code and data into a single entity .
- Encapsulation solves issues at implementation level
- It focuses on internal working.
- It process to contain information.
Analogy:
A capsule binds all its medicinal materials within it.
Example: Properties, access modifiers.
Abstraction:
Abstraction: Abstraction involves focusing on the essential properties and behavior of an object
- Encapsulation solves issues at design level
- It focuses on external outlook.
Example:
- abstract classes and interfaces in C#
- through designs you gain information.
encapsulation and abstraction are both important concepts in programming that help to improve code quality and make it easier to maintain and modify.
-Sohail
WHAT ARE ACCESS SPECIFIERS? WHAT IS THE DEFAULT ACCESS MODIFIER IN A CLASS?
ACCESS SPECIFIERS:Used to Control the Accessbility of class members.
Types of ACCESS SPECIFIERS:
1.Internal
2.public
3.private
4.protected
5.private protected
6.protected internal
The default access modifier in a class is internal.
By :Siva Billudu
What is Polymorphism?
Why polymorphism?
What - Polymorphism is a programming concept that refers to the ability of objects of different types to be treated as if they have the same interface or behavior. In other words, polymorphism allows objects of different classes to be used interchangeably if they have a common set of methods or properties.
How to implement??
Assigned to Rizwan
WHAT IS METHOD OVERLOADING? Why it is called compile time polymorphism?
Developing Multiple methods having same method name but having different parameter list is called Method overloading.
Example:
Demo(int a){
}
Demo(String s, int b){
}
Since this binding process is executed during compile time, that’s why it is known as Compile-Time Polymorphism.
by Ajay
WHAT IS THE DIFFERENCE BETWEEN OVERLOADING AND OVERRIDING?
- Method overloading is a type of polymorphism, in which we can create multiple
methods of the same name in the same class, and all methods work in different
ways. - Method overriding is having methods with the same name and signature
but in different classes. - Method Overloading will not use any special keywords.
Method Overriding uses the VIRTUAL keyword for the base class method and the OVERRIDE
keyword for the derived class method. - Method overloading doesn’t need inheritance. It is in the same class.
- Method overriding needs inheritance (Base class/ Derived class). It is not
possible in the same class
_ Aman Shankar
WHAT IS THE DIFFERENCE BETWEEN METHOD OVERRIDING AND METHOD HIDING?
1.Creating a method in the derived class with the same signature as a method in the base class is called as method overriding.
2.It is calledd runtime polymorphism.
3.Method overriding needs inheritance.
4.Method overriding needs hierachy level of the classes i.e. one parent class and other child class.
5.It must have same method name as well as the signatures or the parameters.
6.using the keyword virtual and override.
7.Method Overriding only redefines the implementation of the method.
1.Method hiding is also known as shadowing.
2.The method of the parent class is available to the child class without using the override keyword in shadowing.
3. The child class has its own version of the same function.
4.use the new keyword to perform shadowing.
5.Method Hiding can completely redefine the method.
V.Mounika
https://www.geeksforgeeks.org/difference-between-method-overriding-and-method-hiding-in-c-sharp/
How will you differentiate abstract class and interface?
(Must not skip the digging down to this question)
Interfaces are used when you want to define a contract that multiple classes can implement. An interface defines a set of methods, properties, and events that a class must implement to satisfy the contract.
Abstract classes, are used when you want to provide a base class that defines some common behavior or functionality for a group of related classes. Abstract classes can have both concrete and abstract members, and they can be used as a base class for other classes.
-MARUTHI B M
When to Use an Interface
1. If the problem needs to be solved using multiple inheritances and is composed of different class hierarchies
2. When unrelated classes implement our interface. For example, Comparable provides the compareTo() method that can be overridden to compare two objects
3. When application functionalities have to be defined as a contract, but not concerned about who implements the behavior. i.e., third-party vendors need to implement it fully.
4. Consider using the interface when our problem makes the statement “A is capable of [doing this]”. For example, “Clonable is capable of cloning an object”, “Drawable is capable of drawing a shape”, etc.
When to Use an Abstract Class?
1. When trying to use the inheritance concept in code (share code among many related classes), by providing common base class methods that the child class override.
2. If we have specified requirements and only partial implementation details
3. While classes that extend abstract classes have several common fields or methods (that require non-public modifiers)
4. If one wants to have non-final or non-static methods to modify the states of an object
5. Consider using abstract classes and inheritance when our problem makes the evidence “A is a B”. For example, “Dog is an Animal”, “Lamborghini is a Car”, etc.
More infor: https://www.baeldung.com/java-interface-vs-abstract-class
DO INTERFACE CAN HAVE A CONSTRUCTOR?
No, an interface in C# cannot have a constructor because an interface is a contract or specification that defines the signatures of methods, properties, and events that implementing classes must provide. It does not provide any implementation, including constructors.
–Bhanu
CAN YOU CREATE AN INSTANCE OF AN ABSTRACT CLASS OR AN INTERFACE?
No, you cannot create an instance of an abstract class or an interface directly in C#.
The purpose of an abstract class is to provide a base class or template for deriving more specialized classes. You cannot create an instance of an abstract class because it is not fully implemented and contains abstract methods that must be implemented by derived classes.
Similarly, An interface does not contain any implementation itself, so it cannot be instantiated. Instead, it serves as a contract that specifies the behavior that an implementing class must adhere to.
To use an abstract class or an interface, you must create a concrete class that derives from the abstract class or implements the interface.
–Sai Krishna
WHAT IS BOXING AND UNBOXING?
Boxing: Converting value type data into a reference type.
Boxing is an implicit conversion.
Memory location also changes from stack to heap.
Ex: int n=12;
object o = n;
unboxing: Converting Reference type data to value type.
unboxing is an explicit conversion.
Memory location also changes from heap to stack.
ex: n=(int) o
-Yasaswini . G