OOPs difference faqs-3
-
Upload
umar-ali -
Category
Technology
-
view
506 -
download
0
description
Transcript of OOPs difference faqs-3
Difference between Abstraction and Encapsulation
Abstraction Encapsulation
Abstraction solves the problem in the design level.
Encapsulation solves the problem in the implementation level.
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.
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.
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
Difference between Composition and Aggregation
Composition Aggregation
Defines a strong-coupled relationship between two entities, where the one entity is part of another, and both need each other for their existence.
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. Human body and the Heart. e.g.School and teacher.
Composition implies real ownership of its components
Aggregation does not necessarily own any of its aggregates.
Composition has a stronger bond of its components.
Aggregation has weaker or looser bonds with its aggregates.
Composition has components that exist at the inner level.
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 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.
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.
public class A { private class B { } B b = new B(); }public class C { A.B b = new A.B(); // ERROR }
public sealed class A { }public class B : A //ERROR { }
The main use of Private class is to create a user-defined type, which we want to be accessible to that class only.
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."
The sealed classes are mainly used to prevent inheritance features of object oriented programming.
Difference between Static Class and Sealed Class
Static Class Sealed Class
We can neither create their instances, nor inherit them
We can create their instances, but cannot inherit them
They can have static members only. They can contain static as well as nonstatic members.
ex: static class Program
ex:
{
}
sealed class demo {
}
class abc:demo { --Wrong }
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.
Difference between Virtual method and Abstract method
Feature Virtual method Abstract method
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
An abstract method should be overriden by inherited class.
i.e.,Abstract method forces the derived class to override it.
abstract == MustOverride
Implementation Virtual method has an implementation.
Abstract method does not provide an implementation.
Necessity to Implement
Virtual methods allow subclasses to provide their own implementation of that method using the override keyword
Abstract methods in a class contain no method body, and are implicitly virtual
Scope Virtual methods scope to members only.
Abstract method's scope to members and classes
Instancing Virtual methods - Not applicable, as we can't create instance for members, it is possible only with 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.
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 Specifier.
In Static Class, Constructor does not have Access Specifier.
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 .
In Class, Class members can be accessed through class object.
In Static Class, members can be accessed through its Class name only
Difference between Method Overloading and Method overriding in C#
Method Overloading Method overriding
Method Overloading is passing same message for different functionality
Method Overriding is redifining parent class function to the child class
Method Overloading is between the same function name with the different signature
Method Overriding is between the same method.
Method Overloading does not check for the return type.
Method Overriding checks the return type.
Method Overloading takes place in the same class.
Method Overriding takes place between parent and child classes
Method Overloading is static binding Method Overriding is a dynamic binding.