Difference between Abstraction and Encapsulation

               Abstraction                                Encapsulation
 Abstraction solves the problem in the       Encapsulation solves the problem in the
 design level.                               implementation level.
 Abstraction is used for hiding the      Encapsulation means hiding the code
 unwanted data and giving relevant data. and data into a single unit to protect the
                                         data from outside world.
 Abstraction allows us to focus on what      Encapsulation means hiding the internal
 the object does instead of how it does it   details or mechanism of how an object
                                             does something.
 Abstraction- Outer layout, used in          Encapsulation- Inner layout, used in
 terms of design.                            terms of implementation.
 For Example:-                               For Example:- Inner Implementation
  Outer Look of a Mobile Phone, like it      detail of a Mobile Phone, how keypad
 has a display screen and keypad buttons     button and Display Screen are
 to dial a number.
                                             connected with each other using circuits


Difference between Composition and Aggregation

               Composition                                 Aggregation
 Defines a strong-coupled relationship       Defines a weak-coupled relationship
 between two entities, where the one         between two entities, where one entity
 entity is part of another, and both need    could be part of another, but either can
 each other for their existence.             exist without the other, independantly.

 e.g. Human body and the Heart.              e.g.School and teacher.

 Composition implies real ownership of       Aggregation does not necessarily own
 its components                              any of its aggregates.
 Composition has a stronger bond of its      Aggregation has weaker or looser bonds
 components.                                 with its aggregates.
 Composition has components that exist       Aggregation has aggregates that live at
 at the inner level.                         the outer level.
Difference between Private Class and Sealed Class

               Private Class                               Sealed Class
 A Private class can only be accessed by A Sealed class can be accessed by any
 the class it is defined and contain within class.Private Constructor of a Private
 - it is completely inaccessible to outside Class = Sealed Class.
 classes.
 In private Class,we can create a            In Sealed class we can not create a
 constructor and therefore we can create     constructor of that class, so no instance
 an instance of that class.                  of that class is possible.
 public class A                              public sealed class A
  {                                            {
  private class B                              }
    {                                        public class B : A //ERROR
    }                                          {
  B b = new B();                               }
  }
 public class C
  {
  A.B b = new A.B(); // ERROR
  }
 The main use of Private class is to create The sealed classes are mainly used to
 a user-defined type, which we want to be prevent inheritance features of object
 accessible to that class only.             oriented programming.

 Private class(i.e private constructor) is
 also used to implement singleton
 classes(pattern). Singleton means "A
 single-instance object, and it simplify
 complex code. Singletons have a static
 property that we must access to get the
 object reference."

Difference between Static Class and Sealed Class

                Static Class                               Sealed Class
 We can neither create their instances,      We can create their instances, but cannot
 nor inherit them                            inherit them
 They can have static members only.          They can contain static as well as
                                             nonstatic members.
 ex:                                         ex:
 static class Program
{                                          sealed class demo
                                            {
 }
                                            }

                                            class abc:demo
                                            {
                                            --Wrong
                                            }
 Static classes are used when a class        The sealed classes are mainly used to
 provides functionality that is not specific prevent inheritance features of object
 to any unique instance.                     oriented programming.

Difference between Virtual method and Abstract method


      Feature                Virtual method                  Abstract method
 Overriding           Virtual method may or may        An abstract method should be
                      not override by inherited class. overriden by inherited class.

                      i.e.,Virtual method provide      i.e.,Abstract method forces
                      the derived class with the       the derived class to
                      option of overriding it.         override it.

                      Virtual = = Overridable          abstract == MustOverride
 Implementation       Virtual method has an            Abstract method does not
                      implementation.                  provide an implementation.
 Necessity to         Virtual methods allow            Abstract methods in a
 Implement            subclasses to provide their      class contain no method
                      own implementation of that       body, and are implicitly
                      method using the override        virtual
                      keyword
 Scope                Virtual methods scope to         Abstract method's scope to
                      members only.                    members and classes
 Instancing           Virtual methods - Not            Abstract method - direcly
                      applicable, as we can't          NO, but other way, Yes.
                      create instance for              We can create an instance
                      members, it is possible only     of a class that derives from
                      with classes.                    an abstract class. And we
                                                       can declare a type Abstract
                                                       class and instantiate that
                                                       as a derived class.
Example:

 public abstract class Test
 {

   public abstract void A();          // Abstract method

   public virtual void B()
   {
     Console.WriteLine("Test.B"); } // Virtual Method
   }

     public class InheritedTest : Test
     {

         public override void A()
         {
          Console.WriteLine("InheritedTest.A");
         }

         //Method B implementation is optional

         public override void B()
         {
          Console.WriteLine("InheritedTest.B");
         }
     }



Difference between Class and Static Class

                   Class                                  Static Class
 Class has Instance Members                 Static class does not have Instance
                                            Members
 In Class, Constructor has Access           In Static Class, Constructor does not
 Specifier.                                 have Access Specifier.
 In Class Constructor, initiation is done   In Static Class ,Constructor will be
 every time when an object is created       called only one time .
 for the class

 In Class, Class members can be             In Static Class, members can be
 accessed through class object.             accessed through its Class name only
