Showing posts with label object oriented programming. Show all posts
Showing posts with label object oriented programming. Show all posts

Sunday, 21 July 2024

What is Method in c#

 

Class is blue print for the objects

Class contains variables and methods

Variables are used to store data.

Method is block of code to perform specific task ex: AddEmployee ,  deleteEmployee , SendMessage etc.

 

As Part of Method Concept, We have know about following components which involve method definition.






Main Components in Method Declaration

1.     Access Specifier          à Control Access

2.     Method Type               à Static or Instance

3.     Return Value Type     à Result value type or void (nothing to return)

4.     Method Name                          à User Specific

5.     Parameters                   à Input values to method

 

 

Types of Methods in C#

1.       Instance Methods

2.       Static Methods

3.       Extension Methods

4.       Abstract Methods

5.       Virtual Methods

 

Types of Return Values

·       Primitive Types

·       Reference Types

·       Void

·       Tuples

·       Task and Task<T>

 

Types of Parameters

·       Value Parameters

·       Reference Parameters (ref)

·       Output Parameters (out)

·       Parameter Arrays (params)

·       Optional Parameters

 

 


Saturday, 20 July 2024

Class and Object

 

Class is Blueprint & Object is Physical form of class.

Class:

At its core, a class in C# defines the structure and behaviour of objects. It acts as a template or prototype from which individual objects are created.

 

Class and Object are the basic concepts of Object-Oriented Programming which revolve around the real-life entities.

A class is a user-defined blueprint or prototype from which objects are created.

Basically, a class combines the fields and methods(member function which defines actions) into a single unit.

In C#, classes support polymorphism, inheritance and also provide the concept of derived classes and base classes.

 


Class Key points : 

·       String, int ,Boolean and float are inbuilt types where as Class is User Defined Type or Data structure.

·       Integer represents number, string represents text likewise if you want to represent any real world Object example

·       you can create user defined type using class.

·       Class is blue print to create object.

·       A Class Consists of two things.

·       Attributes – Variables

·       Behavior - Methods (Functions)

·       Class is Container where you put all your Variables and Methods.

 

Programming Languages without Class Concept

·       Go (Golang)

·       Rust,

·       Kotlin (in the context of scripting)

·       Dart (in the context of Flutter) ,

·       Swift

 






Saturday, 6 July 2024

Abstract Class

 

Abstract Class: -

Abstract classes are closely related to interfaces they are classes that cannot be instantiated and are frequently are partially implemented or not implemented. One key difference between abstract classes and interfaces is that A class may implemented no of interfaces but May Inherit from only One abstract class.

v  A class that implement abstract class may still implement Interfaces.

v  Abstract class consist of CM(concrete methods)&AM(abstract methods).

v  Abstract class can only be used as base class.

v  Abstract classes declared with abstract keyword.

v  A non abstract class derived from abstract class must provide implementation for all inherited abstract members.

v  An abstract class cannot be sealed.

v  By default abstract members are virtual in nature

v  We cannot create object for abstract class but we can create reference for abstract class.

v  * Virtual methods may are may not override but abstract methods must override in derived class.

v  * Abstract class can have zero or more abstract methods.

v  *If the class does not wish to provide implementation for all the abstract members inherited from the abstract class then that class has to be marked as abstract.

 

 






When to use Abstract Class : When multiple classes required common functionality and code reusability and required to  enforce a contract, and provide a structure for derived classes then abstract is better.

Example : bankaccount is abstract class and having PrintAccount concreate method, Interest as abstract method ,Savings Account,and current Account as  child classes, both classes inherit BankAccount abstract class  and both can use PrintAccount method without any change, and implement Interest method in child class with different interest rates.

Thursday, 13 June 2024

Abstraction vs Encapsulation

 

This is very basic and important question asked in many interviews. This question is basically asked to check if candidate have a good understanding of OOP's (Object Oriented Programming) concepts or not. 

 

Encapsulation

Abstraction

Complexity Hiding.

