Wednesday, April 12, 2017

OOPS quick reference


OOPS

  • Problem solving technique to develop software systems
  • Technique to think real world in terms of objects
  • Object maps the software to real world object
  • These objects have responsibility or provide services to application or other objects


Class
  • Describes all attributes of the objects, as well as the methods that implement the behavior of member objects
  • Comprehensive data type, that represents a blue print of object
Object
  • Instance of a class
  • It is a basic unit of a system. 
  • Is an entity that has attributes, behavior and identity 
Relation between Classes and Objects
  • Class is definition and objects are instance of the class
   Ex Car has breaks, accelerator. we can build a prototype of an car which would act like a class and 
     then we can built Tesla, Honda etc

Abstraction
  • Allows complex real world to be represented in simplified manner
  • Example color is abstracted to RGB. By just making the combination of these three colors we can achieve any color in world
Abstraction in Programming world
  • Means- hiding implementation using abstract class and interfaces etc
  • Means a concept or an idea not associated with any specific instance.
  • The abstraction is done when we need to only inherit from a certain class, but not need to instantiate objects of that class. In such case the baseclass can be regarded as "Incomplete". Such classes are known as an "Abstract Base Class".
   
  1. An Abstract Base class can not be instantiated; it means the object of that class can not be created.
  2. Class having abstract keyword and having, abstract keyword with some of its methods (not all) is known as an Abstract Base Class.
  3. Class having Abstract keyword and having abstract keyword with all of its methods is known as pure Abstract Base Class.
  4. The method of abstract class that has no implementation is known as "operation". It can be defined as abstract void method ();

Encapsulation 

  • It is a process of hiding all the internal details of an object from the outside world
  • means-hiding data like using getter and setter

Difference between abstract method and virtual method

Virtual methods have an implementation and provide the derived classes with the option of overriding it. Abstract methods do not provide an implementation and forces the derived classes to override the method.
So, abstract methods have no actual code in them, and subclasses HAVE TO override the method. Virtual methods can have code, which is usually a default implementation of something, and any subclasses CAN override the method using the override modifier and provide a custom implementation.
public abstract class E
{
    public abstract void AbstractMethod(int i);

    public virtual void VirtualMethod(int i)
    {
        // Default implementation which can be overridden by subclasses.
    }
}

public class D : E
{
    public override void AbstractMethod(int i)
    {
        // You HAVE to override this method
    }
    public override void VirtualMethod(int i)
    {
        // You are allowed to override this method.
    }
}

Communication using messages abstract

Objects communicate with each other using messages

Object lifetime
All objects have lifetime. Objects are created, and initialized, necessary functionalities are done and later the object is destroyed. Every object have there own state and identity, which differ from instance to instance.


Relation and hierarchy between objects

Association 
This is the simplest relationship between objects. Example every customer has sales. So Customer object and sales object have an association relation between them.

Aggregation implies a relationship where the child can exist independently of the parent. Example: Class (parent) and Student (child). Delete the Class and the Students still exist.
Composition implies a relationship where the child cannot exist independent of the parent. Example: House (parent) and Room (child). Rooms don't exist separate to a House.
Dependency is a weaker form of relationship and in code terms indicates that a class uses another by parameter or return type.
Dependency is a form of association.


Inheritance 
Hierarchy is used to define more specialized classes based on a preexisting generalized class.

Car can inherit from vehicle

Polymorphism 

The word polymorphism means having many forms. In object-oriented programming paradigm, polymorphism is often expressed as 'one interface, multiple functions'.

Polymorphism can be static or dynamic. In static polymorphism, the response to a function is determined at the compile time. In dynamic polymorphism, it is decided at run-time.

C# provides two techniques to implement static polymorphism. They are:

  • Function overloading
  • Operator overloading

Dynamic polymorphism is implemented by abstract classes and virtual functions.



using System;

public class DrawingObject
{
    public virtual void Draw()
    {
        Console.WriteLine("I'm just a generic drawing object.");
    }


}
using System;

public class Line : DrawingObject
{
    public override void Draw()
    {
        Console.WriteLine("I'm a Line.");
    }
}

public class Circle : DrawingObject
{
    public override void Draw()
    {
        Console.WriteLine("I'm a Circle.");
    }
}

public class Square : DrawingObject
{
    public override void Draw()
    {
        Console.WriteLine("I'm a Square.");
    }
}

Interface

An interface is defined as a syntactical contract that all the classes inheriting the interface should follow. 

Why interface?
  • Interfaces in C# are a means to get around the lack of multiple inheritance in C#, meaning you cannot inherit from multiple classes but you can implement multiple interfaces
  • Logical way of grouping objects in terms of behavior
  • Using interface based design concept provides loose coupling, component-based programming, easier maintainability, makes your code base more scalable and makes code reuse much more accessible because implementation is separated from the interface.
All vehicles have similar items, but are different enough that we could design an interface that holds all the common items of a vehicle.

using System;

interface IPerl
{
    void Read();
}

class Test : IPerl
{
    public void Read()
    {
        Console.WriteLine("Read");
    }
}

class Program
{
    static void Main()
    {
        IPerl perl = new Test(); // Create instance.
        perl.Read(); // Call method on interface.
    }
}

No comments:

Post a Comment