0% found this document useful (0 votes)
4 views8 pages

Module 2

This document covers the creation and management of classes and objects in C#, focusing on concepts such as classification, encapsulation, accessibility, constructors, and partial classes. It explains how to define a class, control method and field accessibility, and the importance of constructors, including default and overloaded constructors. Additionally, it introduces the concept of partial classes for organizing large class definitions into manageable pieces.

Uploaded by

bindiyap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Module 2

This document covers the creation and management of classes and objects in C#, focusing on concepts such as classification, encapsulation, accessibility, constructors, and partial classes. It explains how to define a class, control method and field accessibility, and the importance of constructors, including default and overloaded constructors. Additionally, it introduces the concept of partial classes for organizing large class definitions into manageable pieces.

Uploaded by

bindiyap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Module 2

CREATING AND MANAGING CLASSES AND OBJECTS


Understanding classification
• Class is the root word of the term classification. When we design a class, we systematically arrange
information and behaviour into a meaningful entity.

The purpose of encapsulation


Encapsulation is an important principle when defining classes. Encapsulation actually has two purposes:

■ To combine methods and data within a class; in other words, to support classification

■ To control the accessibility of the methods and data; in other words, to control the use of the class
• Defining and using a class

• In C#, we use the class keyword to define a new class. The data and methods of the class occur in the
body of the class between a pair of braces. Following is a C# class called Circle that contains one
method (to calculate the circle’s area) and one piece of data (the circle’s radius):
class Circle
{
int radius; double Area()
{
return Math.PI * radius * radius;
}
}
 We create a variable specifying Circle as its type, and then we initialize the variable
with some valid data. Here is an example:
Circle c; //
Create a Circle variable
c = new Circle(); // Initialize it
 We cannot write a statement such as this because it gives an error: Circle
c;
c = 42;
 We can directly assign an instance of a class to another variable of the same type, like
this: Circle c;
c = new Circle();
Circle d;
d = c;

Controlling accessibility

■ A method or field is private if it is accessible only from within the class. To declare that a
method or field is private, we write the keyword private before its declaration. As intimated
previously, this is actually the default, but it is good practice to state explicitly that fields
and methods are private to avoid any confusion.
■ A method or field is public if it is accessible from both within and outside of the class. To
declare that a method or field is public, we write the keyword public before its declaration.
class Circle
{
private int radius;
public double Area()
{
return Math.PI * radius * radius;
}

radius is declared as a private field and is not accessible from outside the class, radius is accessible from within
the Circle class. The Area method is inside the Circle class, so the body of Area has access to radius.
• Working with constructors
A constructor is a special method that runs automatically when we create an instance of a class. It has the same
name as the class, and it can take parameters, but it cannot return a value (not even void). Every class must have
a constructor. If we don’t write one, the compiler automatically generates a default constructor for us.
class Circle
{
private int radius;
public Circle() // default constructor
{
radius = 0; } public double Area()
{
return Math.PI * radius * radius;}
}
 We use dot notation to invoke the Area method on a Circle object: Circle
c;
c = new Circle();
double areaOfCircle = c.Area();
• Overloading constructors

Overloading constructor is a constructor where parameters are passed to it is called overloaded constructor.
A constructor is just a special kind of method and it—like all methods—can be overloaded. We can add
another constructor to the Circle class, with a parameter that specifies the radius to use, like this:
class Circle
{
private int radius;
public Circle() // default constructor
{
• radius = 0;
}
public Circle(int initialRadius) // overloaded constructor
{
• radius = initialRadius;
}
public double Area()
{
return Math.PI * radius * radius;
}
}
Partial classes
• A class can contain a number of methods, fields, and constructors, as well as other items
• We can split the source code for a class into separate files so that we can organize the definition of a
large class into smaller, easier to manage pieces.
• When we split a class across multiple files, we define the parts of the class by using the partial
keyword in each file.
For example:
partial class Circle
{
public Circle() // default constructor
{ this.radius = 0;
}

public Circle(int initialRadius) // overloaded constructor{


this.radius = initialRadius;
}
}

You might also like