Showing only what is necessary.

Encapsulated members Direct access and modification not allowed from other class because encapsulated members are private.

Abstracted Members Directly accessible, because abstract members are only public.

Encapsulated members even not visible outside and can’t see weather exists are not.

Abstracted members are visible outside, but can’t see how it is implemented.

Encapsulation binds all members into single unit using class and access specifiers Private, protected, internal etc.

Abstract is layer or interface over the basic implementation methods.

Abstraction can achieve using Interface and abstract class.

Solves problems in implementation level.

Solves Problem in Design Level.

Information hiding

Implementation hiding.

Encapsulation is Inner Layout used in terms of implementation

Example : Inner implementation of mobile phone , how keypad button, display screen connect each other using circuits.

Abstraction – is outer layout used in terms of design.

Example : outer look of mobile phone like it has display screen, keypad button and dialing

Whereas the objects that result in encapsulation need not be abstracted.

The objects that help to perform abstraction are encapsulated.

Encapsulation is closely related to abstraction, as abstracted methods help to give access to encapsulated data in secure manner, because encapsulated data direct access restricted.

Abstraction works together with encapsulation, as encapsulation hide implementation.

 

Abstraction lets you focus on what the object does while Encapsulation means how an object works

 

In summary, encapsulation emphasizes data hiding and access control within an object, while abstraction emphasizes creating a simplified view of a complex system by hiding implementation details. Both principles promote modularity, maintainability, and code reusability in object-oriented programming.

 

 

 


 

Abstraction vs. Encapsulation

This is very basic and important question asked in many Java interviews. This question is basically asked to check if candidate have a good understanding of OOP's (Object Oriented Programming) concepts or not. 

 

Abstraction - In very simple words Abstraction is concept of hiding complexity of a system and showing only necessary details to its user. So user can access the necessary functionalities easily without thinking of internal complexity of the system. Abstraction mainly focusses on outside view or design of the system.

 

In real word scenarios we can take an example of a mobile phone. In mobile we as a user only see's the necessary details or features like screen, buttons etc. and all other complexities (circuits, chips and internal wiring etc) are hidden from us.

 

In  programming abstraction is implemented through interfaces and abstract classes.

 

Encapsulation - Encapsulation is a process of binding members (attributes) and functions (methods) in a single entity, so that they can be protected from outsiders. Encapsulation mainly focus on internal design of a system.

 

In real word scenarios we can take the same mobile phone example, where after binding all attributes and functionality we call it a mobile phone. And after encapsulating everything in a single entity it is safe from outsiders.

 

In programming encapsulation is implemented through class and access modifiers. We encapsulates attributes and methods in a single entity which is called class and we can use access modifiers (public, private, protected and default) to provide different protection levels to internal attributes, methods or the class itself.

 

Abstraction vs Encapsulation n Telugu




Tuesday, 11 June 2024

PolyMorphism

 

Poly = many and Morph = change or form or behaviour(change as per situation)

the word polymorphism says many behaviors

objects or methods acts differently under different conditions 

So, polymorphism is the ability (in programming) to present the same interface for differing underlying forms (data types).


Polymorphism allow us to perform single action in different ways

example :- if ask person to go and speak something with family,friends,teacher,girlfirend


family :- nanna garu bagunnara

frinds :- emi ra jaffa emi chesthunnavu

teacher :- hi sir how r u sir.

girl firend :- hi darling how are u meet avuddam.

same person speak in different modulations based place, single person acts diffrent under different situations.

















Encapsulation

 



Encapsulation is Hide Complexity

Encapsulation is one of the fundamental principle of Object-Oriented Programming (OOP) that promotes data Data security and Data hiding.
Encapsulation Binds the Properties and method into single unit.

You can implement encapsulation using Class and Access Modifiers (Public,Private,protected etc.)

Note : Encapsulated data can not access directly from out side of the class ,if requirement demands we can provide access through the public,internal or through protected methods, so we can do validation based on requirement rather than provide direct access.

