Sunday, 28 January 2018

OOPS Concepts


Class

 It is a collection of objects.

Object

Objects are the basic run-time entities of an object-oriented system. They may represent a person, a place or any item that the program must handle. 
Encapsulation
·         Wrapping up a data member and a method together into a single unit (in other words class) is called Encapsulation.
·         Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object.
·         Encapsulation is like your bag in which you can keep your pen, book etcetera. It means this is the property of encapsulating members and functions.
·         Encapsulation is a technique used to protect the information in an object from another object.
Abstraction
·         Abstraction is "To represent the essential feature without representing the background details."
·         Abstraction is a process of hiding the implementation details and displaying the essential features.
·         Abstraction is a mechanism to provide the essential features without describing the background details. Means provide the functions to access the hidden (private) data.
·         The importance of abstraction is derived from its ability to hide irrelevant details and from the use of names to reference objects. Abstraction is essential in the construction of programs. It places the emphasis on what an object is or does rather than how it is represented or how it works. Thus, it is the primary means of managing complexity in large programs.

The Differences between Abstraction and Encapsulation

Abstraction
Encapsulation
1. Abstraction solves the problem at the design level.
1. Encapsulation solves the problem at the implementation level.
2. Abstraction hides unwanted data and provides relevant data.
2. Encapsulation means hiding the code and data into a single unit to protect the data from the outside world.
3. Abstraction lets you focus on what the object does instead of how it does it
3. Encapsulation means hiding the internal details or mechanics of how an object does something.
4. Abstraction: Outer layout, used in terms of design.
For example:
An external of a Mobile Phone, like it has a display screen and keypad buttons to dial a number.
4. Encapsulation- Inner layout, used in terms of implementation.
For example the internal details of a Mobile Phone, how the keypad button and display screen are connected to each other using circuits.


Polymorphism
Polymorphism means one thing in many forms. Basically, polymorphism is a capability of one object to behave in multiple ways. Example: A man role changes at home, college, and outside the home. There are following types of polymorphism:
1.       Static polymorphism (compile time): It is achieved using function overloading and operator overloading.
2.       Dynamic polymorphism (runtime time)It is achieved using function overriding means using the virtual function.
Polymorphism provides following features: 
  • It allows you to invoke methods of the derived class through base class reference during runtime.
  • It has the ability for classes to provide different implementations of methods that are called by the same name.

Polymorphism is of two types: 
  • Compile time polymorphism/Overloading
  • Runtime polymorphism/Overriding

Compile Time Polymorphism 
Compile time polymorphism is a method and operators overloading. It is also called early binding. 
In method overloading method performs the different task at the different input parameters. 

Runtime Time Polymorphism 
Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding. 
When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same prototype.

Sealed Class

What is the use of sealed classes in c#? Generally, if we create classes we can inherit the properties of that created class in any class without having any restrictions. In some situation, we will get requirement like we don’t want to give permission for the users to derive the classes from it or don’t allow users to inherit the properties from the particular class in that situations what we can do? 
For that purpose, we have a keyword called “Sealed” in OOPS. When we defined class with keyword “Sealed” then we don’t have a chance to derive that particular class and we don’t have permission to inherit the properties from that particular class.

Method Hiding( new keyword)

Method hiding in C# is similar to the function overriding feature in C++. Functions of the base class are available to the derived class. If the derived class is not happy, one of the functions available to it from the base class can define its own version of the same function with the same function signature, just differing in implementation. This new definition hides the base class definition.

Virtual and Overridden Methods

Only if a method is declared virtual, derived classes can override this method if they are explicitly declared to override the virtual base class method with the override keyword.
    using System;
    namespace Polymorphism
    {
        class A
        {
            public virtual void Foo() { Console.WriteLine("A::Foo()"); }
        }

        class B : A
        {
            public override void Foo() { Console.WriteLine("B::Foo()"); }
        }

        class Test
        {
            static void Main(string[] args)
            {
                A a;
                B b;

                a = new A();
                b = new B();
                a.Foo();  // output --> "A::Foo()"
                b.Foo();  // output --> "B::Foo()"

                a = new B();
                a.Foo();  // output --> "B::Foo()"
            }
        }

Constructors and Destructors

Constructors

A constructor is a special function that is a member of the Class and has the same name as that of the Class. Every Object created would have a copy of member data which requires initialization before it can be used. This initialization is common in Object-Oriented Language, which allows the object to initialize themselves as and when they created. This automatic initialization is performing through the use of constructor functions. The constructor methods exist to simplify the process of initializing class member variables within a class.

Declaration of Constructors 

public class Car
{
        int start;
        public Car()
        {
               //Add constructor task here
               start = 0;
        }
}
A constructor is optional if no constructors are declared for a class, the compiler invokes a default constructor for you. The default constructor simply sets all the fields in the class to their default values. You can define as many constructors as you want, as long as each constructor has a different parameter list. Also not that constructor functions cannot return values.

Destructors

A destructor is a function that has the same name as that of the class but is prefixed with a ~(tilde). Destructors de-initialize Object when they are destroyed. You can think of destructors as the opposite of constructors: constructors execute when objects are created, and destructors execute when the objects are destroyed by the built-in Garbage Collection facility. This process occurs behind the scenes with no consequence to the programmer.

Declaration of Destructors 

public class Car
{
        int start;
        //constructor
        public Car()
        {
               start = 0;
        }
        //destructor
        ~Car()
        {
               start = 0;
        }
}
Destructors are optional. Destructors cannot return any values; nor can they accept any parameters. Unlike constructors, you cannot have more than one destructor defined for a class.