object oriented program
Please i want brief description about "object oriented program" in C# .Net with examples , please help me with good understanding data
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Saikiran GstPosted May 21, 2014, 11:40 AM
Hi lokesh sri
i think below explanation and with examples are useful to you if you got any dought refer below link
http://www.msdotnet.co.in/2012/06/oops-concept-in-c.html#.U3y_1tJdVs8
Object Oriented Programming language(OOPS):-
This is one of a successfull approach i.e used in application development for developing applications which came into existence in 70's to overcome the drawbacks of the previous approach i.e procedural programming approach
If the language has to be called as object oriented it need to satisfy a set of principles that are priscribed under the standardeds of object oriented programming those are
1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphysm
Opps is a methodology to write the program where we specify the code in form of classes and objects.
CLASS:-Class is a user defined data type. it is like a template. In c# variable are termed as instances of classes. which are the actual objects.
EX:-
Class classname
{
[variable declaration;]
[Method declaration;]
}
Here Class is a keyword and class name is valid c# identifier. Everything inside the square brackets is optional.It is also valid class definition.
EX:-
Class Empty
{
}
NOTE:- There are some similarities and difference between class & struct. we will discuss later.
OBJECT:- Object is run time entity which has different attribute to identify it uniquely.
EX:-
Here This is an example of creating an object of type Rectangle.
Rectangle rect1=new rectangle();
Rectangle rect1=new rectangle();
Here variable 'rect1' & rect2 is object of the rectangle class.
The Method Rectangle() is the default constructor of the class. we can create any number of objects of Rectangle class.
Basic Principle of oops:- There are main three core principles of any object oriented languages.
· ENCAPSULATION:- Encapsulation provides the ability to hide the internal details of an object from its users.The concept of encapsulation is also known as data hiding or information hiding. In c# , Encapsulation is implemented using the access modifier keywords.
1. Public
2. Private
3. protected
4. Internal
5. Protected Internal
· POLYMORPHISM:- It is also concept of oops. It is ability to take more than one form. An operation may exhibit different behaviour in different situations.
Ex:- An addition operation involving two numeric values will produce sum and the same addition operation will produce a string.If we pass parameter numeric value then method return sum(numeric value) and if we pass parameter String then same method is return string value .this called Polymorphism.
· INHERITANCE:- One class can include the feature of another class by using the concept of inheritance.In c# a class can be inherit only from one class at a time.Whenever we create class that automatic inherit from System.Object class,till the time the class is not inherited from any other class.
Ex:-
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
namespace Inheritance
{
class Program
{
public static void Main()
{
employee obj2 = new employee();
obj2.display();
obj2.show();
Console.ReadLine();
}
}
public class cls
{
public void display()
{
Console.WriteLine("HELLO");
}
}
public class employee : cls
{
public void show()
{
Console.WriteLine("welcome");
}
}
}
NOTE:-Here we are calling cls class(base class) member by creating the object of employclass(child class).it is known as inheritance. Other elements of oops concepts:
1.Method Overloading
We can used more than one Method with same name,within same class but with different parameter,that is called method overloading. When the basic functionality of more than one method is same but they are used different parameter,then we can implement method overloading.
Method overloading will be possible if method name is same but there signature is different.
Signature will be different if:-
1. Number of parameter in method is different.
EX:-
show(int x), show(int x,int y),
here both are same method but number of parameter is different.
2. Data type of parameter is different.
EX:-
display(int x, int y), display(int x, float y)
Here both method is same but there parameter is different.
3. Sequence of data type of parameter is different.
EX:-
Display(int x, string y, float z), Display(int x, float y ,string z).
Here both method is same but sequence of data type parameter is change.
EXAMPLE:-
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
namespace methodoverloading
{
class Program
{
static void Main(string[] args)
{
cls obj = new cls();
obj.display(); //call first display method which takes no
obj.display("neha"); //call second display method which takes one
Console.ReadLine();
}
public class cls
{
public void display()
{
Console.WriteLine("HELLO");
}
public void display(string sname)
{
Console.WriteLine("HELLO" + sname);
}
}
}
}
2.Method Overriding
When one class inherit from another class ,all the member of parent class become the member of child class .If there is a method of parent class that we want to predefined in child class. we can implement the concept of method overriding ,it means a method overriding of parents class and child class can have same method name with same parameter but parent class method will have "VIRTUAL"Keyword and child class method have "OVERRIDE"Keyword.
EXAMPLE:-
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
namespace methodoverriding
{
class Program
{
static void Main(string[] args)
{
employee obj = new employee();
obj.display();
Console.ReadLine();
}
}
public class cls
{
public virtual void display()
{
Console.WriteLine("hello");
}
}
public class employee : cls
{
public override void display()
{
Console.WriteLine("welcome");
}
}
}
3.Method hiding
We can predefine the method of parent class in child class by implementing the concept of method hiding. In method hiding the base(parent) class method is predefined in child class by specify new keyword.
Example:-
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
namespace method_hiding
{
class Program
{
static void Main(string[] args)
{
//employee obj = new employee();
cls obj1 = new employee(); //up casting
obj1.display();
//obj.display();
Console.ReadLine();
}
}
public class cls
{
public void display()
{
Console.WriteLine("hello");
}
}
public class employee : cls
{
public new void display()
{
Console.WriteLine("welcome");
}
}
4.Access-specifier or Modifier
Visibility Control:- When we want to implement inheritance, it is important to understand ,how to establish visibility levels for our classes and their members. There are four types of a accessibility modifiers which may be applied to classes and members to specify their level of visibility.
1. Public
2. private
3. protected
4. internal
Class Visibility:-
Each class needs to specify its level of visibility. class visibility is used to decide which parts of the system can create class objects.In c# class can have one of the two visibility modifierpublic or internal. If we do not explicitly mark the visibility modifier of a class, it is implicit set to 'internal' that is by default all classes are internal.
Class marked public are accessible everywhere ,Both within and outside the program assembly.
Class Member Visibility:- One of the goal of object oriented programming is data hiding.That is a class may be designed to hide its members from outside accessibility. C# provides a set of 'access modifiers' It specify the scope of type and its member up to which level they can be access.
There are five access specifier in C#.
1. Private:- Private member can be access only within the block { },where they have been declared. By default the class member are private.
2. Protected:- protected member can be access within containing classes and Derived classes.
OR
Protected member is visible only to its own class and its derived classes.
3. Internal:- Internal member can be access within Containing classes and Containing program or Application or Assembly.
4. Protected Internal:- Protected Internal member is access within containing classes, Derived classes.and containing program.
OR
It is available in the containing program or assembly and in the Derived classes.
5. Public :-Public member is access within containing classes,Derived classes, containing program, anywhere outside the containing program.
OR
Public member is accessible from anywhere outside the the class as well. It is also accessible in Derived classes.
When no modifier is specified, it default to 'private' accessibility. It is important to remember that the accessibility of a member is never larger than that of the class containing it.
You can easily understand all modifiers with the help of Diagram which is given below.
See it:- Visibility of class member:
Example:- When access inherits from a base class, all members of the base class, except Constructor and Destructors are inherited and become member of derived class.
Program code:-
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
namespace accessspecifier
{
class Program
{
static void Main(string[] args)
{
B B1=new B();
B1.show();
}
}
class A
{
private int x;
protected int y;
internal int z;
public int p;
protected internal int q;
}
class B:A
{
public void show()
{
//x=10;
y=20;
z=30;
p=40;
q=50;
// Console.WriteLine(+x); // Error x is not accessible
Console.WriteLine(+y);
Console.WriteLine(+z);
Console.WriteLine(+p);
Console.WriteLine(+q);
Console.ReadLine();
}
}
}
lokesh sriPosted May 22, 2014, 4:30 AM