Introduction To Object Oriented Programming Concepts (OOPS) in C#.Net - ASP - Net, C#.NET, VB
Introduction To Object Oriented Programming Concepts (OOPS) in C#.Net - ASP - Net, C#.NET, VB
ASP.NET,C#.NET,VB.NET,JQuery,JavaScript,Gridview,SQL HOME ASP.NET AJAX GRIDVIEW JAVASCRIPT SQL JQUERY OOPS CONCEPTS INTERVIEW QUESTIONS Server,Ajax,SSRS, XML examples
aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code Introduction to Object Oriented examples of asp.net 2.0 /3.5,AJAX,SQL Server C#.net Articles,examples of .net technologies
By: Suresh Dasari Apr 16, 2010 Categories: OOPS Concepts
ADVERTISE
Search
OOPS Concepts
Class: It is a collection of objects. Object: It is a real time entity. An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs defines the object's behavior. For example, the hand can grip something or a Student (object) can give the name or address. In pure OOP terms an object is an instance of a class
About Me
S U R ES H DA S A R I T EN A L I , A N D HR A PR A D ES H, I N D I A
The above template describe about object Student Class is composed of three things name, attributes, and operations public class student { } student objstudent=new student (); According to the above sample we can say that Student object, named objstudent, has created out of the student class. In real world you will often find many individual objects all of the same kind. As an example, there may be thousands of other bicycles in existence, all of the same make and model. Each bicycle has built from the same blueprint. In object-oriented terms, we say that the bicycle is an instance of the class of objects known as bicycles. In the software world, though you may not have realized it, you have already used classes. For example, the Textbox control, you always used, is made out of the Textbox class, which defines its appearance and capabilities. Each time you drag a Textbox control, you are actually creating a new instance of the Textbox class.
V IE W M Y CO M P L E TE P RO F IL E
77
Select Language
Pow ered by
Translate
Encapsulation:
Encapsulation is a process of binding the data members and member functions into a single unit. Example for encapsulation is class. A class can contain data structures and methods. Consider the following class public class Aperture { public Aperture () {
www.aspdotnet-suresh.com/2010/04/introduction-to-object-oriented.html
1/29
12/28/13
Abstraction:
Abstraction is a process of hiding the implementation details and displaying the essential features.
?
Example1: A Laptop consists of many things such as processor, motherboard, RAM, keyboard, LCD screen, wireless antenna, web camera, usb ports, battery, speakers etc. To use it, you don't need to know how internally LCD screens, keyboard, web camera, battery, wireless antenna, speakers works. You just need to know how to operate the laptop by switching it on. Think about if you would have to call to the engineer who knows all internal details of the laptop before operating it. This would have highly expensive as well as not easy to use everywhere by everyone. So here the Laptop is an object that is designed to hide its complexity. How to abstract: - By using Access Specifiers
Recent Posts
jQuery Set Maxlength for Multiline Textbox in Asp.net More jQuery Set Maximum Length to Textarea or jQuery Check Textar... More Call WCF Service from jQuery Ajax JSON Example in Asp.net us... More jQuery Slide DIV Content from Left to Right or Move DIV from... More jQuery Ajax JSON Example in Asp.net More SQL Server - Query to Get List of Tables in Database in SQL ... More Get Table Size in SQL Server | Query to Get Table Size in SQ... More SQL Server - How to Get Database Size in SQL Server 2008 More what is authorization in asp.net | authorization rules in we... More C# - Difference between String and Stringbuilder in C#, Asp.... More
public class Class1 { int i; public int j; protected int k; internal int m; protected internal int n; assembly static int x; public static int y; [DllImport("MyDll.dll")] public static extern int MyFoo(); other assembly public void myFoo2() { //Within a class if you create an object of same class then you can access all data members through object reference even private data too Class1 obj = new Class1(); obj.i =10; obj.j =10; obj.k=10; obj.m=10; obj.n=10; //Error cant access private data through object.But here it is accessible.:) //extern means declared in this assembly defined in some // This is also private //Static means shared across objects //No Access specifier means private // Public //Protected data // Internal means visible inside assembly //inside assembly as well as to derived classes outside
Subscribe
www.aspdotnet-suresh.com/2010/04/introduction-to-object-oriented.html
2/29
12/28/13
obj.s =10; //Errror Static data can be accessed by class names only Class1.x = 10; obj.y = 10; //Errror Static data can be accessed by class names only Class1.y = 10;
Aspdotnet-Suresh
Follow
+ 1,557 Find us on Facebook
+1
obj.s =10; //Errror Static data can be accessed by class names only Class1.x = 10; //Error cant access private data outside class obj.y = 10; //Errror Static data can be accessed by class names only Class1.y = 10;
Aspdotnetsuresh
Like
Tags
Asp.net
General
JQuery
Snippets Javascript C#.Net SQL Server Gridview VB.NET JQuery Plugins Interview Questions Errors Ajax DropdownList validations
DatePicker Authentication Fileupload SharePoint Membership OOPS Concepts JSON CSS Google API IISServer CheckBox Crystal Reports Google MAPS AutoComplete SendMail ExcelSheet InternetTips
AjaxModalPopupExtender ExportGridview Data Menu Joins SlideShow WCF Window s Application XML Modalpopup WebService CheckBoxList Dynamic Controls Facebook Global.asax Tw itter Window s Service YouTube jQuery Menu jQuery UI Accordion Menu AjaxAsyncFileUpload EncryptionandDecryption
Degree View
Inheritance:
Inheritance is a process of deriving the new class from already existing class C# is a complete object oriented programming language. Inheritance is one of the primary concepts of object-oriented programming. It allows you to reuse existing code. Through effective use of inheritance, you can save lot of time in your programming and also reduce errors, which in turn will increase the quality of work and productivity. A simple example to understand inheritance in C#.
LightBoxEffect
Polymorphism
Using System; Public class BaseClass { Public BaseClass () { Console.WriteLine ("Base Class Constructor executed"); } Public void Write () { Console.WriteLine ("Write method in Base Class executed"); } } Public class ChildClass: BaseClass {
Session Timeout ThumbnailsGeneration UserName Visitors Count jQuery Cookie jQuery Media Plugins Image 3-TierArchitecture Advertise ADO.NET Ajax
Check
AbstractVsInterface AjaxAccordionControl
ActiveDirectory
Calendarextender
AjaxRatingControl AjaxSlideshowExtender Arraylist Assembly Custom Right Click Menu DataGrid Generic List HTML Address ImportContacts ListBox MCC Award Panorama Image Viewer Plugins Print RSSFeeds DIV Product Reviews QueryString text file RadioButtonList Read/Write
ReadOnlyValues Repeater Resize Image Reviews RichTextBox SQL Constraints SiteMap Social Media Bookmark Plugins Checker Testimonial Example Testing Trace Mobile Number Try Catch UpdatePanel VBScript Virtual Keyboard WPF Audio Plugins jQuery Mobile jQuery Video Plugins top/bottom of div setInterval setTimeOut
www.aspdotnet-suresh.com/2010/04/introduction-to-object-oriented.html
3/29
12/28/13
www.aspdotnet-suresh.com/2010/04/introduction-to-object-oriented.html
4/29
12/28/13
Common Interview Question: Are private class members inherited to the derived class?
Ans: Yes, the private members are also inherited in the derived class but we will not be able to access them. Trying to access a private base class member in the derived class will report a compile time error. Using System; Public class A { Public void M1 () { } Private void M2 () { } } Public class B: A { Public static void Main () { B B1 = new B (); B1.M1 (); //Error, Cannot access private member M2 //B1.M2 (); } } Method Hiding and Inheritance We will look at an example of how to hide a method in C#. The Parent class has a write () method which is available to the child class. In the child class I have created a new write () method. So, now if I create an instance of child class and call the write () method, the child class write () method will be called. The child class is hiding the base class write () method. This is called method hiding. If we want to call the parent class write () method, we would have to type cast the child object to Parent type and then call the write () method as shown in the code snippet below.
Using System; Public class Parent { Public void Write () { Console.WriteLine ("Parent Class write method"); } } Public class Child: Parent
www.aspdotnet-suresh.com/2010/04/introduction-to-object-oriented.html
5/29
12/28/13
Polymorphism:
When a message can be processed in different ways is called polymorphism. Polymorphism means many forms. Polymorphism is one of the fundamental concepts of OOP.
Polymorphism is of two types: 1. Compile time polymorphism/Overloading 2. Runtime polymorphism/Overriding Compile Time Polymorphism
Compile time polymorphism is method and operators overloading. It is also called early binding. In method overloading method performs the different task at the different input parameters.
www.aspdotnet-suresh.com/2010/04/introduction-to-object-oriented.html
6/29
12/28/13
www.aspdotnet-suresh.com/2010/04/introduction-to-object-oriented.html
7/29
12/28/13
Method Overriding:
Whereas Overriding means changing the functionality of a method without changing the signature. We can override a function in base class by creating a similar function in derived class. This is done by using virtual/override keywords. Base class method has to be marked with virtual keyword and we can override it in derived class using override keyword. Derived class method will completely overrides base class method i.e. when we refer base class object created by casting derived class object a method in derived class will be called. Example: // Base class public class BaseClass { public virtual void Method1() { Console.Write("Base Class Method"); } } // Derived class public class DerivedClass : BaseClass { public override void Method1() { Console.Write("Derived Class Method"); } } // Using base and derived class public class Sample { public void TestMethod() { // calling the overriden method DerivedClass objDC = new DerivedClass(); objDC.Method1(); // calling the baesd class method BaseClass objBC = (BaseClass)objDC; objDC.Method1(); } } Output ---------------------
www.aspdotnet-suresh.com/2010/04/introduction-to-object-oriented.html
8/29
12/28/13
Like
9
Tw eet
References and pointers cannot be used on constructors and destructors because their addresses
77
3
Share
Constructors and destructors obey the same access rules as member functions. For example, if you declare a constructor with protected access, only derived classes and friends can use it to
StumbleUpon create class objects. 2 The compiler automatically calls constructors when defining class objects and calls destructors
when class objects go out of scope. A constructor does not allocate memory for the class object its this pointer refers to, but may allocate storage for more objects than its class object refers to. If memory allocation is required for objects, constructors can explicitly call the new operator. During cleanup, a destructor may release objects allocated by the corresponding constructor. To release objects, use the delete operator. Example of Constructor class C { private int x; private int y; public C (int i, int j) { x = i; y = j; } public void display () { Console.WriteLine(x + "i+" + y); } } Example of Destructor class D { public D () { // constructor } ~D () { // Destructor } }
If you enjoyed this post, please support the blog below. It's FREE!
Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.
Like
9.6k
77
Subscribe by RSS
3,104 follow ers
Subscribe by Email
Follow @aspdotnetsuresh
www.aspdotnet-suresh.com/2010/04/introduction-to-object-oriented.html
9/29