1.Difference
between Abstraction and Encapsulation
2.Difference
between Composition and Aggregation
3.Difference
between Private Class and Sealed Class
5.Difference
between Virtual method and Abstract method
7.Difference between Method Overloading and Method overriding in C#
-
S.No AbstractionEncapsulation1Abstraction 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 itEncapsulation 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
-
S.No CompositionAggregation1Defines 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 componentsAggregation 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.
-
S.No Private ClassSealed Class1A 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.2In 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.3The 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 ClassSealed Class1We can neither create their instances, nor inherit themWe can create their instances, but cannot inherit them2They can have static members only.They can contain static as well as nonstatic members.3Static 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
}
-
S.No Virtual methodAbstract method1Overriding :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
2Implementation:Virtual method has an implementation.Implementation:Abstract method does not provide an implementation.3Necessity to Implement:Virtual methods allow subclasses to provide their own implementation of that method using the override keywordNecessity to Implement:Abstract methods in a class contain no method body, and are implicitly virtual4Scope :Virtual methods scope to members only.Scope :Abstract method's scope to members and classes5Instancing :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 1Class has Instance MembersStatic class does not have Instance Members2In Class, Constructor has Access Specifier.In Static Class, Constructor does not have Access Specifier.3In Class Constructor, initiation is done every time when an object is created for the classIn Static Class ,Constructor will be called only one time4In 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
1Method Overloading is passing same message for different functionalityMethod Overriding is redifining parent class function to the child class2Method Overloading is between the same function name with the different signatureMethod Overriding is between the same method.3Method Overloading does not check for the return type.Method Overriding checks the return type.4Method Overloading takes place in the same class.Method Overriding takes place between parent and child classes5Method Overloading is static bindingMethod Overriding is a dynamic binding.
No comments:
Post a Comment