Rajib Rezwan

Hi ! I'm Rezwanul Islam Rajib . I'm a passionate programmer and software devloper . Everything about technology , the beauty in each and every one of them , makes me passionate about them . I wish I could see may be a little bit of all of them .

OOP , Part-2

 Encapsulation:

Encapsulation is the process of keeping or enclosing one or more items within a single physical or logical package. In object oriented programming methodology it prevents access to implementation details.

Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member. Available access specifiers are public, private, protected, internal etc.

How we can achieve Encapsulation?
We can achieve Encapsulation by using private access modifier as shown in below example method.

private string GetEngineMakeFormula()
{
    private string formula = "a*b";
    return formula;
}

Example – [Encapsulation]

public class Bike
{
    public int mileage = 65;
    public string color = "Black";
    private string formula = "a*b";

    //Its public – so accessible outside class
    public int GetMileage()
    {
        return mileage;
    }

    //Its public – so accessible outside class
    public string GetColor()
    {
        return color;
    }

    //Its private – so not accessible outside class
    private string GetEngineMakeFormula()
    {
        return formula;
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        Bike objBike = new Bike();
        Console.WriteLine("Bike mileage is : " + objBike.GetMileage()); //accessible outside "Bike"
        Console.WriteLine("Bike color is : " + objBike.GetColor()); //accessible outside "Bike"
        //we can't call this method as it is inaccessible outside "Bike"
        //objBike.GetEngineMakeFormula(); //commented because we can't access it
        Console.Read();
    }
}

So as you can see from above code that we hide GetEngineMakeFormula() method by using private access modifier because there is no need to give the make formula to users. So exposed only necessary methods for users to use it using public access modifier.


Inheritance : 

Makes programs simpler and faster. With inheritance, we build several types upon a common abstraction. Then we later act upon all those types through the abstraction. We examine inheritance and polymorphism in the C# language.

Base class:The class another class inherits from. In the C# language, the ultimate base class is the object class.

Object

Derived class:The class with a base class. A derived class may have multiple levels of base classes.


Example. When you specify that a class derives from another class, the class you create acquires all the features of the base class. If the derived class has a few extra features but is otherwise the same as the base class, this is a good approach.

Also:By using a base class, you can treat derived classes as the same base type and call virtual functions to access their features.

Example – [Inheritance]

public class Base
{
    public Base()
    {
        Console.WriteLine("Constructor of Base Class");
    }

    public void DisplayMessage()
    {
        Console.WriteLine("Hello, how are you?");
    }
}

public class Child : Base
{
    public Child()
    {
        Console.WriteLine("Constructor of Child class");
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        Child objChild = new Child();
        //Child class don't have DisplayMessage() method but we inherited from "Base" class
        objChild.DisplayMessage();
        Console.Read();
    }
}

As you can see in the previous example code, We created an object of a Child class in Main() method and then called DisplayMessage() method of Base class. If you notice that the Child class doesn’t have DisplayMessage()method in it. So obviously it is inherited from the Base class. When you execute following code, result would be as show below:
Example Result

Constructor of Base Class
Constructor of Child class
Hello, how are you?

As per sample result, we can say that the “Base” class constructor will automatically be called before the “Child” class constructor.

Thus, here conclusion is that “Base/Parent” classes are automatically instantiated before “Child/Derived” classes.

Loading