Advertisement

Classes and Objects

By Bintu Chaudhary   Posted at  5:37:00 AM  

Home  »  C#  »  Classes and Objects


     C# is a Purely OOP Language Means C# is used for Creating Code which is used by another Users and A user has a Facility to use any data from that Existing Code A Language called as OOP Language if it supports all the Features of the OOP Language For Understanding OOP's first you have to understand few concepts:-


Contents

Principles of OOP

     In object-oriented palance, a problem is viewed in terms of the following concepts:

Objects

     An Object is a Real Word Thing Which performs a Specific Task and which has a set of Properties and Methods. Properties of Object are Also Called as Attributes of an Object and Method is also Known as the Function of the Object Like What an Object can do For Example A Car is an Object which has a Certain Number of Properties Like Color, Model No, Average etc and a Function Known as Run When A User gives Some Race then the Car Will be Moved

Classes

     A Class is that which contains the set of properties and Methods of an object in a single unit Like Animals is name of class which contains all the Properties and Methods of an Object of another Animals So if we wants to access any data from the Class then first we have to create the Object of a class Then we can use any data and method from class.

Data abstraction

     Data Abstraction is that in which A User Can use any of the data and Method from the Class Without knowing about how this is created So in other words we can say that A user can use all the Functions without Knowing about its detail For Example When a User gives Race to Car The Car will be Moved but a User doesn't know how its Engine Will Work

Data encapsulation

     Data Encapsulation is also Known as Data Hiding as we know with the inheritance concept of OOP's a user can use any code that is previously created but if a user wants to use that code then it is must that previously code must be Public as the name suggests public means for other peoples but if a code is Private then it will be known as Encapsulate and user will not be able to use that code So With the help of OPPS we can alter or change the code means we can make the Code as Private or public This allows us to make our code either as public or private

Inheritance

     Inheritance is very popular Concept in OOP This provides the Capability to a user to use the Predefined Code or the code that is not created by the user himself but if he may wants to use that code then he can use that code This is Called Inheritance but Always Remember in Inheritance a user only using the code but he will not be able to change the code that is previously created he can only use that code

Polymorphism

     Poly Means many and morphism means many function The Concepts Introduces in the form of Many behaviors of an object Like an Operator + is used for Addition of Two Numbers and + is also used for Joining two names The Polymorphism in java Introduces in the Form of Functions Overloading and in the Form of Constructor Overloading. For example considering the operator plus (+).
16 + 4 = 20
"abc" + "xyz" = abcxyz




Dynamic binding

     Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding means that the code associated with a given procedure call is not know until the time of call at run-time. This is associated with polymorphism and inheritance.

Message Passing

     Message passing is another feature of object-oriented programming. An object-oriented program consists of a set of objects that communicate with each other. The concept of message passing makes it eaiser to talk about building systems that directly model or simulate their real-world conuter-parts.



Defining a Class

     For Making a Class which is a user defined data type we have to use Keyword Class and We can also Create Variables in the Class those are called as instance variables and we can adds also some Methods in the Class . For Accessing Any Element form Class either he may a variable or a Member Function then first we have to Kept in Mind the Access Modifier Means the Access Modifier will Specify where you can Access a Variable or either a Method For Using anything from a Class first we have to create an object of Class with the Help of New Operator and then with the Help of object and dot Operator we can Access any data from the Class Suppose if a data isn't initialized then the data of the Class is set to Zero by default.

A Class Contains
1) Data Members or Simply Called As Variables
2) Member Functions or Simply Called as Functions

//Simple Class




New
New is a Keyword Which is used when we are Creating an object of class For Storing all the data like variables and member functions of class there is some memory Space that is to be needed So that With the help of new Keyword and Object is Instantiated or Simply an object Reserves Some Memory or Some New Memory is Allocated to Class Object For Storing data and member functions of Class So Every Object Must be Created With the Help of New Keyword So For Allocating New Memory Area.

Abstract Classes

     The abstract modifier is used to indicate that a class is incomplete and intended only to be a base class of other classes. An abstract class differs from a non-abstract class is the following ways:

  1. An abstract class cannot be instantiated directly, and it is an error to use the new operator on an abstract class. While it is possible to have variables and values whose compile-time types are abstract, such variables and values will necessarily either be null or contain references to instances of non-abstract classes derived from the abstract types.
  2. An abstract class is derived from an abstract class, the non-abstract class must include actual implementations of all inherited abstract members. Such implementations are provied by overriding the abstract members.




Abstract Classes & Method




Nested Class

     Classes are usually specified independently of each other. But it is possible for one class to be specified within another's specification. In this case, the latter class is termed a nested class.




Sealed Methods

     When an instance method declaration includes a sealed modifier, the method is said to be a sealed method. A sealed method overrides an inherited virtual method with the same signature. Whereas a virtual method declaration introduces a new method, an override method declaration specializes an existing inherited virtual method by providing a new implementation of the method. The sealed modifier can only be used in combination with the override modifier.






About the Author

Nulla sagittis convallis arcu. Sed sed nunc. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.
View all posts by: BT9

Back to top ↑
Connect with Me