How are classes related to objects in .NET Application?As per object
oriented programming, properties and the methods that access these properties
are binded together into an entity. This is known as encapsulation.
This entity which wraps properties and its corresponding methods is known as a Class. Here is an example for the class: class sampleClass { int dataMember1; public void setData(int data) { dataMember1 = data; } public int getData() { return dataMember1; } } In this example,
you have a class called sampleClass containing a property called dataMember1.
Two methods namely setData and getData are trying to access this property.
Hence both the methods are wrapped inside sampleClass. Till this it looks
fine. But how do you access a class and its members? For that you have
to create an instance of the class and that instance is termed as object
of the class. Given below is a sample code to show how to create object
for the class sampleClass: In this example, you create an object obj of sampleClass and access setData and getData methods using this object. This is how classes and objects are related. Object is the instance of the class with which you can access the class members.
|