Sie sind auf Seite 1von 42

Session 3

Classes and Objects

Classes
Objects
Methods
constructors
properties
access modifiers
method overloading

Inheritance
Polymorphism
Boxing and
UnBoxing

Classes
A class is simply an abstract model used to
define a new data types.
Basis for the object-oriented programming
Group of related methods and variables
A class is a template for an object

A class in C# is declared using the keyword class and its


members are enclosed in parenthesis

class MyClass
{
// fields, operations and properties go here
}

Syntax of class
class classname
{
type instance-variable1;
type instance-variable2;
//
type instance-variableN;
type methodname1(parameter-list)
{
// body of method
}
type methodnameN(parameter-list)
{
// body of method
}
}

class ProgramHELLO
{
static void Main(string[] args)
{
Console.WriteLine("WELCOME TO C#");
Console.ReadLine();
}
}

Objects
building blocks of object-oriented programming.
Each object is a programming unit consisting of data
(instance variables) and functionality (instance
methods).
A static method can not access the non static data
members or methods without object .
An object is created in the memory using the
keyword 'new'
Syntax:
classname obj_name=new classname();

using System;
class obj
{
public void show()
{
System.Console.WriteLine (hello) ;
}
public static void Main (String []args)
{
obj ob=new obj();
ob.show();
}
}

Method
Methods are the operations performed on the
data. A method may take some input values
through its parameters and may return a value
of a particular data type
A method is a member that implements an
action that can be performed by an object or
class.
A method is a function owned by your class.

Syntax :
type method_name (parameter-list)
{
// body of the method
}

using System
class meth
{
public void show()
{
System.Console.WriteLine (hello) ;
}
public static void Main ()
{
meth ob=new meth();
ob.show();
}
}

Constructors
Constructors are a special kind of method. A Constructor has
the following properties
It has the same name as its containing class
It has no return type
can have parameters.
It is automatically called when a new instance or objectof a
class is created,hence why it's called aconstructor.

class Super
{
public int length;
public int breadth;
public Super(int x,int y)
{
length = x;
breadth = y;
}
public int Display()
{
return(length * breadth);
}
}