Difference between Method Overloading and Method overriding in C#


         Method Overloading                        Method overriding
 Method Overloading is passing same      Method Overriding is redifining parent
 message for different functionality     class function to the child class
 Method Overloading is between the       Method Overriding is between the same
 same function name with the different   method.
 signature
 Method Overloading does not check for Method Overriding checks the return
 the return type.                      type.
 Method Overloading takes place in the   Method Overriding takes place between
 same class.                             parent and child classes
 Method Overloading is static binding    Method Overriding is a dynamic
                                         binding.

OOPs difference faqs-3

  • 1.
    Difference between Abstractionand Encapsulation Abstraction Encapsulation Abstraction solves the problem in the Encapsulation solves the problem in the design level. implementation level. Abstraction is used for hiding the Encapsulation means hiding the code unwanted data and giving relevant data. and data into a single unit to protect the data from outside world. Abstraction allows us to focus on what Encapsulation means hiding the internal the object does instead of how it does it details or mechanism of how an object does something. Abstraction- Outer layout, used in Encapsulation- Inner layout, used in terms of design. terms of implementation. For Example:- For Example:- Inner Implementation Outer Look of a Mobile Phone, like it detail of a Mobile Phone, how keypad has a display screen and keypad buttons button and Display Screen are to dial a number. connected with each other using circuits Difference between Composition and Aggregation Composition Aggregation Defines a strong-coupled relationship Defines a weak-coupled relationship between two entities, where the one between two entities, where one entity entity is part of another, and both need could be part of another, but either can each other for their existence. exist without the other, independantly. e.g. Human body and the Heart. e.g.School and teacher. Composition implies real ownership of Aggregation does not necessarily own its components any of its aggregates. Composition has a stronger bond of its Aggregation has weaker or looser bonds components. with its aggregates. Composition has components that exist Aggregation has aggregates that live at at the inner level. the outer level.
  • 2.
    Difference between PrivateClass and Sealed Class Private Class Sealed Class A Private class can only be accessed by A Sealed class can be accessed by any the class it is defined and contain within class.Private Constructor of a Private - it is completely inaccessible to outside Class = Sealed Class. classes. In private Class,we can create a In Sealed class we can not create a constructor and therefore we can create constructor of that class, so no instance an instance of that class. of that class is possible. public class A public sealed class A { { private class B } { public class B : A //ERROR } { B b = new B(); } } public class C { A.B b = new A.B(); // ERROR } The main use of Private class is to create The sealed classes are mainly used to a user-defined type, which we want to be prevent inheritance features of object accessible to that class only. oriented programming. Private class(i.e private constructor) is also used to implement singleton classes(pattern). Singleton means "A single-instance object, and it simplify complex code. Singletons have a static property that we must access to get the object reference." Difference between Static Class and Sealed Class Static Class Sealed Class We can neither create their instances, We can create their instances, but cannot nor inherit them inherit them They can have static members only. They can contain static as well as nonstatic members. ex: ex: static class Program
  • 3.
    { sealed class demo { } } class abc:demo { --Wrong } Static classes are used when a class The sealed classes are mainly used to provides functionality that is not specific prevent inheritance features of object to any unique instance. oriented programming. Difference between Virtual method and Abstract method Feature Virtual method Abstract method Overriding Virtual method may or may An abstract method should be not override by inherited class. overriden by inherited class. i.e.,Virtual method provide i.e.,Abstract method forces the derived class with the the derived class to option of overriding it. override it. Virtual = = Overridable abstract == MustOverride Implementation Virtual method has an Abstract method does not implementation. provide an implementation. Necessity to Virtual methods allow Abstract methods in a Implement subclasses to provide their class contain no method own implementation of that body, and are implicitly method using the override virtual keyword Scope Virtual methods scope to Abstract method's scope to members only. members and classes Instancing Virtual methods - Not Abstract method - direcly applicable, as we can't NO, but other way, Yes. create instance for We can create an instance members, it is possible only of a class that derives from with classes. an abstract class. And we can declare a type Abstract class and instantiate that as a derived class.
  • 4.
    Example: public abstractclass Test { public abstract void A(); // Abstract method public virtual void B() { Console.WriteLine("Test.B"); } // Virtual Method } public class InheritedTest : Test { public override void A() { Console.WriteLine("InheritedTest.A"); } //Method B implementation is optional public override void B() { Console.WriteLine("InheritedTest.B"); } } Difference between Class and Static Class Class Static Class Class has Instance Members Static class does not have Instance Members In Class, Constructor has Access In Static Class, Constructor does not Specifier. have Access Specifier. In Class Constructor, initiation is done In Static Class ,Constructor will be every time when an object is created called only one time . for the class In Class, Class members can be In Static Class, members can be accessed through class object. accessed through its Class name only
  • 5.
    Difference between MethodOverloading and Method overriding in C# Method Overloading Method overriding Method Overloading is passing same Method Overriding is redifining parent message for different functionality class function to the child class Method Overloading is between the Method Overriding is between the same same function name with the different method. signature Method Overloading does not check for Method Overriding checks the return the return type. type. Method Overloading takes place in the Method Overriding takes place between same class. parent and child classes Method Overloading is static binding Method Overriding is a dynamic binding.