OOAD – Object Oriented Analysis and Design. This is the software engineering approach that models the systems which interact with objects. OOA is the focuses on understanding and analyse the problem in the object oriented manner.
When we say modeling the system means definitely we need some tool to model the systems. This tool is called UML (Unified modeling language). I will cover more about the UML in the sub sequent posts.
So OOAD convert real world problems to software solutions using Object Oriented Concepts. In this approach, everything is consists of objects.
I am not going to write much theory about OOAD here. But I do write which we need it for our day to day software development process.
The OO concepts are
1. Abstraction: Simplifying the complexity into the simplified manner. In other words, representing the important details without including all the details.
For example, if you are working on banking domain, different types of accounts such as savings, mortgage can be treated as account only.
2. Encapsulation: It is a process of hiding all the internal details of an object from the outside world. For example, class is encapsulation. i.e.) the wrapping up of data and functions into a single unit is called as encapsulation.
Public class Prepaid
{
// Constructor
Public Prepiad(){}
Protected decimal ratePerMinute;
Protected decimal connectionCharge;
Protected int totalMinutes;
Public decimal GetChargeForPerCall(
{
return connectionCharge + (ratePerMinute * totalMinutes);
}
}
3. Inheritance: Inheritance is the process of creating new class from already existing class. The new class called derived or child class and existing class is called base or parent or super class. The main purpose of inheritance is use existing code or behaviour of the parent class in the derived class.
For example,
Public class Account
{
//Constructor
Public Account () {}
Public decimal getSavingsAmount()
{
// code here
}
Public decimal getTodayWithDrawAmount()
{
}
}
Public class SavingsAccount:Account
{
// code here
}
Public class MortgageAccount: Account
{
// code here
}
Here Account is the base class and SavingsAccount and MortgageAccount are the derived classes.
4. Polymorphism: Polymorphism means many forms. There are two types of polymorphism.
1. Compile time polymorphism: Example, method overloading
// Start
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HowToBecomeAnArchitect.OOAD.Polymorphism.OverLoading
{
class Customer
{
//Constructor
Customer()
{
Console.WriteLine("Customer class constructor is executed");
}
private string GetCustomerFirstName(int customerId)
{
//write code here to get customer first name
Console.WriteLine("Method GetCustomerFirstName is executed and method parameter is customerId");
//For testing I hardcoded the the name
return "Jayachandran";
}
private string GetCustomerFirstName(string emailAddress)
{
//write code here to get customer first name
Console.WriteLine("Method GetCustomerFirstName is executed & method parameter is emailAddress");
//For testing I hardcoded the email address
return "Jayachandran";
}
static void Main(string[] args)
{
Console.WriteLine("Main method is started");
Customer customerObj = new Customer();
Console.WriteLine(customerObj.GetCustomerFirstName(1234567890));
Console.WriteLine(customerObj.GetCustomerFirstName("Jayachandran.NET@hotmail.com"));
Console.ReadKey();
}
}
}
// End
The above program contains getCustomerFirstName method and this method is overloaded with different types of parameters. We can also overload method by using different number of parameters, different order of parameters.
Finally,
What are the different ways a method can be overloaded?
By using different parameter data types, different number of parameters, different order of parameters.
2. Run time polymorphism: Example, method overriding
In C#, we can use either new or virtual keyword to override the base class methods.
What is the different between new & virtual?
Please try to execute the below programs on Visual Studio 2010 & see the output before answering this question.
//Program 1
// Start
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HowToBecomeAnArchitect.OOAD.Polymorphism.Overriding
{
public class BaseAccount
{
//Constructor
public BaseAccount()
{
Console.WriteLine("BaseAccount class constructor is executed");
}
public void DisplayBalance()
{
Console.WriteLine("BaseAccount::DisplayBalance() is executed");
}
public void DisplayBPay()
{
Console.WriteLine("BaseAccount::DisplayBPay() is executed");
}
}
public class DerivedSavingsAccount : BaseAccount
{
//constructor
public DerivedSavingsAccount()
{
Console.WriteLine("DerivedSavingsAccount class constructor is executed");
}
public void DisplayBalance()
{
Console.WriteLine("DerivedSavingsAccount::DisplayBalance() is executed");
}
public new void DisplayBPay()
{
Console.WriteLine("DerivedSavingsAccount::DisplayBPay() is executed");
}
}
class Demo
{
static void Main(string[] args)
{
Console.WriteLine("Main method is started");
BaseAccount baseAccount;
baseAccount = new DerivedSavingsAccount();
baseAccount.DisplayBalance();
baseAccount.DisplayBPay();
Console.ReadKey();
}
}
}
// End
Output:
Main method is started
BaseAccount class constructor is executed
DerivedSavingsAccount class constructor is executed
BaseAccount::DisplayBalance() is executed
BaseAccount::DisplayBPay() is executed
The above program complies and run successfully with one waring.
Warning details is 'HowToBecomeAnArchitect.OOAD.Polymorphism.Overriding.DerivedSavingsAccount.DisplayBalance()' hides inherited member 'HowToBecomeAnArchitect.OOAD.Polymorphism.Overriding.BaseAccount.DisplayBalance()'. Use the new keyword if hiding was intended and warning will not show if press F6 again until you modify the program and compile again.
//Program 2
// Start
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HowToBecomeAnArchitect.OOAD.Polymorphism.Overriding
{
public class BaseAccount
{
//Constructor
public BaseAccount()
{
Console.WriteLine("BaseAccount class constructor is executed");
}
public virtual void DisplayBalance()
{
Console.WriteLine("BaseAccount::DisplayBalance() is executed");
}
public virtual void DisplayBPay()
{
Console.WriteLine("BaseAccount::DisplayBPay() is executed");
}
}
public class DerivedSavingsAccount : BaseAccount
{
//constructor
public DerivedSavingsAccount()
{
Console.WriteLine("DerivedSavingsAccount class constructor is executed");
}
public override void DisplayBalance()
{
Console.WriteLine("DerivedSavingsAccount::DisplayBalance() is executed");
}
public new void DisplayBPay()
{
Console.WriteLine("DerivedSavingsAccount::DisplayBPay() is executed");
}
}
class Demo
{
static void Main(string[] args)
{
Console.WriteLine("Main method is started");
BaseAccount baseAccount;
baseAccount = new DerivedSavingsAccount();
baseAccount.DisplayBalance();
baseAccount.DisplayBPay();
Console.ReadKey();
}
}
}
// End
Output:
Main method is started
BaseAccount class constructor is executed
DerivedSavingsAccount class constructor is executed
DerivedSavingsAccount::DisplayBalance() is executed
BaseAccount::DisplayBPay() is executed
Finally, the answer is
New: If you use the new keyword instead of override, the method in the derived class doesn't override the method in the base class, it merely hides it.
Override: When a method of a base class is overridden in a derived class, the version in the derived class is used, even if the calling code didn't "know" that the object was an instance of the derived class.
Why we use OOPS’ for our development?
The major advantages of OOPs are:
1. Simplicity:
Software objects model real world objects, so the complexity is reduced and the program structure is very clear.
2. Modularity:
Each object forms a separate entity whose internal workings are decoupled from other parts of the system.
3. Modifiability:
It is easy to make minor changes in the data representation or the procedures in an OO program. Changes inside a class do not affect any other part of a program, since the only public interface that the external world has to a class is through the use of methods.
4. Extensibility:
Adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones.
5. Maintainability:
Objects can be maintained separately, making locating and fixing problems easier.
6. Re-usability:
Objects can be reused in different programs.
If you would like to know more about OOPS please refer the following URL’s
Please click the below link to know more about other OOAD concepts
2.1.1 OOAD Concepts
No comments:
Post a Comment