8.07.2012

OOPs Difference FAQs-3

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

2.Difference between Composition and Aggregation


S.No
Composition
Aggregation
1
Defines a strongly-coupled relationship between two entities, where the one entity is part of another, and both need each other for their existence.
Defines a weakly-coupled relationship between two entities, where one entity could be part of another, but either can exist without the other, independantly.
2
e.g. Human body and the Heart.
e.g.School and teacher.
3
Composition implies real ownership of its components
Aggregation does not necessarily own any of its aggregates.
4
Composition has a stronger bond of its components.
Aggregation has weaker or looser bonds with its aggregates.
5
Composition has components that exist at the inner level.
Aggregation has aggregates that live at the outer level.

3.Difference between Private Class and Sealed Class
S.No
Private Class
Sealed Class
1
A Private class can only be accessed by the class it is defined and contain within - it is completely inaccessible to outside classes.
A Sealed class can be accessed by any class.Private Constructor of a Private Class = Sealed Class.
2
In private Class,we can create a constructor and therefore we can create an instance of that class.
In Sealed class we can not create a constructor of that class, so no instance of that class is possible.
3
The main use of Private class is to create a user-defined type, which we want to be accessible to that class only.
The sealed classes are mainly used to prevent inheritance features of object oriented programming.

Note: 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."
Example for Private Class:

public class A
  {
  private class B
     {
     }
  B b = new B();
  }
public class C
  {
  A.B b = new A.B(); // ERROR
  }
Example for Sealed Class:

public sealed class A
   {
   }
public class B : A //ERROR
   {
   }
4.Difference between Static Class and Sealed Class
S.No
Static Class
Sealed Class
1
We can neither create their instances, nor inherit them
We can create their instances, but cannot inherit them
2
They can have static members only.
They can contain static as well as nonstatic members.
3
Static classes are used when a class provides functionality that is not specific to any unique instance.
The sealed classes are mainly used to prevent inheritance features of object oriented programming.

Example for Static Class:

static class Program 
{ 

} 
Example for Sealed Class:
sealed class demo 
{ 

} 

class abc:demo 
{ 
--Wrong 
} 

5.Difference between Virtual method and Abstract method
S.No
Virtual method
Abstract method
1
Overriding :
Virtual method may or may not override by inherited class.

i.e.,Virtual method provide the derived class with the option of overriding it.
Virtual = = Overridable
Overriding :
An abstract method should be overriden by inherited class.

i.e.,Abstract method forces the derived class to override it.

abstract == MustOverride
2
Implementation:
Virtual method has an implementation.
Implementation:
Abstract method does not provide an implementation.
3
Necessity to Implement:
Virtual methods allow subclasses to provide their own implementation of that method using the override keyword
Necessity to Implement:
Abstract methods in a class contain no method body, and are implicitly virtual

4
Scope :
Virtual methods scope to members only.
Scope :
Abstract method's scope to members and classes
5
Instancing :
Virtual methods - Not applicable, as we can't create instance for members, it is possible only with classes.
Instancing :
Abstract method - directly NO, but other way,Yes.We can create an instance of a class that derives from 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"); 
            }
        }
6.Difference between Class and Static Class
S.No
Class Static Class
1
Class has Instance Members
Static class does not have Instance Members
2
In Class, Constructor has Access Specifier.
In Static Class, Constructor does not have Access Specifier.
3
In Class Constructor, initiation is done every time when an object is created for the class
In Static Class ,Constructor will be called only one time
4
In Class, Class members can be accessed through class object.
In Static Class, members can be accessed through its Class name only


7.Difference between Method Overloading and Method overriding in C#
S.No
Method Overloading
Method Overriding
1
Method Overloading is passing same message for different functionality
Method Overriding is redifining parent class function to the child class
2
Method Overloading is between the same function name with the different signature
Method Overriding is between the same method.

3
Method Overloading does not check for the return type.
Method Overriding checks the return type.
4
Method Overloading takes place in the same class.
Method Overriding takes place between parent and child classes
5
Method Overloading is static binding
Method Overriding is a dynamic binding.

No comments:

Post a Comment