Abstract and Virtual:
•If a class is to serve the purpose of providing common fields
and members to all subclasses, we create an Abstract class. For creating an
abstract class, we make use of the abstract keyword. Such a class cannot be
instantiated.
•Whatever functionality in common to the entire system which
functionality put together and keep into separate.
Why do we need Abstract class:
•Take three classes Human, Man and Woman. All Man and Woman
belong to Human category. In Original object exists for only
for Man or Woman. NO object exists for Human category. But
both Man and Woman have common characteristics where we can say it as Human characters.
Now you can understand that why do we need abstract class.
Abstract Rules
-> We
cannot create object for abstract class
-> we
cannot declare the abstract method in non abstract class
-> we
cannot declare the non abstract method or virtual method without declare the
body
-> We
cannot declare a body in abstract method
->
When at least one of the methods in the class is abstract.
-> Abstract
can extend only one abstract class.
->An abstract class can extend c# class only
Difference between Abstract and Virtual
Abstract
Virtual
No Keyword
Can have implementation?
No
Yes
Yes
Can override?
Must
Can but not a must
You can declare a new method with the same name
Which keyword to use to provide new implementation in the concrete
class?
override
override
No keyword needed
If an object is created of thebase class type,
which method will be executed?
Concrete implementation
The parent implementation will be called only if no
implementation is provided in the concrete class
Parent implementation
Concrete implementation
Concrete implementation
The parent implementation will be called only if no
implementation is provided in the concrete class
Interface
•Interface
nothing but skeleton of the class
•We
cant create object for interface
•It s
a Pure abstraction
•Pure
abstraction means you cannot have concrete methods
•Interfaces
should not have any concrete methods, it should only have method
declarations.
A
method with definition is concrete method
In
which scenario we use Interfaces:
•If
your child classes should all implement a certain group of
methods/functionalities but each of the child classes is free to provide its
own implementation then use interfaces.
•For
e.g. if you are implementing a class hierarchy for vehicles implement an
interface called Vehicle which has properties like
Color MaxSpeed etc. and methods like Drive(). All child classes like
Car Scooter Airplane Solar Car etc. should derive from this base interface but
provide a separate implementation of the methods and properties exposed by
Vehicle
Which
one is advantage over another:
•Interface
has advantage over Abstract class. Since you can not use abstract class for
already inherited class. But you can use interface for already inherited
class.
Difference
between Abstract and Interface:
Interface
Abstract
1.A class can implement
multiple interface
1.Abstract can
extend only one abstract class.
●
●
2.An interface can extend
another interface
2.An abstract class
can extend c# class only
●
●
3.Interface contain only
method and propertied . (can t declare variables)
3.It contains private
member also we can put some methods with implementation.
●
●
4.EX:
4.Ex:
Public interface
Public abstract class vehicles
{
{
Void accelerator();
Private intnoofwheels;
String engine{
Private string color;
Get;
Public abstract string engine
Set;
{
}
Get;
}
Set;
5.I can say that interface is for
"PURE ABSTRACTION". Pure abstraction means you can not
have concrete methods. A method with definition is Concrete method.
}
●
Public abstract void accelerator();
}
5. But abstract class
can have concrete methods.
Implementing
few methods of a interface class-C#
•We
declared 10 methods in interface, now we should implement all 10 methods. But
we want implement 2 methods only.
•How?
•Example.
Sealed
Class:
•Sealed
classes are used to restrict the inheritance feature of object oriented
programming
•A
sealed class cannot be used as a base class.
•Sealing
a class means one cannot derive from it.
•A
method means one cannot override it.
•Example.
Polymorphism
•Polymorphism
means many forms.
•Polymorphism
is the ability to process objects differently depending on their data
types.
•Polymorphism
is the ability to redefine methods for derived classes.
Polymorphism
provides following features:
•It
allows you to invoke methods of derived class through base class reference
during runtime.
•It
has the ability for classes to provide different implementations of methods
that are called through the same name.
Polymorphism
Polymorphism
is of two types:
1.Compile
time polymorphism/Overloading
2.Runtime
polymorphism/Overriding
Compile
Time Polymorphism
• Compile
time polymorphism is method overloading. It is also called early binding.
• In
method overloading method performs the different task at the different input
parameters.
Runtime
Time Polymorphism
• Runtime
time polymorphism is done using inheritance and virtual functions. Method
overriding is called runtime polymorphism. It is also called late
binding
Overloading:
•Overloading a
method simply involves having another method with the same
prototype.
Overriding
•To
change the behavior of the method for the derived class
Note
•Method
overloading has nothing to do with inheritance or virtual
methods.
Following
are examples of methods having different overloads:
•void area(int side);
•void area(int l, int b);
•Void
area(int l, double b, int c)
•Void
area(double b, int l, int c)
•void area(float radius);
Polymorphism
When
and why to use method overloading
• Use
method overloading in situation where you want a class to be able to do
something, but there is more than one possibility for what information is
supplied to the method that carries out the task.
• You
should consider overloading a method when you for some reason need a couple of
methods that take different parameters, but conceptually do the same
thing.
•Example
•
• Void
Area(int a) //Square
•Void
Area(int h,int w) //Rectangle
•Void
Area(int a, int l, int w)
//Cylinder
•Void
Area(int pi, int r , int l)
//Cone
•Void
Area(int pi, int r ) //Circle
When
and why to use method overriding
•Its
obvious that when we do inheritance between two classes, all the methods and
properties of the first class are derived to the other class so that this
becomes the child class which adobes all the functionality of base class.
It can also possesses its own separate methods.
•But
there is a big problem in inheriting the second class to the first class as it
adobes all the methods same as the base class has, which means that after
inheritance both(base class& child class) have the methods of same name and
same body as shown in this example
•Example,
we
have seen example of overriding
•how
many overload of a method we can have?
•
•What?
•Type?
•When?
•Overloading?
•Overriding?
•static
binding?
•Late
binding?
String
and String Builder:
•Strings
are Immutable (Not Modifiable). If you try to modify the string it
actually creates a new string and the old string will be then ready for garbage
collection.
•String
Builder when instantiated, creates a new string with predefined capacity and up
to that capacity it can accommodate string without needing to create a new
memory location for the string....i mean it is modifiable and can also
grow as and when needed.
•The
most common operation with a string is concatenation. This activity has to be
performed very efficiently. When we use the "String" object to
concatenate two strings, the first string is combined to the other string by
creating a, and then the old string is deleted. This process is a little long.
Hence we say "Strings are immutable".
•When
we make use of the "String Builder" object, the Append method is
used. This means, an insertion is done on the existing string. Operation on
String Builder object is faster than String operations, as the copy is done to
the same location. Usage of String Builder is more efficient incase large
amounts of string manipulations have to be performed.
•String
Example:
String myString=”Welcome”;
myString=myString+”Software
Team”;
•String
builder Example
StringBuilder sb =
new StringBuilder();
sb.Append("raj");
sb.Append("mani");
String
builder
Methods:
Ex:
Append, insert, remove, replace and Tostring.
Performance:
1.Avoid
short appends.
2.Cache
objects.
3.Avoid
some data types.
4.Data
Types
5.Append
Chars
6.Stringbuilder Comparison
7.Append
integers
8.Capacity
●
9.Example:
http://www.dotnetperls.com/stringbuilder
Override:
When a method of a base class is overridden in a derived class, the version in
the derived class is used, even if the calling code didn't "know"
that the object was an instance of the derived class.
New:
If you use the new keyword instead of override, the method in the derived class
doesn't override the method in the base class, it merely hides it.
If
you don't specify either new or overrides, the resulting output is the same as
if you specified new, but you'll also get a compiler warning (as you may not be
aware that you're hiding a method in the base class method, or indeed you may
have wanted to override it, and merely forgot to include the
keyword).
Override:
used with virtual/abstract/override type of method in base
class
New:
when base class has not declared method as
virtual/abstract/override
Static
class , method and variables
12
Features of Static class
1.A
static class cannot be instantiated
2.Static
class can have only static members.
3.Member
of the Static class can be accessed by class name
itself.
4.Static
class is sealed. So static class cannot be inherited.
5.Static
class contains only static constructors.
6.Static
class cannot have instance constructors.
7.Static
class can only be inherited only from object
class.
8.Static
class is preceded by keyword static.
9.Static
constructor of static class called only once.
10.Static
class has private constructors
11.Static
classes cannot implement interfaces
12.static
classes cannot have instance constructors
Notable
Points here are:
•A
static method can be invoked directly from the class
level
•A
static method not requires any class object
•Any
main() method is shared through entire class scope so it always appears with
static keyword.
Static
method
•A
static method can access only static fields.
•A
static method cannot access non static fields.
•A
static method cannot be called using object of a class
•A
static method can be called using class name.
• Static
field can be used using class name.
•Static
method cannot be override or overload.
•We
can't put access modifier in static constructor?
Constructor:
http://www.programcall.com/16/csnet/constructor-types-with-example-programs-in-csnet.aspx
•A
static constructor does not take access modifiers or have
parameters.
•A
static constructor is called automatically to initialize
the class before the first instance is created or any static members
are referenced.
•A
static constructor cannot be called directly.
•The
user has no control on when the static constructor is executed in the program.
•A
typical use of static constructors is when the class is using a log file and
the constructor is used to write entries to this file.
•Static
constructors are also useful when creating wrapper classes for unmanaged code,
when the constructor can call theLoadLibrary method.
•If a
static constructor throws an exception, the runtime will not invoke it a second
time, and the type will remain uninitialized for the lifetime of the
application domain in which your program is
running.
http://msdn.microsoft.com/en-us/library/k9x6w0hc.aspx
what
is difference between instance constructor and Static constructor
inC#.Net
Main
Differences are :
• A Constructor
declare using Static modifier was known as Static Constructor. Rest of
all were Instance Constructor.
•Static
Constructor are responsible for initialization of static variables and Instance
Constructor for initialization of instance constructor.
•A
static constructor is called only one's in the execution of a class. Where as
instance constructor gets called each time we create the object of the class.
if no object is created. It is not called at all.
•static
constructor is the first block of which gets executed under the
class.
•A
static constructor can't be parameterized because explicit calling of the
constructor is not done.
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace
ConsoleApplication1{
class staticconstructor
{
static staticconstructor()
{
Console.WriteLine("static
constructor");
}
staticconstructor()
{
Console.WriteLine("non-static
constructor");
}
static void Main()
{
Console.WriteLine("main
method");
staticconstructor st =
new staticconstructor();
staticconstructor st1 =
new staticconstructor();
Console.ReadLine();
}
}
}
o/p: static constructor, main method , non-static constructor ,
non-static constructor
Notable
Points here are:
•A
static method can be invoked directly from the class
level
•A
static method not requires any class object
•Any
main() method is shared through entire class scope so it always appears with
static keyword.
Static
method
•A
static method can access only static fields.
•A
static method cannot access non static fields.
•A
static method cannot be called using object of a class
•A
static method can be called using class name.
• Static
field can be used using class name.
•Static
method cannot be override.
•We
can't put access modifier in static constructor?
Access ViewState from
Static method in aspx page
You
can get the reference to the page
via HttpContext.CurrentHandler.
public static string GetData(int CustomerID)
{
Page page = HttpContext.Current.Handler as Page;
if (page
!= null)
{
string outputToReturn = "";
page.ViewState["MyVal"]="Hello";
return outputToReturn;
//btw,
what a strange method!
}
}
collection
Four
important categories
•Index
based
–Array
–List
•Key
value pair
–Hash
table
–Sort
list
•Prioritized
collection
–Queue
–Stack
•Specialized
collection
–String
collection
–Hybrid
dictionary
array and array list
Array
Array List
1.System namespace
1.System.collection namespace
2.Char[] vowel=new
char[];
2.Arraylist list
=new arraylist()
3.Capacity of an array is
fixed
3.Array list can increase
and decrease size dynamically
4.It collection of same
data type
4.It can hold different
data type
5.Array can have multiple
dimensions
5.Array list can have one
dimension.
6.Ex:
6.Ex:
Hash Table and Array List
Hash Table
Array List
1.It is a dictionary
1.Collection of data
2.It is both index and
key based
2.Index based
3.Hash table is a map
3.It is a list
4.Here we can add data
with key
4.We can only add items
to the list
5.Retrieving by key in
hash table is faster than t\retrieving by array list
5.Here we can add any
data type value, every item in array list is treated as object.
hash table
hashTable:
HashTable stores a key-value pair type collection of data.
NOTE:
Both
key and value are object
the key values are UNIQUE
Functions:
•Clear
•Remove
•Containskey
•Containsvalue
•Contains
•Add
Variables
•Count
Remove:
hash_table_name.Remove(key);
Remove
All Values:
hash_table_name.Clear()
ContainsKey(Key)
its
returns true or false..
ContainsValue(value):
its returns true or false..
EXAMPLE
Hashtable
sampleHt = New Hashtable
sampleHt.add("US",”united
States”) // Area- key. 1000 -
valuesampleHt.add("IND",”India”)
// To fetch the value we can use name, for example
String countryname=convert.tostring(sampleHt[“US”]);
sampleHt.remove(“US”);
IDictionaryEnumerator idic= sampleHt.GetEnumerator();
while(idic.MoveNext())
{
idic.Value.ToString();
}
Example
1:
// We
cannot crate object for abstract class
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public abstract class AbstactClass
{
public abstract string GetAbstractValue();
}
public class main1
{
static void Main(string[]
args)
{
AbstactClass absObj = new AbstactClass();
}
}
}
Error
1 Cannot create an instance of the abstract class
or interface 'ConsoleApplication1.AbstactClass'
Example
2:
// we
cannot declare the abstract method in non abstract class
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class AbstactClass
{
public abstract string GetAbstractValue();
}
public class main1
{
static void Main(string[]
args)
{
AbstactClass absObj = new AbstactClass();
}
}
}
Error:
'ConsoleApplication1.AbstactClass.GetAbstractValue()'
is abstract but it is contained in non-abstract class
'ConsoleApplication1.AbstactClass'
Example
2:
// we
cannot declare the non abstract method without definition
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public abstract class AbstactClass
{
public abstract void GetAbstractValue();
public void GetValue();
// public void GetValue(); // don’t throw error
//{}
}
public class Test : AbstactClass
{
public override void GetAbstractValue()
{
}
}
public class main1
{
static void Main(string[]
args)
{
Test absObj = new Test();
absObj.GetAbstractValue();
}
}
}
// We
cannot declare a body in abstract method
public abstract class AbstactClass
{
public abstract void GetAbstractValue()
{
}
public void GetValue()
{
Console.WriteLine("Get abstract method");
}
}
//
Abstract, virtual method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public abstract class BaseClass
{
public abstract string GetAbstractValue();
public virtual string GetVirtualValue()
{
return "Base
GetVirtualValue";
}
public string GetValue()
{
return "Base
GetValue";
}
}
public class SubClass : BaseClass
{
public override string GetAbstractValue()
{
return "Sub
Get Abstract Value";
}
public override string GetVirtualValue()
{
return "Sub
GetVirtualValue";
}
public string GetValue() //new
{
return "Concrete
GetValue";
}
}
class Test
{
static void Main(string[]
args)
{
BaseClass instance1 = new SubClass();
Console.WriteLine(instance1.GetAbstractValue());
Console.WriteLine(instance1.GetVirtualValue());
Console.WriteLine(instance1.GetValue());
SubClass instance2 = new SubClass();
Console.WriteLine(instance2.GetAbstractValue());
Console.WriteLine(instance2.GetVirtualValue());
Console.WriteLine(instance2.GetValue());
Console.Read();
}
}
}
//Since
you cannot use abstract class for already inherited class. But you can use
interface for already inherited class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public interface inrefacename
{
void somemethod();
}
public abstract class BaseClass
{
public abstract string GetAbstractValue();
public virtual string GetVirtualValue()
{
return "Base
GetVirtualValue";
}
public string GetValue()
{
return "Base
GetValue";
}
}
public class classtest
{
public string GetValue1()
{
return "Base
GetValue";
}
}
public class classtest1 : classtest, inrefacename // baseclass – abstract class (will not
use abstract class)
{
public string GetValue4()
{
return "Base
GetValue";
}
public void somemethod()
{
Console.WriteLine("somemethod");
}
}
public class SubClass : BaseClass
{
public override string GetAbstractValue()
{
return "Sub
Get Abstract Value";
}
public override string GetVirtualValue()
{
return "Sub
GetVirtualValue";
}
public string GetValue() //new
{
return "Concrete
GetValue";
}
}
class Test
{
static void Main(string[]
args)
{
BaseClass instance1 = new SubClass();
Console.WriteLine(instance1.GetAbstractValue());
Console.WriteLine(instance1.GetVirtualValue());
Console.WriteLine(instance1.GetValue());
SubClass instance2 = new SubClass();
Console.WriteLine(instance2.GetAbstractValue());
Console.WriteLine(instance2.GetVirtualValue());
Console.WriteLine(instance2.GetValue());
Console.Read();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public interface IMyInterface
{
void SomeMethod();
void SomeOtherMethod();
}
public abstract class MyClass : IMyInterface
{
// Really implementing this
public void SomeMethod()
{
// ...
}
// Derived class must implement this
public abstract void SomeOtherMethod();
}
class Test
{
static void Main(string[]
args)
{
Console.Read();
}
}
}
//
Achieve multiple inheritance concept via interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public interface IA1
{
void m2();
void m();
}
public interface IA2
{
void m2();
}
public class CA : IA1, IA2
{
public void m()
{
}
public void m2()
{
Console.WriteLine("A");
}
}
public class CA1 : CA
{
public void m()
{
}
public void m2()
{
Console.WriteLine("B");
}
}
public class main1
{
static void Main(string[]
args)
{
CA c1 = new CA();
c1.m();
c1.m2();
CA1 ca1 = new CA1();
ca1.m();
ca1.m2();
Console.Read();
}
}
}
// Dynamic Binding (hiding)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class A
{
public virtual void WhoAreYou()
{ Console.WriteLine("I am an A"); }
public virtual void WhoAreYou1()
{ Console.WriteLine("I am an A1"); }
public virtual void WhoAreYou2()
{ Console.WriteLine("I am an A2"); }
}
class B : A
{
public override void WhoAreYou()
{ Console.WriteLine("I am a B"); }
}
class C : B
{
public new virtual void WhoAreYou() { Console.WriteLine("I am a C"); }
// public override void WhoAreYou1() {
Console.WriteLine("I am an C1"); }
public override void WhoAreYou2()
{ Console.WriteLine("I am an C2"); }
}
class D : C
{
public override void WhoAreYou()
{ Console.WriteLine("I am a D"); }
public override void WhoAreYou2()
{ Console.WriteLine("I am an D2"); }
}
class Test
{
static void Main(string[]
args)
{
C c = new D(); // where to extend C class in D
c.WhoAreYou();//
"I am a D"
A a = new D(); // where to extend A class in B
a.WhoAreYou();//
"I am a B"
A a1 = new D(); // No extend - virtual method
a1.WhoAreYou1(); //
"I am a A1"
B a2 = new C(); // where to extend b class in c
a2.WhoAreYou2(); //
"I am a C2"
B a3 = new D(); //
where to extend b class in c
a3.WhoAreYou2(); //
"I am a D2"
Console.ReadLine();
}
}
}
Sealed Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public sealed class classsealed
{
public string ID;
public double Price;
public string GetValue()
{
return "Sealed
class";
}
}
//public class subsealedclasss : classsealed
//{
// public string
subsealedmethod()
// {
//
return "subSealed class";
// }
//}
class Test
{
static void Main(string[]
args)
{
classsealed sealedobject = new classsealed();
Console.WriteLine(sealedobject.GetValue());
sealedobject.ID = "10";
sealedobject.Price = 12.23;
//subsealedclassssubsealedobject = new
subsealedclasss();
//subsealedobject.subsealedmethod();
Console.Read();
}
}
}
//
Static Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public static class staticclass // : object //7. Static class can only be inherited only from
object class
{
// int a; // 2. Static class
can have only static members.
static int i=1; // access
//public void method() // Static class can
have only static method.
//{
// Console.WriteLine("Test
method");
//}
public static void method1()
{
Console.WriteLine("Test static method");
Console.WriteLine(i);
}
//staticclass() // static classess cannot
have instance constructors
//{
// Console.WriteLine("instance
constructor");
//}
static staticclass()
{
Console.WriteLine("static constructor");
}
}
public class Bus
{
// Static constructor:
static Bus()
{
System.Console.WriteLine("The static constructor invoked.");
}
public static void Drive()
{
System.Console.WriteLine("The Drive method invoked.");
}
}
//public class Testclass :
staticclass // 4. Static class is sealed.
So static class cannot be inherited
//{
//}
class Test
{
static void Main(string[]
args)
{
// staticclass obj = new
staticclass(); // static class cannot be instantiated
staticclass.method1(); // first constructor only executed
//staticclass.i = 6;
Bus.Drive();
Console.ReadLine();
}
}
}
//
static method
public class Bus
{
// Static constructor:
static Bus()
{
System.Console.WriteLine("The
static constructor invoked.");
}
public static void Drive()
{
System.Console.WriteLine("The
Drive method invoked.");
}
}
class TestBus
{
static void Main()
{
Bus.Drive();
}
}
// A static constructor is called automatically
to initialize and first called
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[]
args)
{
Child t = new Child();
}
}
class Parent
{
public Parent()
{
Console.WriteLine("Parent Instance Constructor");
Console.ReadKey();
}
static Parent()
{
Console.WriteLine("Parent Static Constructor");
Console.ReadKey();
}
}
class Child : Parent
{
public Child()
{
Console.WriteLine("Child Instance Constructor");
Console.ReadKey();
}
static Child()
{
Console.WriteLine("Child Static Constructor");
Console.ReadKey();
}
}
}
Output:
Child
Static Constructor
Parent
Static Constructor
Parent
Instance Constructor
Child
Instance Constructor
//
Hash table
HashTable:
HashTable stores a key-value pair type
collection of data.
NOTE:
Both key and value are object
the key values are UNIQUE
Functions:
1. Clear
2. Remove
3. Containskey
4. Containsvalue
5. Contains
6. Add
Variables
1. Count
Remove:
hash_table_name.Remove(key);
Remove All Values:
hash_table_name.Clear()
ContainsKey(Key)
its returns true or false..
ContainsValue(value):
EXAMPLE
Hashtable sampleHt = New Hashtable
sampleHt.add("US",”united States”) // Area- key. 1000 -
value
sampleHt.add("IND",”India”)
// To fetch the value we can use name, for
example
String countryname=convert.tostring(sampleHt[“US”]);
sampleHt.remove(“US”);
IDictionaryEnumerator idic= sampleHt.GetEnumerator();
while(idic.MoveNext())
{
idic.Value.ToString();
}
// Why c# not support multiple inheritance?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class A
{
protected void samplemethod();
}
class B
{
protected void samplemethod();
}
class C : A, B
{
}
class Test
{
static void Main(string[]
args)
{
C obj = new C();
obj.samplemethod(); // here
which method to call now A or B?
}
}
}
Interface with objects:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public interface IMyInterface
{
void SomeMethod();
void SomeOtherMethod();
}
public class MyClass : IMyInterface
{
// Really implementing this
public void SomeMethod()
{
Console.WriteLine("Interface object");
}
// Derived class must implement this
public void SomeOtherMethod()
{
}
}
class Test
{
static void Main(string[]
args)
{
IMyInterface obj = new MyClass();
obj.SomeMethod();
Console.ReadLine();
}
}
}
Abstract and Virtual:
•If a class is to serve the purpose of providing common fields
and members to all subclasses, we create an Abstract class. For creating an
abstract class, we make use of the abstract keyword. Such a class cannot be
instantiated.
•Whatever functionality in common to the entire system which
functionality put together and keep into separate.
Why do we need Abstract class:
•Take three classes Human, Man and Woman. All Man and Woman
belong to Human category. In Original object exists for only
for Man or Woman. NO object exists for Human category. But
both Man and Woman have common characteristics where we can say it as Human characters.
Now you can understand that why do we need abstract class.
Abstract Rules
-> We
cannot create object for abstract class
-> we
cannot declare the abstract method in non abstract class
-> we
cannot declare the non abstract method or virtual method without declare the
body
-> We
cannot declare a body in abstract method
->
When at least one of the methods in the class is abstract.
-> Abstract
can extend only one abstract class.
->An abstract class can extend c# class only
Difference between Abstract and Virtual
Abstract
|
Virtual
|
No Keyword
|
|
Can have implementation?
|
No
|
Yes
|
Yes
|
Can override?
|
Must
|
Can but not a must
|
You can declare a new method with the same name
|
Which keyword to use to provide new implementation in the concrete
class?
|
override
|
override
|
No keyword needed
|
If an object is created of thebase class type,
which method will be executed?
|
Concrete implementation
|
The parent implementation will be called only if no
implementation is provided in the concrete class
|
Parent implementation
|
Concrete implementation
|
Concrete implementation
|
The parent implementation will be called only if no
implementation is provided in the concrete class
|
Interface
•Interface
nothing but skeleton of the class
•We
cant create object for interface
•It s
a Pure abstraction
•Pure
abstraction means you cannot have concrete methods
•Interfaces
should not have any concrete methods, it should only have method
declarations.
A
method with definition is concrete method
In
which scenario we use Interfaces:
•If
your child classes should all implement a certain group of
methods/functionalities but each of the child classes is free to provide its
own implementation then use interfaces.
•For
e.g. if you are implementing a class hierarchy for vehicles implement an
interface called Vehicle which has properties like
Color MaxSpeed etc. and methods like Drive(). All child classes like
Car Scooter Airplane Solar Car etc. should derive from this base interface but
provide a separate implementation of the methods and properties exposed by
Vehicle
Which
one is advantage over another:
•Interface
has advantage over Abstract class. Since you can not use abstract class for
already inherited class. But you can use interface for already inherited
class.
Difference
between Abstract and Interface:
Interface
|
Abstract
|
1.A class can implement
multiple interface
|
1.Abstract can
extend only one abstract class.
|
●
|
●
|
2.An interface can extend
another interface
|
2.An abstract class
can extend c# class only
|
●
|
●
|
3.Interface contain only
method and propertied . (can t declare variables)
|
3.It contains private
member also we can put some methods with implementation.
|
●
|
●
|
4.EX:
|
4.Ex:
|
Public interface
|
Public abstract class vehicles
|
{
|
{
|
Void accelerator();
|
Private intnoofwheels;
|
String engine{
|
Private string color;
|
Get;
|
Public abstract string engine
|
Set;
|
{
|
}
|
Get;
|
}
|
Set;
|
5.I can say that interface is for
"PURE ABSTRACTION". Pure abstraction means you can not
have concrete methods. A method with definition is Concrete method.
|
}
|
●
|
Public abstract void accelerator();
|
}
|
|
5. But abstract class
can have concrete methods.
|
Implementing
few methods of a interface class-C#
•We
declared 10 methods in interface, now we should implement all 10 methods. But
we want implement 2 methods only.
•How?
•Example.
Sealed
Class:
•Sealed
classes are used to restrict the inheritance feature of object oriented
programming
•A
sealed class cannot be used as a base class.
•Sealing
a class means one cannot derive from it.
•A
method means one cannot override it.
•Example.
Polymorphism
•Polymorphism
means many forms.
•Polymorphism
is the ability to process objects differently depending on their data
types.
•Polymorphism
is the ability to redefine methods for derived classes.
Polymorphism
provides following features:
•It
allows you to invoke methods of derived class through base class reference
during runtime.
•It
has the ability for classes to provide different implementations of methods
that are called through the same name.
Polymorphism
Polymorphism
is of two types:
1.Compile
time polymorphism/Overloading
2.Runtime
polymorphism/Overriding
Compile
Time Polymorphism
• Compile
time polymorphism is method overloading. It is also called early binding.
• In
method overloading method performs the different task at the different input
parameters.
Runtime
Time Polymorphism
• Runtime
time polymorphism is done using inheritance and virtual functions. Method
overriding is called runtime polymorphism. It is also called late
binding
Overloading:
•Overloading a
method simply involves having another method with the same
prototype.
Overriding
•To
change the behavior of the method for the derived class
Note
•Method
overloading has nothing to do with inheritance or virtual
methods.
Following
are examples of methods having different overloads:
•void area(int side);
•void area(int l, int b);
•Void
area(int l, double b, int c)
•Void
area(double b, int l, int c)
•void area(float radius);
Polymorphism
When
and why to use method overloading
• Use
method overloading in situation where you want a class to be able to do
something, but there is more than one possibility for what information is
supplied to the method that carries out the task.
• You
should consider overloading a method when you for some reason need a couple of
methods that take different parameters, but conceptually do the same
thing.
•Example
•
• Void
Area(int a) //Square
•Void
Area(int h,int w) //Rectangle
•Void
Area(int a, int l, int w)
//Cylinder
•Void
Area(int pi, int r , int l)
//Cone
•Void
Area(int pi, int r ) //Circle
When
and why to use method overriding
•Its
obvious that when we do inheritance between two classes, all the methods and
properties of the first class are derived to the other class so that this
becomes the child class which adobes all the functionality of base class.
It can also possesses its own separate methods.
•But
there is a big problem in inheriting the second class to the first class as it
adobes all the methods same as the base class has, which means that after
inheritance both(base class& child class) have the methods of same name and
same body as shown in this example
•Example,
we
have seen example of overriding
•how
many overload of a method we can have?
•
•What?
•Type?
•When?
•Overloading?
•Overriding?
•static
binding?
•Late
binding?
String
and String Builder:
•Strings
are Immutable (Not Modifiable). If you try to modify the string it
actually creates a new string and the old string will be then ready for garbage
collection.
•String
Builder when instantiated, creates a new string with predefined capacity and up
to that capacity it can accommodate string without needing to create a new
memory location for the string....i mean it is modifiable and can also
grow as and when needed.
•The
most common operation with a string is concatenation. This activity has to be
performed very efficiently. When we use the "String" object to
concatenate two strings, the first string is combined to the other string by
creating a, and then the old string is deleted. This process is a little long.
Hence we say "Strings are immutable".
•When
we make use of the "String Builder" object, the Append method is
used. This means, an insertion is done on the existing string. Operation on
String Builder object is faster than String operations, as the copy is done to
the same location. Usage of String Builder is more efficient incase large
amounts of string manipulations have to be performed.
•String
Example:
String myString=”Welcome”;
myString=myString+”Software
Team”;
•String
builder Example
StringBuilder sb =
new StringBuilder();
sb.Append("raj");
sb.Append("mani");
String
builder
Methods:
Ex:
Append, insert, remove, replace and Tostring.
Performance:
1.Avoid
short appends.
2.Cache
objects.
3.Avoid
some data types.
4.Data
Types
5.Append
Chars
6.Stringbuilder Comparison
7.Append
integers
8.Capacity
●
9.Example:
http://www.dotnetperls.com/stringbuilder
Override:
When a method of a base class is overridden in a derived class, the version in
the derived class is used, even if the calling code didn't "know"
that the object was an instance of the derived class.
New:
If you use the new keyword instead of override, the method in the derived class
doesn't override the method in the base class, it merely hides it.
If
you don't specify either new or overrides, the resulting output is the same as
if you specified new, but you'll also get a compiler warning (as you may not be
aware that you're hiding a method in the base class method, or indeed you may
have wanted to override it, and merely forgot to include the
keyword).
Override:
used with virtual/abstract/override type of method in base
class
New:
when base class has not declared method as
virtual/abstract/override
Static
class , method and variables
12
Features of Static class
1.A
static class cannot be instantiated
2.Static
class can have only static members.
3.Member
of the Static class can be accessed by class name
itself.
4.Static
class is sealed. So static class cannot be inherited.
5.Static
class contains only static constructors.
6.Static
class cannot have instance constructors.
7.Static
class can only be inherited only from object
class.
8.Static
class is preceded by keyword static.
9.Static
constructor of static class called only once.
10.Static
class has private constructors
11.Static
classes cannot implement interfaces
12.static
classes cannot have instance constructors
Notable
Points here are:
•A
static method can be invoked directly from the class
level
•A
static method not requires any class object
•Any
main() method is shared through entire class scope so it always appears with
static keyword.
Static
method
•A
static method can access only static fields.
•A
static method cannot access non static fields.
•A
static method cannot be called using object of a class
•A
static method can be called using class name.
• Static
field can be used using class name.
•Static
method cannot be override or overload.
•We
can't put access modifier in static constructor?
Constructor:
http://www.programcall.com/16/csnet/constructor-types-with-example-programs-in-csnet.aspx
•A
static constructor does not take access modifiers or have
parameters.
•A
static constructor is called automatically to initialize
the class before the first instance is created or any static members
are referenced.
•A
static constructor cannot be called directly.
•The
user has no control on when the static constructor is executed in the program.
•A
typical use of static constructors is when the class is using a log file and
the constructor is used to write entries to this file.
•Static
constructors are also useful when creating wrapper classes for unmanaged code,
when the constructor can call theLoadLibrary method.
•If a
static constructor throws an exception, the runtime will not invoke it a second
time, and the type will remain uninitialized for the lifetime of the
application domain in which your program is
running.
http://msdn.microsoft.com/en-us/library/k9x6w0hc.aspx
what
is difference between instance constructor and Static constructor
inC#.Net
Main
Differences are :
• A Constructor
declare using Static modifier was known as Static Constructor. Rest of
all were Instance Constructor.
•Static
Constructor are responsible for initialization of static variables and Instance
Constructor for initialization of instance constructor.
•A
static constructor is called only one's in the execution of a class. Where as
instance constructor gets called each time we create the object of the class.
if no object is created. It is not called at all.
•static
constructor is the first block of which gets executed under the
class.
•A
static constructor can't be parameterized because explicit calling of the
constructor is not done.
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace
ConsoleApplication1{
class staticconstructor
{
static staticconstructor()
{
Console.WriteLine("static
constructor");
}
staticconstructor()
{
Console.WriteLine("non-static
constructor");
}
static void Main()
{
Console.WriteLine("main
method");
staticconstructor st =
new staticconstructor();
staticconstructor st1 =
new staticconstructor();
Console.ReadLine();
}
}
}
o/p: static constructor, main method , non-static constructor ,
non-static constructor
Notable
Points here are:
•A
static method can be invoked directly from the class
level
•A
static method not requires any class object
•Any
main() method is shared through entire class scope so it always appears with
static keyword.
Static
method
•A
static method can access only static fields.
•A
static method cannot access non static fields.
•A
static method cannot be called using object of a class
•A
static method can be called using class name.
• Static
field can be used using class name.
•Static
method cannot be override.
•We
can't put access modifier in static constructor?
Access ViewState from
Static method in aspx page
You
can get the reference to the page
via HttpContext.CurrentHandler.
public static string GetData(int CustomerID)
{
Page page = HttpContext.Current.Handler as Page;
if (page
!= null)
{
string outputToReturn = "";
page.ViewState["MyVal"]="Hello";
return outputToReturn;
//btw,
what a strange method!
}
}
collection
Four
important categories
•Index
based
–Array
–List
•Key
value pair
–Hash
table
–Sort
list
•Prioritized
collection
–Queue
–Stack
•Specialized
collection
–String
collection
–Hybrid
dictionary
array and array list
|
|
Array
|
Array List
|
1.System namespace
|
1.System.collection namespace
|
2.Char[] vowel=new
char[];
|
2.Arraylist list
=new arraylist()
|
3.Capacity of an array is
fixed
|
3.Array list can increase
and decrease size dynamically
|
4.It collection of same
data type
|
4.It can hold different
data type
|
5.Array can have multiple
dimensions
|
5.Array list can have one
dimension.
|
6.Ex:
|
6.Ex:
|
Hash Table and Array List
|
|
Hash Table
|
Array List
|
1.It is a dictionary
|
1.Collection of data
|
2.It is both index and
key based
|
2.Index based
|
3.Hash table is a map
|
3.It is a list
|
4.Here we can add data
with key
|
4.We can only add items
to the list
|
5.Retrieving by key in
hash table is faster than t\retrieving by array list
|
5.Here we can add any
data type value, every item in array list is treated as object.
|
hash table
|
hashTable:
HashTable stores a key-value pair type collection of data.
NOTE:
Both
key and value are object
the key values are UNIQUE
Functions:
•Clear
•Remove
•Containskey
•Containsvalue
•Contains
•Add
Variables
•Count
Remove:
hash_table_name.Remove(key);
Remove
All Values:
hash_table_name.Clear()
ContainsKey(Key)
its
returns true or false..
ContainsValue(value):
its returns true or false..
EXAMPLE
Hashtable
sampleHt = New Hashtable
sampleHt.add("US",”united
States”) // Area- key. 1000 -
valuesampleHt.add("IND",”India”)
// To fetch the value we can use name, for example
String countryname=convert.tostring(sampleHt[“US”]);
sampleHt.remove(“US”);
IDictionaryEnumerator idic= sampleHt.GetEnumerator();
while(idic.MoveNext())
{
idic.Value.ToString();
}
Example
1:
// We
cannot crate object for abstract class
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public abstract class AbstactClass
{
public abstract string GetAbstractValue();
}
public class main1
{
static void Main(string[]
args)
{
AbstactClass absObj = new AbstactClass();
}
}
}
Error
1 Cannot create an instance of the abstract class
or interface 'ConsoleApplication1.AbstactClass'
Example
2:
// we
cannot declare the abstract method in non abstract class
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class AbstactClass
{
public abstract string GetAbstractValue();
}
public class main1
{
static void Main(string[]
args)
{
AbstactClass absObj = new AbstactClass();
}
}
}
Error:
'ConsoleApplication1.AbstactClass.GetAbstractValue()'
is abstract but it is contained in non-abstract class
'ConsoleApplication1.AbstactClass'
Example
2:
// we
cannot declare the non abstract method without definition
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public abstract class AbstactClass
{
public abstract void GetAbstractValue();
public void GetValue();
// public void GetValue(); // don’t throw error
//{}
}
public class Test : AbstactClass
{
public override void GetAbstractValue()
{
}
}
public class main1
{
static void Main(string[]
args)
{
Test absObj = new Test();
absObj.GetAbstractValue();
}
}
}
// We
cannot declare a body in abstract method
public abstract class AbstactClass
{
public abstract void GetAbstractValue()
{
}
public void GetValue()
{
Console.WriteLine("Get abstract method");
}
}
//
Abstract, virtual method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public abstract class BaseClass
{
public abstract string GetAbstractValue();
public virtual string GetVirtualValue()
{
return "Base
GetVirtualValue";
}
public string GetValue()
{
return "Base
GetValue";
}
}
public class SubClass : BaseClass
{
public override string GetAbstractValue()
{
return "Sub
Get Abstract Value";
}
public override string GetVirtualValue()
{
return "Sub
GetVirtualValue";
}
public string GetValue() //new
{
return "Concrete
GetValue";
}
}
class Test
{
static void Main(string[]
args)
{
BaseClass instance1 = new SubClass();
Console.WriteLine(instance1.GetAbstractValue());
Console.WriteLine(instance1.GetVirtualValue());
Console.WriteLine(instance1.GetValue());
SubClass instance2 = new SubClass();
Console.WriteLine(instance2.GetAbstractValue());
Console.WriteLine(instance2.GetVirtualValue());
Console.WriteLine(instance2.GetValue());
Console.Read();
}
}
}
//Since
you cannot use abstract class for already inherited class. But you can use
interface for already inherited class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public interface inrefacename
{
void somemethod();
}
public abstract class BaseClass
{
public abstract string GetAbstractValue();
public virtual string GetVirtualValue()
{
return "Base
GetVirtualValue";
}
public string GetValue()
{
return "Base
GetValue";
}
}
public class classtest
{
public string GetValue1()
{
return "Base
GetValue";
}
}
public class classtest1 : classtest, inrefacename // baseclass – abstract class (will not
use abstract class)
{
public string GetValue4()
{
return "Base
GetValue";
}
public void somemethod()
{
Console.WriteLine("somemethod");
}
}
public class SubClass : BaseClass
{
public override string GetAbstractValue()
{
return "Sub
Get Abstract Value";
}
public override string GetVirtualValue()
{
return "Sub
GetVirtualValue";
}
public string GetValue() //new
{
return "Concrete
GetValue";
}
}
class Test
{
static void Main(string[]
args)
{
BaseClass instance1 = new SubClass();
Console.WriteLine(instance1.GetAbstractValue());
Console.WriteLine(instance1.GetVirtualValue());
Console.WriteLine(instance1.GetValue());
SubClass instance2 = new SubClass();
Console.WriteLine(instance2.GetAbstractValue());
Console.WriteLine(instance2.GetVirtualValue());
Console.WriteLine(instance2.GetValue());
Console.Read();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public interface IMyInterface
{
void SomeMethod();
void SomeOtherMethod();
}
public abstract class MyClass : IMyInterface
{
// Really implementing this
public void SomeMethod()
{
// ...
}
// Derived class must implement this
public abstract void SomeOtherMethod();
}
class Test
{
static void Main(string[]
args)
{
Console.Read();
}
}
}
//
Achieve multiple inheritance concept via interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public interface IA1
{
void m2();
void m();
}
public interface IA2
{
void m2();
}
public class CA : IA1, IA2
{
public void m()
{
}
public void m2()
{
Console.WriteLine("A");
}
}
public class CA1 : CA
{
public void m()
{
}
public void m2()
{
Console.WriteLine("B");
}
}
public class main1
{
static void Main(string[]
args)
{
CA c1 = new CA();
c1.m();
c1.m2();
CA1 ca1 = new CA1();
ca1.m();
ca1.m2();
Console.Read();
}
}
}
// Dynamic Binding (hiding)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class A
{
public virtual void WhoAreYou()
{ Console.WriteLine("I am an A"); }
public virtual void WhoAreYou1()
{ Console.WriteLine("I am an A1"); }
public virtual void WhoAreYou2()
{ Console.WriteLine("I am an A2"); }
}
class B : A
{
public override void WhoAreYou()
{ Console.WriteLine("I am a B"); }
}
class C : B
{
public new virtual void WhoAreYou() { Console.WriteLine("I am a C"); }
// public override void WhoAreYou1() {
Console.WriteLine("I am an C1"); }
public override void WhoAreYou2()
{ Console.WriteLine("I am an C2"); }
}
class D : C
{
public override void WhoAreYou()
{ Console.WriteLine("I am a D"); }
public override void WhoAreYou2()
{ Console.WriteLine("I am an D2"); }
}
class Test
{
static void Main(string[]
args)
{
C c = new D(); // where to extend C class in D
c.WhoAreYou();//
"I am a D"
A a = new D(); // where to extend A class in B
a.WhoAreYou();//
"I am a B"
A a1 = new D(); // No extend - virtual method
a1.WhoAreYou1(); //
"I am a A1"
B a2 = new C(); // where to extend b class in c
a2.WhoAreYou2(); //
"I am a C2"
B a3 = new D(); //
where to extend b class in c
a3.WhoAreYou2(); //
"I am a D2"
Console.ReadLine();
}
}
}
Sealed Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public sealed class classsealed
{
public string ID;
public double Price;
public string GetValue()
{
return "Sealed
class";
}
}
//public class subsealedclasss : classsealed
//{
// public string
subsealedmethod()
// {
//
return "subSealed class";
// }
//}
class Test
{
static void Main(string[]
args)
{
classsealed sealedobject = new classsealed();
Console.WriteLine(sealedobject.GetValue());
sealedobject.ID = "10";
sealedobject.Price = 12.23;
//subsealedclassssubsealedobject = new
subsealedclasss();
//subsealedobject.subsealedmethod();
Console.Read();
}
}
}
//
Static Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public static class staticclass // : object //7. Static class can only be inherited only from
object class
{
// int a; // 2. Static class
can have only static members.
static int i=1; // access
//public void method() // Static class can
have only static method.
//{
// Console.WriteLine("Test
method");
//}
public static void method1()
{
Console.WriteLine("Test static method");
Console.WriteLine(i);
}
//staticclass() // static classess cannot
have instance constructors
//{
// Console.WriteLine("instance
constructor");
//}
static staticclass()
{
Console.WriteLine("static constructor");
}
}
public class Bus
{
// Static constructor:
static Bus()
{
System.Console.WriteLine("The static constructor invoked.");
}
public static void Drive()
{
System.Console.WriteLine("The Drive method invoked.");
}
}
//public class Testclass :
staticclass // 4. Static class is sealed.
So static class cannot be inherited
//{
//}
class Test
{
static void Main(string[]
args)
{
// staticclass obj = new
staticclass(); // static class cannot be instantiated
staticclass.method1(); // first constructor only executed
//staticclass.i = 6;
Bus.Drive();
Console.ReadLine();
}
}
}
//
static method
public class Bus
{
// Static constructor:
static Bus()
{
System.Console.WriteLine("The
static constructor invoked.");
}
public static void Drive()
{
System.Console.WriteLine("The
Drive method invoked.");
}
}
class TestBus
{
static void Main()
{
Bus.Drive();
}
}
// A static constructor is called automatically
to initialize and first called
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[]
args)
{
Child t = new Child();
}
}
class Parent
{
public Parent()
{
Console.WriteLine("Parent Instance Constructor");
Console.ReadKey();
}
static Parent()
{
Console.WriteLine("Parent Static Constructor");
Console.ReadKey();
}
}
class Child : Parent
{
public Child()
{
Console.WriteLine("Child Instance Constructor");
Console.ReadKey();
}
static Child()
{
Console.WriteLine("Child Static Constructor");
Console.ReadKey();
}
}
}
Output:
Child
Static Constructor
Parent
Static Constructor
Parent
Instance Constructor
Child
Instance Constructor
//
Hash table
HashTable:
HashTable stores a key-value pair type
collection of data.
NOTE:
Both key and value are object
Both key and value are object
the key values are UNIQUE
Functions:
1. Clear
2. Remove
3. Containskey
4. Containsvalue
5. Contains
6. Add
Variables
1. Count
Remove:
hash_table_name.Remove(key);
hash_table_name.Remove(key);
Remove All Values:
hash_table_name.Clear()
hash_table_name.Clear()
ContainsKey(Key)
its returns true or false..
its returns true or false..
ContainsValue(value):
EXAMPLE
Hashtable sampleHt = New Hashtable
sampleHt.add("US",”united States”) // Area- key. 1000 - value
sampleHt.add("IND",”India”)
Hashtable sampleHt = New Hashtable
sampleHt.add("US",”united States”) // Area- key. 1000 - value
sampleHt.add("IND",”India”)
// To fetch the value we can use name, for
example
String countryname=convert.tostring(sampleHt[“US”]);
sampleHt.remove(“US”);
IDictionaryEnumerator idic= sampleHt.GetEnumerator();
while(idic.MoveNext())
{
idic.Value.ToString();
}
// Why c# not support multiple inheritance?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class A
{
protected void samplemethod();
}
class B
{
protected void samplemethod();
}
class C : A, B
{
}
class Test
{
static void Main(string[]
args)
{
C obj = new C();
obj.samplemethod(); // here
which method to call now A or B?
}
}
}
Interface with objects:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public interface IMyInterface
{
void SomeMethod();
void SomeOtherMethod();
}
public class MyClass : IMyInterface
{
// Really implementing this
public void SomeMethod()
{
Console.WriteLine("Interface object");
}
// Derived class must implement this
public void SomeOtherMethod()
{
}
}
class Test
{
static void Main(string[]
args)
{
IMyInterface obj = new MyClass();
obj.SomeMethod();
Console.ReadLine();
}
}
}
No comments:
Post a Comment