Showing posts with label OOPS. Show all posts
Showing posts with label OOPS. Show all posts

Wednesday, 4 August 2021

What is the differnece between Abstract class & Interface?

In an abstract class, we can create the functionality and that needs to be implemented by the derived class. The interface allows us to define the functionality or functions but cannot implement that. The derived class extends the interface and implements those functions.

Abstract ClassInterface
Abstract classes are inherited.Interfaces are Implemented.
It is a half-defined parent class.It is a contract.
Share some common logic in child classesPlanning Abstraction.
It contains both declaration and definition parts.It contains only a declaration part.
It contains both declaration and definition parts.It contains only a declaration part.
Multiple inheritance is not achieved by abstract class.Multiple inheritance is achieved by interface.
It contain constructor.It does not contain constructor.
It can contain static members.It does not contain static members.
It can contain different types of access modifiers like public, private, protected etc.It only contains public access modifier because everything in the interface is public.
The performance of an abstract class is fast.The performance of interface is slow because it requires time to search actual method in the corresponding class.
It is used to implement the core identity of class.It is used to implement peripheral abilities of class.
A class can only use one abstract class.A class can use multiple interface.
If many implementations are of the same kind and use common behavior, then it is superior to use abstract class.If many implementations only share methods, then it is superior to use Interface.
Abstract class can contain methods, fields, constants, etc.Interface can only contain methods .
It can be fully, partially or not implemented.
It should be fully implemented.
Sr. No.KeyAbstract ClassInterface
1DefinitionIn terms of standard definition, an Abstract class is, conceptually, a class that cannot be instantiated and is usually implemented as a class that has one or more pure virtual (abstract) functions.On other hand an Interface is a description of what member functions must a class, which inherits this interface, implement. In other words, an interface describes behaviour of the class.
2ImplementationAs like of other general class design in C# Abstract class also have its own implementation along with its declaration.On other hand an Interface can only have a signature, not the implementation. While its implementation is being provided by the class which implements it.
3InheritanceAs per specification in C# a class can extends only one other class hence multiple inheritance is not achieved by abstract class.On other hand in case of Interface a class can implements multiple interfaces and hence multiple inheritance is achieved by interface.
4ConstructorLike other classes in C# for instantiation abstract class also have constructor which provide an instance of abstract class to access its non-static methods.On other hand Interface do not have constructor so we can't instantiate an interface directly although its method could get accessed by creating instance of class which implementing it.
5ModifiersAs abstract class is most like of other ordinary class in C# so it can contain different types of access modifiers like public, private, protected etc.On other hand as Interface needs to be get implemented in order to provide its methods implementation by other class so can only contains public access modifier.
6PerformanceAs abstract class have its method as well as their implementations also for its abstract methods implementation it have reference for its implementing class so performance is comparatively faster as compare to that of Interface.On other hand the performance of interface is slow because it requires time to search actual method in the corresponding class.
Watch video to clear the concept-

Friday, 23 May 2014

What is Classes and Objects in C# .NET ?

Objects are created from Classes, The Windows Form itself is a Class, and when you run your program, you are creating Objects: a Form object, a button object, a Textbox object, etc.


Class:

A Class is simply a block of code that does a particular job. You might have a class that handles all your database work, for example, or one that does error checking on Textboxes.
The idea is that you can reuse this code (Class) whenever you need it, or when you need it in other projects. It saves you from having to write the same thing over and over again.

Think of a Class as a recipe. If you had a recipe for a chicken biryani, the recipe will tell you what you need to do to make the biryani. But it's not the biryani itself. It's the instructions for the biryani. If you have the recipe, you can make chicken biryani whenever you need them.


Object:

An Object is the thing that the recipe makes - The Chicken biryani itself. You do all the coding in a Class (recipe), and then instruct C# to make an object (Chicken). 

But all you are really doing with Classes and Objects is trying to separate code into blocks, so that it can be reused. When the code is being used, it's an object. But the object is created from your code (Class).

An object is an instance of a class through which we access the methods of that class. “New” keyword is used to create an object. A class that creates an object in memory will contain the information about the methods, variables and behavior of that class.

Example:

Reference:
http://www.c-sharpcorner.com/UploadFile/84c85b/object-oriented-programming-using-C-Sharp-net/ 

What is an Interface?

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.

What is the difference between Object and Instance?

  • An instance of a user-defined type is called an object. We can instantiate many objects from one class.
  • An object is an instance of a class.
Example:   ClassName Object = new ClassName();

Tuesday, 20 May 2014

What is the difference between early binding and late binding?