Direct access issues :

Encapsulation intention is data security , so if you provide direct access we may face different problems like.
unauthorised persons may get  access.
data may be misused 
data can be deleted or modified.

What is the Benfits of giving encapsulated data access through methods.

Technically encapsulated data is private members so outside class can not accessible, so through public methods we can provide access. while you giving access through methods you do validations as per your requirement.

 Encapsulation solves the security problems in Implementation level.


Features : 

  1. Modularity: Encapsulation promotes modularity by hiding the internal workings of a class from the outside world. This allows for easier maintenance and modification of code since changes to the internal implementation of a class won't affect other parts of the program that use it, as long as the interface remains unchanged.

  2. Information Hiding: Encapsulation allows you to hide the details of how a class works, exposing only what is necessary for other parts of the program to interact with it. This prevents the direct access to the internal state of an object from outside the class, reducing the risk of unintended modifications and ensuring data integrity.

  3. Abstraction: Encapsulation provides a way to abstract away complex implementation details, allowing developers to focus on using objects without needing to understand how they are implemented internally. This simplifies the programming model and makes code easier to understand and maintain.

  4. Encapsulation of Complexity: By encapsulating data and behavior into a single unit, encapsulation helps manage complexity by breaking down a system into smaller, more manageable components. Each class encapsulates a specific set of responsibilities, making it easier to understand and reason about the code.

  5. Controlled Access: Encapsulation allows you to control access to the internal state of an object by using access modifiers such as public, private, and protected. This ensures that data is accessed and modified only through well-defined interfaces, reducing the potential for bugs and making the code more robust.


Encapsulation Summary : 

In summary, encapsulation is a fundamental principle of OOP that promotes data hiding, molecularity, information hiding, and improved security and flexibility in software design.


Encapsulation explanation through Cold capsule


Encapsulation Example


In Real world we can say cold Capsule as Perfect example.

Explanation :-  cold capsule is look like above picture , it is capsule it contains small blue balls and red balls inside 💊 capsule, these are sensitive materials which cure our cold problem,so these blue and red balls are encapsulated inside container, the container protects these sensitive content so these sensitive content people can not touch directly. only when people swallowed capsule then container get melt and sensitive content absorbs by human body.
so here blue balls representing as variables , red balls representing methods and container represented as class, so these variables and methods are encapsulated inside class so unauthorised persons can not access it directly from out side class but we can give access indirectly using methods on conditionally. 


Watch 4 Min Video for Quick revision : 
 


Watch Encapsulation In detailed video  : Coming Soon



Abstraction

Abstraction is one of principle in object oriented programming ,abstraction showing only necessary details (only method name without implementation) to user/client/developer and hide implementation details, so that user can easily understand what methods are there and how to use it by seeing abstract rather than going through entire implementation, abstract is outer layer of implementation class so user interact with abstract.
usage of abstraction is very high in advanced application like micro services, loosely coupled system , design patterns , solid principles etc.


Real World example of abstract :
if you remember your academic project it may contains around 100 papers in record, for every academic project we will prepare abstract it must be you first paper of project book , so your HOD or project manager look into your abstract understand the key points of project.

What we can understand using abstract

abstract is summary of you entire implementation & methods.
abstract is short info of you lengthy code and features.
user can understand by seeing abstract rather than see  the entire implementation and lengthy lines.
Abstract helps users to remember key points of entire implementation.


Abstraction in C#.Net
We can achieve abstraction using Interface and Abstract class in c#, Interface is example for full abstraction because interface only contains abstract members , Abstract class is example for partial abstraction  it contains Abstract members as well Concrete(Normal class) members Depends on the requirement bot are very useful in development.





Abstraction Definition :

In very simple words Abstraction is concept of hiding complexity of a system and showing only necessary details to its user. So user can access the necessary functionalities easily without thinking of internal complexity of the system. Abstraction mainly focuses on outside view or design of the system.

