Forums » .NET » .NET »

Doubts in oops concepts


Posted Date: 18 Sep 2011      Posted By:: Rama Murthy     Member Level: Silver  Member Rank: 0     Points: 5   Responses: 3

Pls tell me the scenario's of below questions.

1) in which situation encapsulation is used in ur project?

2)in which situation abstraction is used in ur project?

3)in which situation method overloading is used in ur project?

4)in which situation method overriding is used in ur project?

5)in which situation constructor is used in ur project?

6)in which situation interface is used in ur project?

7)in which situation inheritence is used in ur project?

8)in which situation abstract class is used in ur project?



Responses

#632957    Author: Pawan Awasthi        Member Level: Gold      Member Rank: 4     Date: 18/Sep/2011   Rating: 2 out of 52 out of 5     Points: 4

Hai Rama Murthy,

See below for the answers of your questions related to OOPs:


1) In which situation encapsulation is used in your project?

Accessing the public properties by using private variables is one of the good examples of Encapsulation in all the projects.
e.g.


private string _name;
public String Name
{
get{return _name}
set{_name= value;}
}

Here you are not able to access the private variable _name but its value can be accessible by the public property Name. So a kind of Encapsulation here.


2) In which situation abstraction is used in your project?

When there are the specific requirements and that can be flown to their child, then we can use abstraction. it means whatever is essential, we implement to its child classes, else will be inherited automatically.
e.g.


class A
{
public abstract void Add(int a, int b);
protected int x;
public string Name{get; set;};
}
Class B:A
{
public override void Add(int a, int b)
{
Console.WriteLine("Sum of two numbers is:" + int. Parse(a + b).toString());
}
}

Here the essential method Add is implemented to the child class B. rest all the members will be inherited accordance to their protection levels.


3) In which situation method overloading is used in your project?

When we have more functionality to implement, we go with the overloading of method.
e.g. we have to get the sum of 2 numbers, 3 number and 4 numbers. So in that case we can take only one method name Add with its varied parameters like:


public void Add(int a, int b)
{
Console.WriteLine("Sum of 2 numbers" + int. Parse(a + b).toString());
}

public void Add(int a, int b, int c)
{
Console.WriteLine("Sum of 3 numbers" + int. Parse(a + b +c).toString());
}

public void Add(int a, int b, int c, int d)
{
Console.WriteLine("Sum of 4 numbers" + int. Parse(a + b + c + d).toString());
}
Now according to our requirement, we can call the respective method with its arguments.



4) In which situation method overriding is used in your project?

When we may or may not want to implement the particular method at runtime to its child class according to the requirement, we go with overriding concept.
e.g.


Class X
{
public virtual void Print()
{
Console.WriteLine ("This is the print method. You can override according to your definitions and implementation");
}
}
Class Y:X
{
public Override void Print()
{
Console.WriteLine("Implementation for Excel print methods");
}
}



5) In which situation constructor is used in your project?

Constructor is by default initialize with the class.
But if you want to initialize some members when the class in getting instantiate, you can write the constructor and code for initialization inside that constructor.
e.g.


Class MyClass
{
public MyClass()
{
int a=10; //initialized with the class instantiation
}
}

Here the constructor code will get initialized with the class instantiation.


6) In which situation interface is used in your project?

Interface is the collection of abstract members.
So whenever we have the requirements such as it requires through the application at many places, then we create an interface with that method so that we can implement it at our own way according to the requirements.
e.g.


Interface Inf
{
void Print();
}

Class ExcelClass:Inf
{
public override void Print()
{
//Write code to print in excel
}
}

Class WordPrintClass: Inf
{
public override void Print()
{
//Write code to print in Word
}

}
Class PDFPrintClass: Inf
{
public override void Print()
{
//Write code to print in PDF
}
}


Also to implement the multiple inheritances, we use inheritance.


7) In which situation inheritance is used in your project?

When we want to have some common features for multiple classes then we go for inheritance.
To inherit the properties of base class to their derived class.


8) In which situation abstract class is used in your project?

Abstract class is same as abstract. So go to the Q 2 for the details.

Hope you will understand now these concepts.
Let me know if still you have any confusion regarding these OOPs concepts.

Regards,
Pawan Awasthi(DNS MVM)
+91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
pawansoftit@gmail.com


 
#632958    Author: Solai murugan      Member Level: Bronze      Member Rank: 0     Date: 18/Sep/2011   Rating: 2 out of 52 out of 5     Points: 4

1) in which situation encapsulation is used in ur project?
Whenever we use the class for separate module, the encapsulation will be used.
2)in which situation abstraction is used in ur project?
similar to the encapsulation, data abstraction is the inbuild concept. No need to bothering about the real time usage.
3)in which situation method overloading is used in ur project?
In such a cases, where the same method need to perform different actions based on the parameters of that function, overload can be used.
4)in which situation method overriding is used in ur project?
In such a cases, where there is a need to redesign the method's goal, override can be used.
5)in which situation constructor is used in ur project?
whenever we are creating the classes, we should always create a constructor for dynamic creation of instances in better programming.
6)in which situation interface is used in ur project?
The interface should be used for referencing the defined methods and variables for the third party classes usage.
7)in which situation inheritence is used in ur project?
For reuse purpose.
8)in which situation abstract class is used in ur project?
for defining the base methods and variables of our project.

Thanks & Regards,
Solai murugan...


 
#632982    Author: Harsha      Member Level: Bronze      Member Rank: 0     Date: 18/Sep/2011   Rating: 2 out of 52 out of 5     Points: 4

Hi..

1)Encapsulation is the basic method for producing information hiding in
object-oriented methodology.
To make sure data is used in an appropriate manner by preventing programmers from accessing data in a non-intended manner.

2)Data abstraction avoid unimportant details of an object, so that only the useful characteristics that define it remain.

3)Method overloading enables the C# programmer to use the same method name multiple times with only the passed arguments being different.

e.g)
class methodDemo
{
void mydemo()
{
System.out.println("No parameters");
}

void mydemo(int a)
{
System.out.println("a: " + a);
}

void mydemo(int a, int b)
{
System.out.println("a and b: " + a + " " + b);
}

double mydemo(double a)
{
System.out.println("double a: " + a);
return a*a;
}
}
//call methods as required with different parameter.

4)Methods have same signature but different bevior
or functionality.
e.g.

class mybase
{
public void demo()
{
System.Console.WriteLine("This is Base class");
}
}

class mtderive :mybase
{
new public void demo()
{
System.Console.WriteLine("This is derived class");
}
}

class diaplsy
{
public static void Main()
{
mybase b;
b = new mybase();
b.demo();
}
}

5)a constructor is a method in the class which gets executed when its object is created,constructor is also used to initialise the member varialbles.

class ABC
{
public ABC()
{
int a=10;
}
}

6)An interface looks like a class, but it has no implementation. It contains definitions of events, indexers, methods and properties.
Interface mainly used for multiple inheritance.

7)Inheritance is used to combine the features of one or more classes.

8) When we want that all subclass must implement all methods of
base class ;In such situation use interface.
But if we want only some method of base class in our subclass
then use base class as abstract class because abstract class
can contain non-abstract method.





 
Post Reply     

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.





  • Return to Discussion Forum
  • Start new thread


  • Subscribe to Subscribers
    Top Contributors
    Today
      Last 7 Daysmore...

      Awards & Gifts

      Online Members

      More...
       
      Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India