Calling a non-virtual method, decided at a compile time is known as early binding. Calling a virtual method (Pure Polymorphism), decided at a runtime is known as late binding.

Sunday, 4 May 2014

What is Polymorphism?

Behaving in different ways depending upon input receive is know as polymorphism. i.e. whenever input changes automatically output or behavior also changes.

There are basically two types of polymorphism in c# i.e.

Static/Compiletime/Early Binding 
Dynamic/Runtime/ Late Binding


Static- Static polymorphism is also called as Compile Time polymorphism. In Static polymorphism methods are overloaded  with same name but having different signatures.So it is called as method overloading.

 - In case of overloading methods whenever we call a method for that method call based on parameter if identifies which method should be executed and binds the method calls its method definition and method executed in runtime.

Dynamic-Dynamic polymorphism is also called as Run Time polymorphism. In this type of polymorphism methods have the same name, same signature but different in the implementation.

In Dynamic polymorphism methods are overridden so it also called as method overriding. 

During run time, Method overriding can be achieved by using inheritance principle and using "virtual" and "override" keyword. so this type of polymorphism can also be called as late binding.
In late binding compiler doesn't know what kind of methods it has to call and which can be achieved only during the run time.so it is called as run time polymorphism.


Polymorphism can be implemented in object oriented programming language using three different approach:

1.Overloading

2.Overriding
3.Hiding/Shadowing

Overloading is again of 3 types-



  • Method Overloading
  • Constructor Overloading
  • Operator Overloading

Method Overloading:

  • Its an approach of defining multiple methods under a class with the same name by changing their signature.
  • changing the signature in the sense, we can change the numbers of parameter being passed to method , type of parameter being pass to the method , order of parameter.
Example:
public class Calculation

{


        public int Sum(int a, int b)

        {

            return a + b;

        }


        public float Sum(int xint yfloat z)

        {

            return x + y + z;

        }

        

}

Constructor Overloading:
  • Defining multiple constructor in a class with a different signature is known as constructor overloading i.e. just like we overload methods of a class , we can also overload constructor of a class.
  • If a class contains multiple constructors in it , object of that class can be created by calling any of available constructor it is not require to call a specific constructor for creating the object.
Example:

Note-   
  • Generally we overload constructor of a class so that variable of that class can be initialized either with default values with the help of parameter less constructor or can be initialized with the given values that are passed as parameter to the constructor while creating the object .
  • Parameter less constructor also known as default constructors, they will initialize variable of a class with the default value.   
Operator Overloading:
  • This is something very much similar to the concept of method overloading which allows to define multiple behaviors to an operator.
  • In method overloading the behavior of method changes according to the type of parameter being passed to the method , whereas in the operator overloading the behavior of an operator changes according to the operands types between which we use the operator.
  • '+' is an overloaded operator which can be used both for addition as well as concatenation also. it works as a addition operator when used between numeric operands and works as a concatenation operator when used between string and numeric  operands.
Example: + is already overloaded operator under the base class.

Syntax: [<modifier>]static<type>operator<opt>(<oprand type def>)
            {
                 -stmnt-
            } 
  • <type> : implies the type of result we are expecting when the operator is used between two operands.
  • <operand type definition>: in the sense the type of operands between which we want to use the operator.
  • operator: is the name of the method that can naot be changed.
  • <opt>: iplies the operator we want to overload. 
Method Overriding:
  • If at all parent classes method is redifined or reimplemented under the child class , exactly with the same  name & signature , we call it as method overriding.
  • Method overriding is possible only in derived classes, but not within the same class. 
  • When derived class needs a method with same signature as in base class, but wants to execute different code than provided by base class then method overriding will be used.
  • To allow the derived class to override a method of the base class, C# provides two options,virtual methods and abstract methods.
  • Method overriding in c# is achieved through inheritance.
Class1
public virtual void show() // overridable
class2: class1
public override void show() // overriding

Example 1:



Example 2:implementing abstract method

Method Hiding/Shadowing:

  • This is another approach that is used for re-implementing a parent class method under child class . A parent classes method can be re-implemented under its child class using 2 different approaches:- 1.overriding ,2. hiding/shadowing
  •  in the first case we re implemented the parent class method that are declared as virtual under the child class by using override modifier wherese in second case  we remplemented the parent classes method under child class even if they not declared as virtual ie. re implementation being performing without parent classes permission.
Example:





How to improve applications performance which is hosted in cloud ?

Improving the performance of an application hosted in Microsoft Azure involves a combination of optimizing your application code, leveraging...