OOPs Difference FAQs-1

  1. What are the differences between TypeOf() and GetType()?
S.No TypeOf() GetType()
1 Its an operator Its a method
2 Can't be overloaded Has lot of overloads
  1. What are the differences between const and readonly?
S.No const readonly
1 It cannot be static It can be instance level or static
2 It is evaluated at design time It is evaluated at run time
3 It is initialized at declaration It is initialized at declaration or in constructor
4 It must be of integral type or enumeration In addition, it can have complex types with new keyword and enumerations are not allowed
  1. What are the Differences between Abstract Class and Interface?
S.No Abstract Class Interface
Default Implementation:
In Abstract Class we can provide default implementation.
Default Implementation:
Interface has only signature, we cannot provide implementation in interface.
A class can inherit only one abstract class
A Class can implement any number of Interfaces
When to use ?
We go for Abstract classes on such situations where we need to give common functionality for group of related classes
When to use ?
We go for Interface on such situations where we need to give common functionality for group of un-related classes
Default Implementation Pros/Cons:
If we add a new method, then we can provide a default implementation and so no need to make any change to existing work
Default Implementation Pros/Cons:
If we add a new method, then we need to change all the existing work
Constants type support:
Static and Instance constants are possible
Constants type support:
Only Static constants are possible
Support for Automatic Properties:
Abstract class can have automatic properties.
Support for Automatic Properties:
Interface cannot have automatic properties. We need to implement all the properties defined in interface into all it’s implementors.
Access Modifiers (public, private, protected, internal, protected internal):
Abstract Class can have access modifiers for subs, functions, properties
Access Modifiers (public, private, protected, internal, protected internal):
Interface does not have access modifiers for subs, functions, properties. Everything defined inside the interface is assumed public modifier
Abstract Class suits to implement core requirements that is shared among common type of objects. i.e. Person abstract class can be inherited by Manager, Supervisor, Clerk, Administrator…
Interface suits to implement peripheral requirements. i.e. both person and vehicle class can implement IRun interface.
Support for Data Fields:
Abstract class can have data fields.
Support for Data Fields:
Interface cannot contain data fields. However in .NET we can have abstract properties in interface. However just like methods we need to implement those abstract properties inside the implementor class.
An abstract class is fast
Interfaces require more time to find the actual method in the corresponding classes. Hence it is slower than abstract class.
  1. What are the differences between Structure and Class?
    S.No Structure Class
    1 It is value type It is reference type
    2 It is stored on stack It is stored on heap
    3 It does not support inheritance It supports inheritance
    It is suitable for small data structure usually where inheritance, overriding is not required
    example: int a=100;
    It is suitable for complex data structures where inheritance, overriding is required
    or we need to create objects
    example: DataSet,ArrayList can handle large data.
    5 'this' pointer it will not work in structure. 'this' pointer will work only in class.
    6 Structure cannot be declared as private or protected. Class can be declared public as well as private.
    7 Structure contains only data member
    Class contains both data member and member function

    8 Default access specifier for structure is public Default access specifier for class is private
    Structure does not require constructor
    Class requires constructor
    Structures are
    not destroyed using GC.
    Objects created from classes are terminated using Garbage collector.
    Faster in access.
    Not faster in access than structure.
    Supports only interface inheritance.
    Supports both implemenation and interface inheritance.
    They do not have destructors
    Classes have destructors
    They do no have explicit parameterless constructors
    They can have explicit parameterless constructors
    We cannot put sealed /abstract modifiers before the structures.
    Sealed/abstract modifers can be put before classes.
    Easier memory management
    Comparitively Difficult memory management
    17 examples: int, short,long,DateTime,
    Point (predefined)
    examples: SqlConnection,DataView(predefined classes)
  2. What are the differences between property and indexer?
S.No Property Indexer
1 Identified by its name. Identified by its signature.
2 Accessed through a simple name or a member access. Accessed through an element access.
3 Can be a static or an instance member. Must be an instance member.
4 A get accessor of a property has no parameters. A get accessor of an indexer has the same formal parameter list as the indexer.
5 A set accessor of a property contains the implicit value parameter. A set accessor of an indexer has the same formal parameter list as the indexer, in addition to the value parameter.
  1. What are the differences between overloading and overriding?
    S.No Overloading Overriding
    1 Same name in same / derived class but with different / type of parameter We need to provide different implementation than base class
    2 Has different signature Has same signature
    3 Otherwise called Compile-time Polymorphism Otherwise called Run-time Polymorphism
  1. What are the differences between Value Types and Reference Types?
S.No Value Types Reference Types
1 It is stored on stack It is stored on heap
2 It can be accessed directly It can be accessed through references.Reference type variable will hold object reference.
3 Life time of value type is determined by lifetime of variable that contain them Lifetime of reference type is managed by .net framework
4 Examples: All numeric data type, Boolean, char, Date, Structure, enumerations Examples: All arrays, String, Class types, Delegate
5 Value types do not accept null value. Value types can be made to accept null values by using null coalescence operator. Reference type variables can hold null values.
6 Value types cannot be inherited. Reference types can be inherited.
7 Value types other than structure cannot implement interfaces. Class and Interface of Reference types can implement interfaces.
8 Boxing is performed to convert value type to an object and unboxing is performed to convert an object into a value type. You can directly assign a reference type to an object and an object to a reference type without boxing and unboxing.
9 Value type is inherited from System.ValueType namespace which is inherited from System.Object. Reference type is inherited from System.Object directly.
10 When one value type variable is assigned to another, both value type variables will contain same data but independent copy. Changing value of one value type variable will not impact value of the other value type variable. When one reference type variable is assigned to another, both reference type variables will be referencing to the same object. If one reference type changes value of the object then the other reference type variable will also point to the modified object.
11 CLR (Common Language Runtime) does not perform memory management of value types. Memory management of reference types is automatically done by CLR.
12 Value types are primitive data types. Reference types are not primitive in nature. Reference types deal with objects.

Note: Object is not any kind of type. You can create object of structure as well as Class

Are not type: Namespaces, Modules, Events, properties, procedures, variables, constants, & fields.

No comments:

Post a Comment