Real world Example : We can take Keypad mobile as real world example , where you see and use display screen , keypad  and speaker but you can not see how it is internally connected keypad,screen each other using circuits inside mobile, because internal connection programming is not required to user.


Note : Abstraction intention is to decrease complexity by showing essential feature and hide internal implementation , most of the People thinks this is to implement security in application that is not 100% correct  since you can implement security in implementation class if required.

Features of Abstraction in OOP's

Highlighting Essential Features:

Abstraction allows the developer to expose only the relevant attributes and methods of an object that are necessary for the client (user of the object). This makes it easier for the client to interact with the object without being overwhelmed by unnecessary details.

Hiding Implementation Details:

By hiding the complex implementation details and exposing only what is necessary, abstraction simplifies the interaction with the object. This helps in managing complexity and makes the system easier to understand and use.


Reducing Complexity:

Abstraction helps in breaking down complex systems into simpler, high-level components. This makes it easier to manage and maintain the system.


Improving Modularity:

By defining clear and simple interfaces, abstraction promotes modular design. Each module or component can be developed, tested, and understood independently.


Enhancing Maintainability:

With abstraction, changes in the implementation of an object do not affect the code that interacts with it, as long as the interface remains consistent. This improves maintainability and allows for easier updates and modifications.


Encouraging Reusability:

Abstract classes and interfaces define common behaviors that can be reused across different parts of the application or even in different projects. This reduces duplication and promotes code reuse.












Other Definition

Abstraction in OOP is about managing complexity by exposing only the necessary details to the client and hiding the implementation details. While it is not primarily about security, it does contribute to a more secure and robust system by encapsulating the internal workings and reducing the risk of unintended interactions.




Video Reference :

https://www.youtube.com/@ASP4UT

https://www.youtube.com/@ASP4U

Sunday, 2 June 2024

Constructor In C#




In C#, a constructor is a special method that is called when an instance of a class is created. It is used to initialize the object's state.

Constructors are fundamental in object-oriented programming for setting up initial values and enforcing rules at the time of object creation.

Every time you create object , you call constructor as well.

 

Here are the key points about constructors in C#:

  1. Name: A constructor has the same name as the class.
  2. No Return Type: Constructors do not have a return type, not even void.
  3. Default Constructor: If no constructor is defined, C# provides a default parameterless constructor.
  4. Parameterized Constructor: You can define constructors that take parameters to initialize the object with specific values.
  5. Overloading: Constructors can be overloaded, meaning you can have multiple constructors in the same class with different parameter lists.
  6. Static Constructor: Used to initialize static members of a class. It is called once, before any instances of the class are created or any static members are referenced.
  7. Copy Constructor: Creates a new object as a copy of an existing object.

 

Constructor Types :

  1. Default Constructor
  2. Parameterized Constructor
  3. Static Constructor
  4. Copy Constructor
  5. Private Constructor
  6. Constructor Chaining
  7. Base Constructor


Accessibility:

  • Constructors can have different access modifiers (public, private, protected, internal).
  • A private constructor is used to restrict instantiation and can be used in singleton patterns.

 

Overloading:

  • Constructors can be overloaded by defining multiple constructors with different parameters.
  • This allows creating objects in different ways.

Chaining Constructors:

  • A constructor can call another constructor in the same class using the this keyword.
  • This helps in reusing constructor code and reducing redundancy.

 

 

Inheritance and Base Constructors:

  • When a class inherits from a base class, the derived class can call a constructor of the base class using the base keyword.
  • This ensures that the base class is properly initialized before the derived class.

 

Static Constructors:

  • Static constructors cannot take parameters.
  • They are called automatically to initialize the class before any static members are accessed.
  • They are typically used to initialize static fields.

 

Exceptions in Constructors:

  • Constructors can throw exceptions if initialization fails.
  • It is important to handle such exceptions appropriately to avoid runtime errors.

 

No Inheritance for Constructors:

  • Constructors are not inherited. Each class must define its own constructors.
  • The derived class constructors can call base class constructors to ensure proper initialization.