You are on page 1of 13

UNIT IV

Defining a Class

 A class definition starts with the keyword class followed by the class
name; and the class body enclosed by a pair of curly braces.
 Access specifiers specify the access rules for the members as well as
the class itself. If not mentioned, then the default access specifier for a
class type is internal. Default access for the members is private.
 Data type specifies the type of variable, and return type specifies the
data type of the data the method returns, if any.
 To access the class members, you use the dot (.) operator.
 The dot operator links the name of an object with the name of a
member.
Constructors

 A class constructor is a special member function of a class that is


executed whenever we create new objects of that class.
 A constructor has exactly the same name as that of class and it does
not have any return type.
 A default constructor does not have any parameter but if you need, a
constructor can have parameters. Such constructors are
called parameterized constructors.
EXAMPLE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
classArea
{
public Area(int a)
{
Console.WriteLine("Area of Square="+a*a);
}
public Area(float r)
{
Console.WriteLine("Area of circle=" +3.14*r*r);
}
public Area(double s)
{
Console.WriteLine("Volume of a cube= " +s*s*s);
}
public Area(double l,double b)
{
Console.WriteLine("Area of Rectangle= " +l*b);
}
}
classProgram
{
publicstaticvoid Main(string[] args)
{
Area z1=newArea(6);
Area z2=newArea(10.0f);
Area z3=newArea(6.0);
Area z4=newArea(20.0,15.0);
Console.ReadKey();

}
}
}

OUTPUT
Area of a square=36
Area of a circle=314
Volume of a cube=216
Area of a Rectangle=300

Destructors

 A destructor is a special member function of a class that is executed


whenever an object of its class goes out of scope.
 A destructor has exactly the same name as that of the class with a
prefixed tilde (~) and it can neither return a value nor can it take any
parameters.

CONSTANT

1. const keyword can be applied to fields or local variables


2. We must assign const field at time of declaration
3. Const in C# are by default static.
4. Const field can not be passed as ref or out parameter

READONLY MEMBERS

1. readonly keyword applies only to fields not local variables


2. We can pass readonly field as ref or out parameters in the constructor
context.

PROPERTIES

Accessing private data using accessor method.

EXAMPLE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
classnumber
{
privateint num;
publicvoid setNumber(int x)
{
num = x;
}
classProgram
{
staticvoid Main(string[] args)
{
setNumber(100);
Console.WriteLine("num="n.getnumber);
{
n.number();
}
}
}
}
}

INDEXERS

It allows our class to be used just like an


Array.
We can index the object using[] just like an array.
The indexers are special type of properties which add functionality
to the class to be index.
We can use access specifier with indexers.
It is created by this keyword.

EXAMPLE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
publicclassperson
{
String[]={"aa","bb","cc","dd"};
publicobjectthis[int index]
{
get
{
returnperson[index];
}
set
{
person(index)=value;
}
}
}
classProgram
{
staticvoid Main(string[] args)
{
person p=newperson();
Console.writeline(p[2]);
}
}
}

Inheritance

 new class should inherit the members of an existing class. This


existing class is called the baseclass, and the new class is referred to
as the derived class.
EXAMPLE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
classa
{
protectedstring name;
publicvoid Name()
{
Console.WriteLine("Enter your Name:");
name = Console.ReadLine();
}
}
classb : a
{
protectedstring regno;
publicvoid RegNo()
{
Console.WriteLine("Enter Your RegNo:");
regno = Console.ReadLine();
}
}
classc : b
{
protectedstring major;
publicvoid Major()
{
Console.WriteLine("Enter Your Major:");
major = Console.ReadLine();
}
}
classd : c
{
protectedstring college;
publicvoid College()
{
Console.WriteLine("Enter Your College:");
college = Console.ReadLine();
}
publicvoid show()
{
Console.WriteLine("\n\n Data Entered \n\n");
Console.WriteLine(name);
Console.WriteLine(regno);
Console.WriteLine(major);
Console.WriteLine(college);
}
}

classProgram
{
publicstaticvoid Main(string[] args)
{
d z = newd();
z.Name();
z.RegNo();
z.Major();
z.College();
z.show();
Console.ReadKey();
}
}
}

