abstract classes & interface
TRANSCRIPT
-
8/14/2019 Abstract Classes & Interface
1/6
4
What are Abstract Classes?
An abstract class is a class that cannot be instantiated and must be inherited. An Abstract classcontains abstract and non-abstract method(s). An abstract method needs to be overridden andimplemented in a derived non-abstract class. An abstract class is essentially a blueprint for a classwithout any implementation.
Example:
--------------------Code C#
--------------------public abstract class Car
{protected int headlights = 100; // Declare Variable valuepublic abstract void price(); // Abstract Methodpublic void clsbaseMethod() // NON-Abstract Method{
Console.WriteLine("it a Non-abstract method");}
}
public class tool : Car{
public override void price(){
Console.WriteLine("Child Move");}public new void clsbaseMethod(){
Console.WriteLine(headlights);}
}
--------------------
Code VB.Net--------------------Public MustInherit Class Car
' Declare Variable valueProtected headlights As Int16 = 100
' Declare abstract MethodPublic MustOverride Sub Move(ByVal NewX As Integer, ByVal NewY As Integer)
' Declare NON-abstract MethodPublic Overridable Sub clsbaseMethod()
Console.WriteLine("Parent")End Sub
End Class
Public Class tool
Inherits CarPublic Overrides Sub Move(ByVal NewX As Integer, ByVal NewY As Integer)
Console.WriteLine("Child Move")End SubPublic Overrides Sub clsbaseMethod()
Console.WriteLine(headlights)End Sub
End Class
Abstract Class
Abstract Class
Declare Abstract Method
Define Abstract Method
Declare Abstract Method
Define Abstract Method
-
8/14/2019 Abstract Classes & Interface
2/6
4
NOTE:
1. Base class abstract method must to define in drive class.
2. An abstract class cannot be a sealed class.
3. An abstract method cannot be private.
4. An abstract method cannot have the modifier virtual. Because an abstract method is implicitly
virtual.
5. An abstract member cannot be static.
6. The access modifier of the abstract method should be same in both the abstract class and itsderived class. If you declare an abstract method as protected, it should be protected in itsderived class. Otherwise, the compiler will raise an error.
Difference between an abstract method & virtual method:
Abstract memberis not implemented (define) in the base class and must be implemented in derivedclasses
Virtual methodmust be implemented (define) in the base class, but may be optionally overriden inthe derived class if different behavior is required.
Example :--------------------
Code C#--------------------class Hello{
public abstract class Talk{
public abstract void speak(); // not Implemented
public virtual void goodbye()// its implements{
Console.WriteLine("Talk class says goodbye!");}
}
public class SayHello : Talk{
public override void speak(){
Console.WriteLine("Hello!");}
}
static void Main()
{SayHello hello = new SayHello();hello.speak();hello.goodbye();
}}
-
8/14/2019 Abstract Classes & Interface
3/6
4
What are InterfacesClasses?
An Interface is a reference type and it contains only non-implement (only means declare) members.Interface's members can be Events, Methods, Properties and Indexers. Any implementation must beplaced in drive class. The interface can't contain constants, data fields, constructors, destructors andstatic members. All the member declarations inside interface are implicitly public.Interfaces in C# are provided as a replacement of multiple inheritance. Because C# does not support multipleinheritance
Example 1:--------------------
Code C#--------------------public delegate void StringListEvent(IStringList sender);
public interface IStringList{
void Add(string s); //Methodint Count { get; } //Propertyevent StringListEvent Changed; //Eventstring this[int index] { get; set; } //Indexer
}
Example 2:
--------------------Code C#
--------------------interface Icls1
{void tag();string Text{
get;set;
}}
interface Icls2{
void cls2_tag();string cls2_Text
{get;set;
}}
public class gcls: Icls1,Icls2{
private string strtextVal;public void tag()
1st interface Class
2nd interface Class
Multiple inheritance through Interface
Declare methods, property
-
8/14/2019 Abstract Classes & Interface
4/6
4
{Console.WriteLine("Use Interface in drive class");
}public string Text{
get{
return strtextVal;}set{
strtextVal = value;}
}public void cls2_tag(){
Console.WriteLine("Use Interface in drive class");}public string cls2_Text{
get{
return strtextVal;}set{
strtextVal = value;}
}}
public class main{gcls objcls = new gcls();objcls.cls2_tag();
}
--------------------Code VB.Net
--------------------Public Interface Icls1
Sub tag()Property text()
End InterfacePublic Interface Icls2
Sub cls2_tag()Property cls2_text()
End Interface
Public Class gclsImplements Icls1, Icls2Private strtextVal As StringPublic Sub tag() Implements Icls1.tag
Console.WriteLine("Use Interface in drive class")End Sub
Public Property text() As Object Implements Icls1.textGet
text = strtextValEnd GetSet(ByVal value As Object)
Define methods, property
Define methods, property
-
8/14/2019 Abstract Classes & Interface
5/6
4
strtextVal = valueEnd Set
End Property
Public Sub cls2_tag() Implements Icls2.cls2_tagConsole.WriteLine("Use Interface in drive class")
End Sub
Public Property cls2_text() As Object Implements Icls2.cls2_textGet
cls2_text = strtextValEnd GetSet(ByVal value As Object)
strtextVal = valueEnd Set
End PropertyEnd Class
Why do we use interfaces?We use interfaces because they allow reusability of code as well as help create useful relationships between objects.
-
8/14/2019 Abstract Classes & Interface
6/6