You are on page 1of 5

Difference between Abstraction and Encapsulation Abstraction Abstraction solves the problem in the design level.

Encapsulation Encapsulation solves the problem in the 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 the object does instead of how it does it 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 means hiding the internal details or mechanism of how an object does something. 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

Difference between Composition and Aggregation Composition Defines a strong-coupled relationship between two entities, where the one entity is part of another, and both need each other for their existence. e.g. Human body and the Heart. Composition implies real ownership of its components Composition has a stronger bond of its components. Composition has components that exist at the inner level. Aggregation Defines a weak-coupled relationship between two entities, where one entity could be part of another, but either can exist without the other, independantly. e.g.School and teacher. Aggregation does not necessarily own any of its aggregates. Aggregation has weaker or looser bonds with its aggregates. Aggregation has aggregates that live at 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 constructor and therefore we can create an instance of that class. public class A { private class B { } B b = new B(); } public class C { A.B b = new A.B(); // ERROR } In Sealed class we can not create a constructor of that class, so no instance of that class is possible. public sealed class A { } public class B : A //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 We can neither create their instances, nor inherit them They can have static members only. ex: static class Program Sealed Class We can create their instances, but cannot inherit them They can contain static as well as nonstatic members. ex:

{ }

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 Overriding

Virtual method

Abstract method

Virtual method may or may An abstract method should be not override by inherited class. overriden by inherited class. i.e.,Virtual method provide the derived class with the option of overriding it. Virtual = = Overridable i.e.,Abstract method forces the derived class to override it. abstract == MustOverride Abstract method does not provide an implementation. Abstract methods in a class contain no method body, and are implicitly virtual Abstract method's scope to members and classes Abstract method - direcly 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.

Implementation Necessity to Implement

Virtual method has an implementation. Virtual methods allow subclasses to provide their own implementation of that method using the override keyword Virtual methods scope to members only. Virtual methods - Not applicable, as we can't create instance for members, it is possible only with classes.

Scope Instancing

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 Class has Instance Members In Class, Constructor has Access Specifier. In Class Constructor, initiation is done every time when an object is created for the class In Class, Class members can be accessed through class object. Static Class Static class does not have Instance Members In Static Class, Constructor does not have Access Specifier. In Static Class ,Constructor will be called only one time .

In Static Class, members can be accessed through its Class name only

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

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

You might also like