Polymorphism

 The word polymorphism means having many forms


 Polymorphism can be static or dynamic
 In static polymorphism, the response to a function is determined at
the compile time
 In dynamic polymorphism, it is decided at run-time.
EXAMPLE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
classShape
{
protectedint width, height;
public Shape(int a, int b)
{
width = a;
height = b;
}
publicvirtualint area()
{
Console.WriteLine("Parent Class");
return 0;
}
}
classRectangle : Shape
{
public Rectangle(int a, int b)
: base(a, b)
{
}
publicoverrideint area()
{
Console.WriteLine("Rectangle class area:");
return (width * height);
}
}
classTriangle:Shape
{
public Triangle(int a,int b):base(a,b)
{
}
publicoverrideint area()
{
Console.WriteLine("Triangle class area:");
return (width*height/2);
}
}

classCaller
{
publicvoid CallArea(Shape s)
{
int a;
a=s.area();
Console.WriteLine("Area {0}",a);
}
}
classDyPoly
{
staticvoid Main(string[] args)
{
Caller c = newCaller();
Rectangle r = newRectangle(10, 7);
Triangle t = newTriangle(10, 5);
c.CallArea(r);
c.CallArea(t);
Console.ReadKey();
}
}
}

ABSTRACT CLASSES

 Implementation is completed when a derived class inherits from it.


 Abstract classes contain abstract methods, which are implemented by
the derived class.
rules about abstract classes:

 You cannot create an instance of an abstract class


 You cannot declare an abstract method outside an abstract class
 When a class is declared sealed, it cannot be inherited, abstract
classes cannot be declared sealed.
SEALED CLASSES

Sealed classes are used to restrict the users from inheriting the class. A class can
be sealed by using the sealed keyword. The keyword tells the compiler that the
class is sealed, and therefore, cannot be extended. No class can be derived from
a sealed class.

EXAMPLE

// C# code to define
// a Sealed Class
using System;

// Sealed class
sealedclassSealedClass
{

// Calling Function
publicint Add(int a, int b)
{
return a + b;
}
}

classProgram
{

// Main Method
staticvoid Main(string[] args)
{

// Creating an object of Sealed Class


SealedClass slc = newSealedClass();

// Performing Addition operation


int total = slc.Add(6, 4);
Console.WriteLine("Total = " + total.ToString());
}
}

Method Overloading

• Creating a multiple methods in a class with same name but different


parameters and types is called as method overloading.method overloading
is the example of Compile time polymorphism which is done at compile
time.

EXAMPLE

using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace ConsoleApplication20
{
class A1
{
public virtual void abc()
{
Console.WriteLine("IN A");
}
}
class B1 : A1
{
public override void abc()
{
Console.WriteLine("IN B");
}
}
class C1 : B1
{
public override void abc()
{
Console.WriteLine("IN C");
}
}
class D1 : C1
{
public override void abc()
{
Console.WriteLine("IN D");
}
}
class Ride
{
public static void Main()
{
A1 z1 = new A1();
B1 z2 = new B1();
C1 z3 = new C1();
D1 z4 = new D1();
z1.abc();
z2.abc();
z3.abc();
z4.abc();
Console.ReadLine();
}
}
}

Method overriding
• Creating the method in a derived class with same name, same parameters
and same return type as in base class is called as method overriding.

• Method overriding is the example of run time polymorphism,howits is the


part of run time polymorphism i will explain in detail.

Some Key Points of Method overriding

• Method overriding is only possible in derived class not within the same
class where the method is declared.

• Only those methods are overrides in the derived class which is declared in
the base class with the help of virtual keyword or abstract keyword.

EXAMPLE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
classA1
{
publicvirtualvoid abc()
{
Console.WriteLine("In A");
}
}
classB1 : A1
{
publicoverridevoid abc()
{
Console.WriteLine("In B");
}
}
classC1 : B1
{
publicoverridevoid abc()
{
Console.WriteLine("In C");
}
}
classRide
{
staticvoid Main(string[] args)
{
A1 z1 = newA1();
B1 z2 = newB1();
C1 z3 = newC1();
z1.abc();
z2.abc();
z3.abc();
Console.ReadKey();
}
}
}

You might also like