Module 9:
Defining Classes &
Members
By
SRIRAM . B
Overview
Access Specifiers
Access Modifiers
Properties
Interface
Abstract Class
Sealed Class
Access Specifier
Access specifiers are used to limit the accessibility of
the data members and functions in a class.
The accessibility of class members can be specified
using the keywords:
Public
Protected
Private
Internal
Protected internal
Access Specifier
The public keyword is used to provide unlimited access.
The protected keyword is used to provide access to the same class
and its derived classes.
The private keyword is used to limit the access within the class.
The internal keyword is used to limit the access to the program file.
The protected internal keyword is used to provide access within the
program file that contains the class definition and to the derived
classes.
Access Modifiers
Modifiers determine how the data members and
methods are used in other classes and objects.
A class declaration can include a sequence of access
modifiers.
Access modifiers can also be applied to methods.
The different access modifiers that can be used in a C-
Sharp program are:
static
sealed
abstract
virtual
Access Modifier - Static
The features of a static modifier are:
A static modifier allows a variable or method to be
associated with its class and not a class instance.
A class cannot be declared static.
Static members are shared by all the instances of a
class.
A static member can be accessed using the class name.
Example 1 - Access Modifier - Static
using System;
public class Static_modifier
{
public static void sum(int a, int b)
//static keyword
{
int result=a+b;
Console.WriteLine("Sum of the numbers is
{0}",result);
}
Example 1 - Access Modifier - Static
public static void Main()
{
Static_modifier.sum(10,20);
//Static method used without declaring
the instance of the class
}
}
Access Modifier - Sealed
The sealed modifier
Does not allow a class to be inherited.
Is used to create classes in which the methods cannot
be overridden.
Is used to create standard classes that cannot be
derived.
Does not allow a method to be overridden.
Example 1 - Access Modifier - Sealed
using System;
sealed class Base
//class declared as sealed
{
public void disp()
{
Console.WriteLine("Base");
}
}
Example 1 - Access Modifier -
Sealed..
class Derived : Base
{
public override void disp()
{
Console.WriteLine("Derived");
}
public static void Main()
{
Derived d=new Derived();
d.disp();
}
}
Access Modifier - Abstract
The abstract modifier:
Is used to define an incomplete class that is referred to
as the abstract class.
An abstract class can act as the base class for other
classes.
An abstract class cannot be instantiated directly.
An abstract class cannot be sealed.
An abstract method is one in which the method
definition is not given.
Example 1 - Access Modifier -
Abstract
using System;
abstract class Base
//abstract class declaration
{
public abstract void disp();
// abstract method declaration
}
Example 1 - Access Modifier -
Abstract..
class Derived : Base
{
public override void disp()
//overriding the abstract method
{
Console.WriteLine("Derived");
}
public static void Main()
{
Derived d=new Derived();
d.disp();
}
}
Access Modifier - Virtual
The feature of the virtual modifier are:
A virtual modifier is used to declare a virtual method.
A virtual method cannot include static, abstract or
override modifier.
Example - Access Modifier - Virtual
using System;
class one
{
public virtual void func()
{
Console.WriteLine("in one func");
}
public virtual void func1()
{
Example - Access Modifier - Virtual
Console.WriteLine("in one func1");
} }
class two: one
{
public override void func()
{
Console.WriteLine("in two func");
}
public override void func1()
Example - Access Modifier - Virtual
{
Console.WriteLine("in two func1");
}
}
class three: one
{
Example - Access Modifier - Virtual
public override void func()
{
Console.WriteLine("in three func");
}
public override void func1()
{
Console.WriteLine("in three func1");
}
}
Example - Access Modifier - Virtual
class implement
{
public static void Main(){
one o=new two();
three t=new three();
o.func(); //invokes func() of two
o.func1(); //invokes func() of two
o=t;
o.func(); //invokes func() of three
o.func1(); // invokes func1() of three
}
}
Properties
A property is an element of a programming language that
enables you to access an attribute of an object or a class.
Properties can be considered to be extensions of fields but
they can not be stored in specific memory locations.
The syntax of fields and properties is similar.
Properties consist of accessors that are executed in order
to read or write values.
There are two types of accessors, get and set.
Example - Properties
You can access class fields with
properties using the following code:
using System;
public class PropertyHolder
{
private int someProperty = 0;
Example - Properties..
public int SomeProperty
{
get
{
return someProperty;
}
set
{
someProperty = value;
}
}
}
Example - Properties..
public class PropertyTester
{
public static int Main(string[] args)
{
PropertyHolder propHold = new
PropertyHolder();
propHold.SomeProperty = 5;
Console.WriteLine(“Property Value:{0}”,
propHold.SomeProperty);
return 0;
}
}
Example – Write Only Property
You can create write-only property using the set
accessor in the property implementation.
using System;
public class PropertyHolder
{
private int someProperty = 0;
public int SomeProperty
{
set
{
someProperty = value;
Example – Write Only Property..
Console.WriteLine(“The someProperty field is
equal to {0}”,
someProperty);
} }
}
public class PropertyTest
{
public static int Main(string[] args)
{
PropertyHolder propHold = new
PropertyHolder();
propHold.SomeProperty = 5;
return 0;
}
}
Example – Read Only Property
You can create properties that are read-only by
using only the get accessor in the property
implementation.
using System;
public class PropertyHolder
{
private int someProperty = 0;
public PropertyHolder(int propVal)
{
someProperty = propVal;
}
Example – Read Only Property
public int SomeProperty
{
get
{
return someProperty;
}
}
}
Example – Read Only Property
public class PropertyTest
{
public static int Main(string[] args)
{
PropertyHolder propHold = new
PropertyHolder(5);
Console.WriteLine(“Property Value:{0}”,
propHold.SomeProperty);
return 0;
}
}
Interface
An interface is a contract that contains a collection
of function prototypes.
You can implement the interface functions in a class
in order to derive from an interface.
Interfaces can contain:
Methods
Properties
Indexers
Events
Interface
Syntax
– - interface inter { }
– - Contains only method prototypes and not fields
– - By default all are public
– - It can extend zero or more interfaces
– - A class implementing an interface should implement
all the methods
Interface
Implementing methods explicitly
Use fully qualified name for the method ie the name of the
interface and the method name.
Restrictions
You can access the explicitly defined method only thro the
interface ref.
Cannot specify an access modifier
This allows multiple methods with same signature from
diff interfaces to be defined
Example - Interface
interface Itest{
string this[int index] {
get;
set;
}
void F(int value);
string P {
get;
set;
}
}
Example - Interface
You can implement multiple inheritance using
interfaces.
For example,
interface IControl
{
void Paint();
}
Interface ITextBox: IControl
//ITextBox inherits from IControl
{
void SetText(string text);
}
Example - Interface
interface IListBox: IControl
{
void SetItems(string[] items);
}
interface IComboBox: ITextBox, IListBox
// The interface IComboBox inherits from more
than one interface, ITextBox and IListBox.
Example - Interface
You can implement multiple interfaces using
classes.
For example,
interface IMath
{
void Divide(int a, int b, out int re, out int
rem);
}
public class EditBox:IMath, ITextBox
Example - Interface
{
public void Divide(int a, int b, out int
res, out int remainder)
{…}
public void Text()
{…}
}
}
Interface
You can avoid making the interfaces public in the
implementing class by using qualified names like
void IMath.Divide (int a, int b, out int res, out int
remainder) {…}
void ITextBox.Text() {…}
Interfaces used with qualified names are also known as
explicit interface members because each member
explicitly provides names of the interface member that
is implemented.
Explicit interface members can only be called through
the respective interface.
Diff. Between Interface & Abstract
Class
In an interface class, all methods must be abstract.
In an abstract class some methods can be concrete.
In an interface class, no accessibility modifiers are
allowed, which is ok in an abstract class.
Abstract Class
A class that provides common behaviour across a set of
subclasses, but is not itself designed to have instances that
work.
If you would like to make classes that only represent base
classes, and don’t want anyone to create objects of these class
types.
It is used to indicate that a class is incomplete and that it is
intended to be used only as a base class.
Abstract Class
An abstract class cannot be instantiated.
An abstract class is permitted (but not required) to
contain abstract members.
An abstract class cannot be sealed.
An abstract method cannot be private.
If you declare an abstract method as protected, it should
be protected in its derived class.
An abstract method cannot have the modifier virtual,
because an abstract method is implicitly virtual.
An abstract member cannot be static
Example 1 - Abstract Class
using System;
namespace sampleAbstractClass
{
class sampleAbstractClass
{
static void Main(string[] args)
{
yyy a = new yyy();
www b = new www();
a.abc();
b.abc();
Console.ReadLine();
//System.Console.WriteLine("yyy abc");
}
}
Example 1 - Abstract Class
abstract class xxx
{
public abstract void abc();
//System.Console.WriteLine("yyy abc");
}
class yyy : xxx
{
public override void abc()
{
System.Console.WriteLine("yyy abc");
}
}
class www : xxx
{
public override void abc()
{
System.Console.WriteLine("www abc");
}
}
}
Example 2 - Abstract Class
using System;
using System.Collections.Generic;
using System.Text;
namespace sealed_abstract
{
abstract class absClass
{
//A Non abstract method
public int AddTwoNumbers(int Num1, int Num2)
{
return Num1 + Num2;
}
//An abstract method, to be overridden in derived
class
public abstract int MultiplyTwoNumbers(int Num1,
int Num2);
}
Example 2 - Abstract Class..
//A Child Class of absClass
class absDerived : absClass
{
static void Main(string[] args)
{
// You can create an instance of the derived class
absDerived calculate = new absDerived();
int added = calculate.AddTwoNumbers(10, 20);
Example 2 - Abstract Class..
int multiplied =
calculate.MultiplyTwoNumbers(10, 20);
Console.WriteLine("Added : {0},Multiplied : {1}",
added, multiplied);
}
//using override keyword,implementing the abstract
method
//MultiplyTwoNumbers
public override int MultiplyTwoNumbers(int
Num1, int Num2)
{
return Num1 * Num2;
}
}
}
Sealed Class
You can use sealing to limit the ways in which developers
can extend your framework.
When you seal a class, other classes cannot inherit from it.
When you seal a member, derived classes cannot override
the implementation of the member. You should not seal
types and members by default.
Sealing prevents customization of library types and
members, and impacts the perception of usability for some
developers.
In addition, extensibility is one of the fundamental benefits
of using an object-oriented framework.
You should carefully weigh decisions that restrict this
benefit.
Sealed Class..
A sealed class cannot be used as a base class.
For this reason, it cannot also be an abstract class.
Sealed classes are primarily used to prevent derivation.
Sealing a class means one can not derive from it.
Sealing a method means one can not override it.
Example :
public sealed class classSealed
{
// Class members here.
public string ID;
public double Price;
}
Example 1 - Sealed Class
using System;
using System.Collections.Generic;
using System.Text;
namespace sealed_abstract
{
sealed class SealedClass
{
public int x;
public int y;
}
Example 1 - Sealed Class..
class MainClass
{
static void Main()
{
SealedClass sc = new SealedClass();
sc.x = 110;
sc.y = 150;
Console.WriteLine("x = {0}, y = {1}",
sc.x, sc.y);
}
}
}
Session Ends
Exercise
Relax