class test
{
public static void Main()
{
Super s = new Super(10, 20);
int i = s.Display();
Console.WriteLine("Multiplication is :" +i);
Console.ReadLine();
}

Destructor
Destructs an instance of a class
A destructor method is called just prior to an object's final
destruction by the garbage collector.
A destructor can be used to ensure that an object terminates
cleanly.
A destructor is a function with the same name as the name of
the class but starting with the character ~.

class A
{
public A()
{
Console.WriteLine("creation A");
}
~A()
{
Console.WriteLine("destroying A");
}
}
class B : A
{
public B()
{
Console.WriteLine("creation B");
}

~B()
{
Console.WriteLine("destroying B");
}
}
class C : B
{
public C()
{
Console.WriteLine("creation C");
}

~C()
{
Console.WriteLine("destroying C");
}
}
class MainClass
{
public static void Main()
{
C c = new C();
Console.WriteLine("object created");
Console.WriteLine("press enter to destroy");
c = null;
Console.ReadLine();
}
}

Properties
It is the new feature implimented in c#.
Usually used for geting and setting the values for the data
members
Properties are named members of classes, structs, and
interfaces
They provide a flexible mechanism to read, write, or compute
the values of private fields through accessors.

Syntax
acc- modifier ret-type propertyName
{
get // Return the value to caller
{
return(var);
}
Set // Assign the value to particular variable
{
var=value
}
}

Example program
class Myclass
{
int x;
float y;
public int xprop
{
get
{
return (x);
}
set
{
x = value;
}
}

public float yprop


{
get
{
return (y);
}
set
{
y = value;
}
}
}

class MainClass
{
public static void Main()
{
Myclass m = new Myclass();
m.xprop = 10;
m.yprop = 10.3f;
Console.WriteLine("x=: " + m.xprop);
Console.WriteLine("y=: " + m.yprop);
Console.ReadLine();
}
}

Access modifiers
Access Modifier Description
Private

private members can only be accessed within the class


that contains them

protected internal

This type of member can be accessed from the current


project or from the types inherited
from their containing type

Internal

Can only be accessed from the current project

protected

Can be accessed from a containing class and types


inherited from the containing class

public

public members are not restricted to anyone. Anyone


who can see them can also access them.

Method overloading
It is possible to have more than one method
with the same name and return type but with a
different number and type of arguments
(parameters). This is called method
overloading

class demo
{
void add(int a, int b)
{
int c=a+b;
System.out.print(c + , );
}
void add(float x, float y)
{
float z=x+y;
System.out.println(z);
}
public static void main(String arg[])
{
demo ob=new demo();
ob.add(10,20);
ob.add(23.4f,34.4f);
}}

Inheritance & Polymorphism


Inheritance
Inheritance in C#
Implementing inheritance in C#
The sealed keyword

Polymorphism
Overriding the methods - virtual and override
keywords
The new keyword
Boxing and Un-boxing

Inheritance
Inheritance is the process by which one object
acquires the properties of another object.
A class that is inherited is called a superclass. The
class which does the inheriting is called a subclass
Base Class

Derived Class

Derived Class

Derived Class

Types of Inheritance
Single Inheritance.
Multilevel Inheritance.
Multiple Inheritance.
Syntax of Inheritance
Derive class: Base class

Inheritance in C#
C# allows only single class inheritance.
Multiple inheritance of classes is not allowed
in C#.
Interfaces in C# can inherit more than one
interface
multiple inheritance of interfaces is allowed in
C#

Implementing inheritance in C#

C# uses the colon ':' operator to indicate


inheritance

class MyClass
{
public int x, y;
public void readxy()
{
x = 10;
y= 20;
}
public void printxy()
{
Console.WriteLine("X= " + x);
Console.WriteLine("Y= " + y);
}
}

class MainClass
{
public static void Main()
{
MyClass1 m = new MyClass1();
m.readpq();
m.readxy();
m.printpq();
m.printxy();
Console.ReadLine();
}
}

class MyClass1 : MyClass


{
public int p, q;
public void readpq()
{
p = 30;
q = 40;
}
public void printpq()
{
Console.WriteLine("P= " + p);
Console.WriteLine("Q= " + q);
}
}

The sealed keyword


If you don't want your class to be inherited by
any class, you can mark it with the sealed
keyword. No class can inherit from a sealed
class

using System;
public sealed class demo
{
public int a = 10;
public int b = 2;
public void add()
{
int c = a + b;
Console.WriteLine(c);
}
}
public class demo1 : demo
{
public void sub()
{
int c = a - b;
Console.WriteLine(c);
}
}

class sample
{
public static void Main()
{
demo1 d = new demo1();
d.sub();
d.add();
Console.ReadLine();
}
}

Polymorphism
Polymorphism is the ability for classes to
provide different implementations of methods
that are called by the same name.
Polymorphism allows a method of a class to be
called without regard to what specific
implementation it provides.

Run time polymorphism


class Shape
{
public void Draw()
{
Console.WriteLine("Drawing Shape...");
}
}
class Circle : Shape
{
public void Draw()
{
Console.WriteLine("Drawing Circle...");
}
}
static void Main()
{
Circle theCircle = new Circle();
theCircle.Draw();
}

Overriding the methods - virtual and


override keywords
class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing Shape...");
}
}
class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing Circle...");
}
}
static void Main()
{
Shape theShape = new Circle();
theShape.Draw();
}

The new keyword


C# introduces a keyword new to mark a method as
a non-overriding method and as the one which we
don't want to use polymorphically.

Boxing and Un-boxing


. Boxing allows value types to be implicitly
treated like objects.
un-boxing is explicit conversion from object
type to value type
Boxing
int i = 5;Object
obj = i; // implicit boxing
obj.ToString();

Unboxing
int i = 5;
Object obj = i; // implicit boxing
int j = (int) obj;// explicit un-boxing

Das könnte Ihnen auch gefallen