Abstract Classes & Interface

  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Abstract Classes & Interface as PDF for free.

More details

  • Words: 1,080
  • Pages: 6
What are Abstract Classes? An abstract class is a class that cannot be instantiated and must be inherited. An Abstract class contains abstract and non-abstract method(s). An abstract method needs to be overridden and implemented in a derived non-abstract class. An abstract class is essentially a blueprint for a class without any implementation. Example: -------------------Code C# Abstract Class -------------------public abstract class Car Declare Abstract Method { protected int headlights = 100; // Declare Variable value public abstract void price(); // Abstract Method public void clsbaseMethod() // NON-Abstract Method { Console.WriteLine("it a Non-abstract method"); } } Define Abstract Method

public class tool : Car { public override void price() { Console.WriteLine("Child Move"); } public new void clsbaseMethod() { Console.WriteLine(headlights); } } -------------------Code VB.Net Abstract Class -------------------Public MustInherit Class Car ' Declare Variable value Protected headlights As Int16 = 100 Declare Abstract Method ' Declare abstract Method Public MustOverride Sub Move(ByVal NewX As Integer, ByVal NewY As Integer) ' Declare NON-abstract Method Public Overridable Sub clsbaseMethod() Console.WriteLine("Parent") End Sub End Class Define Abstract Method

Public Class tool Inherits Car Public Overrides Sub Move(ByVal NewX As Integer, ByVal NewY As Integer) Console.WriteLine("Child Move") End Sub Public Overrides Sub clsbaseMethod() Console.WriteLine(headlights) End Sub End Class

4

NOTE: Base class abstract method must to define in drive class. An abstract class cannot be a sealed class. An abstract method cannot be private. An abstract method cannot have the modifier virtual. Because an abstract method is implicitly virtual. An abstract member cannot be static. The access modifier of the abstract method should be same in both the abstract class and its derived class. If you declare an abstract method as protected, it should be protected in its derived class. Otherwise, the compiler will raise an error.

1. 2. 3. 4. 5. 6.

Difference between an abstract method & virtual method: Abstract member is not implemented (define) in the base class and must be implemented in derived classes Virtual method must be implemented (define) in the base class, but may be optionally overriden in the 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(); } }

4

What are Interfaces Classes?

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 be placed in drive class. The interface can't contain constants, data fields, constructors, destructors and static 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 multiple inheritance

Example 1: -------------------Code C# -------------------public delegate void StringListEvent(IStringList sender); public interface IStringList { void Add(string s); //Method int Count { get; } //Property event StringListEvent Changed; //Event string this[int index] { get; set; } //Indexer }

Example 2: -------------------Code C# -------------------interface Icls1 { void tag(); string Text { get; set; } }

1st interface Class

Declare methods, property

interface Icls2 { void cls2_tag(); string cls2_Text { get; set; } }

2nd interface Class

Multiple inheritance through Interface

public class gcls: Icls1,Icls2 { private string strtextVal; public void tag()

4

Define methods, property

{ Console.WriteLine("Use Interface in drive class"); } public string Text { get { return strtextVal; Define methods, property } 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 Interface Public Interface Icls2 Sub cls2_tag() Property cls2_text() End Interface Public Class gcls Implements Icls1, Icls2 Private strtextVal As String Public Sub tag() Implements Icls1.tag Console.WriteLine("Use Interface in drive class") End Sub Public Property text() As Object Implements Icls1.text Get text = strtextVal End Get Set(ByVal value As Object)

4

strtextVal = value End Set End Property Public Sub cls2_tag() Implements Icls2.cls2_tag Console.WriteLine("Use Interface in drive class") End Sub Public Property cls2_text() As Object Implements Icls2.cls2_text Get cls2_text = strtextVal End Get Set(ByVal value As Object) strtextVal = value End Set End Property End Class

Why do we use interfaces?

We use interfaces because they allow reusability of code as well as help create useful relationships between objects.

4

Abstract class vs. Interface Interface can only contain events, indexers, methods or properties and of these defined in interface are by default public. abstract; while as abstract class can contain abstract methods, abstract property as well as other members with implementation. An Interface can support multiple inheritance, while abstract class cannot support multiple inheritance. An Interface can be inherited from by structures, when abstract class cannot be inherited from by structures. Feature

Interface

Abstract class

Multiple inheritance

A class may inherit several interfaces.

A class may inherit only one abstract class.

Default implementation

An interface just defines a contract, it cannot provide any implementation.

An abstract class can provide complete, default code and/or just the details that have to be overridden.

Access Modfiers

An interface cannot have access modifiers for the methods, events, properties etc, everything is assumed as public

An abstract class can contain access modifiers for the methods, properties etc

Core VS Peripheral

Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.

An abstract class defines the core identity of a class and there it is used for objects of the same type.

Homogeneity

If various implementations only share method signatures then it is better to use Interfaces.

If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.

Adding functionality (Versioning)

If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.

If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.

Fields and Constants

No fields can be defined in interfaces

An abstract class can have fields and constrants defined

4

Related Documents